mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Script elements now fire async error events (fixes #4506)
This commit is contained in:
parent
ec42c01fba
commit
52f8b0ceb7
1 changed files with 37 additions and 3 deletions
|
@ -40,6 +40,8 @@ use url::UrlParser;
|
||||||
pub struct HTMLScriptElement {
|
pub struct HTMLScriptElement {
|
||||||
htmlelement: HTMLElement,
|
htmlelement: HTMLElement,
|
||||||
|
|
||||||
|
error_occurred: Cell<bool>,
|
||||||
|
|
||||||
/// https://html.spec.whatwg.org/multipage/scripting.html#already-started
|
/// https://html.spec.whatwg.org/multipage/scripting.html#already-started
|
||||||
already_started: Cell<bool>,
|
already_started: Cell<bool>,
|
||||||
|
|
||||||
|
@ -68,6 +70,7 @@ impl HTMLScriptElement {
|
||||||
creator: ElementCreator) -> HTMLScriptElement {
|
creator: ElementCreator) -> HTMLScriptElement {
|
||||||
HTMLScriptElement {
|
HTMLScriptElement {
|
||||||
htmlelement: HTMLElement::new_inherited(HTMLElementTypeId::HTMLScriptElement, localName, prefix, document),
|
htmlelement: HTMLElement::new_inherited(HTMLElementTypeId::HTMLScriptElement, localName, prefix, document),
|
||||||
|
error_occurred: Cell::new(false),
|
||||||
already_started: Cell::new(false),
|
already_started: Cell::new(false),
|
||||||
parser_inserted: Cell::new(creator == ElementCreator::ParserCreated),
|
parser_inserted: Cell::new(creator == ElementCreator::ParserCreated),
|
||||||
non_blocking: Cell::new(creator != ElementCreator::ParserCreated),
|
non_blocking: Cell::new(creator != ElementCreator::ParserCreated),
|
||||||
|
@ -93,8 +96,14 @@ pub trait HTMLScriptElementHelpers {
|
||||||
/// Set the "already started" flag (<https://whatwg.org/html/#already-started>)
|
/// Set the "already started" flag (<https://whatwg.org/html/#already-started>)
|
||||||
fn mark_already_started(self);
|
fn mark_already_started(self);
|
||||||
|
|
||||||
|
// Queues error event
|
||||||
|
fn queue_error_event(self);
|
||||||
|
|
||||||
/// Dispatch load event.
|
/// Dispatch load event.
|
||||||
fn dispatch_load_event(self);
|
fn dispatch_load_event(self);
|
||||||
|
|
||||||
|
/// Dispatch error event.
|
||||||
|
fn dispatch_error_event(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Supported script types as defined by
|
/// Supported script types as defined by
|
||||||
|
@ -189,7 +198,7 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
|
||||||
let (origin, source, url) = match element.get_attribute(ns!(""), &atom!("src")).root() {
|
let (origin, source, url) = match element.get_attribute(ns!(""), &atom!("src")).root() {
|
||||||
Some(src) => {
|
Some(src) => {
|
||||||
if src.r().Value().is_empty() {
|
if src.r().Value().is_empty() {
|
||||||
// TODO: queue a task to fire a simple event named `error` at the element
|
self.queue_error_event();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match UrlParser::new().base_url(&base_url).parse(src.r().Value().as_slice()) {
|
match UrlParser::new().base_url(&base_url).parse(src.r().Value().as_slice()) {
|
||||||
|
@ -206,13 +215,14 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!("error loading script {}", src.r().Value());
|
error!("error loading script {}", src.r().Value());
|
||||||
|
self.queue_error_event();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
// TODO: queue a task to fire a simple event named `error` at the element
|
|
||||||
error!("error parsing URL for script {}", src.r().Value());
|
error!("error parsing URL for script {}", src.r().Value());
|
||||||
|
self.queue_error_event();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,6 +246,15 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn queue_error_event(self) {
|
||||||
|
self.error_occurred.set(true);
|
||||||
|
let window = window_from_node(self).root();
|
||||||
|
let window = window.r();
|
||||||
|
let chan = window.script_chan();
|
||||||
|
let handler = Trusted::new(window.get_cx(), self, chan.clone());
|
||||||
|
chan.send(ScriptMsg::RunnableMsg(box handler));
|
||||||
|
}
|
||||||
|
|
||||||
fn dispatch_load_event(self) {
|
fn dispatch_load_event(self) {
|
||||||
let window = window_from_node(self).root();
|
let window = window_from_node(self).root();
|
||||||
let window = window.r();
|
let window = window.r();
|
||||||
|
@ -247,6 +266,17 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
|
||||||
event.r().fire(target);
|
event.r().fire(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dispatch_error_event(self) {
|
||||||
|
let window = window_from_node(self).root();
|
||||||
|
let window = window.r();
|
||||||
|
let event = Event::new(GlobalRef::Window(window),
|
||||||
|
"error".to_owned(),
|
||||||
|
EventBubbles::DoesNotBubble,
|
||||||
|
EventCancelable::NotCancelable).root();
|
||||||
|
let target: JSRef<EventTarget> = EventTargetCast::from_ref(self);
|
||||||
|
event.r().fire(target);
|
||||||
|
}
|
||||||
|
|
||||||
fn is_javascript(self) -> bool {
|
fn is_javascript(self) -> bool {
|
||||||
let element: JSRef<Element> = ElementCast::from_ref(self);
|
let element: JSRef<Element> = ElementCast::from_ref(self);
|
||||||
match element.get_attribute(ns!(""), &atom!("type")).root().map(|s| s.r().Value()) {
|
match element.get_attribute(ns!(""), &atom!("type")).root().map(|s| s.r().Value()) {
|
||||||
|
@ -361,6 +391,10 @@ impl<'a> HTMLScriptElementMethods for JSRef<'a, HTMLScriptElement> {
|
||||||
impl Runnable for Trusted<HTMLScriptElement> {
|
impl Runnable for Trusted<HTMLScriptElement> {
|
||||||
fn handler(self: Box<Trusted<HTMLScriptElement>>) {
|
fn handler(self: Box<Trusted<HTMLScriptElement>>) {
|
||||||
let target = self.to_temporary().root();
|
let target = self.to_temporary().root();
|
||||||
target.r().dispatch_load_event();
|
if target.r().error_occurred.get() {
|
||||||
|
target.r().dispatch_error_event();
|
||||||
|
} else {
|
||||||
|
target.r().dispatch_load_event();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue