Implement 'beforescriptexecute' and 'afterscriptexecute' events.

Spec: https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block, sections 2.b.2 & 2.b.9
This commit is contained in:
James Gilbertson 2015-02-25 15:09:20 -07:00
parent 259792e481
commit 0b085df1bc
3 changed files with 55 additions and 29 deletions

View file

@ -246,7 +246,7 @@ impl<'a> EventMethods for JSRef<'a, Event> {
pub trait EventHelpers {
fn set_trusted(self, trusted: bool);
fn fire(self, target: JSRef<EventTarget>);
fn fire(self, target: JSRef<EventTarget>) -> bool;
}
impl<'a> EventHelpers for JSRef<'a, Event> {
@ -255,8 +255,8 @@ impl<'a> EventHelpers for JSRef<'a, Event> {
}
// https://html.spec.whatwg.org/multipage/webappapis.html#fire-a-simple-event
fn fire(self, target: JSRef<EventTarget>) {
fn fire(self, target: JSRef<EventTarget>) -> bool {
self.set_trusted(true);
target.dispatch_event(self);
target.dispatch_event(self)
}
}

View file

@ -100,6 +100,12 @@ pub trait HTMLScriptElementHelpers {
// Queues error event
fn queue_error_event(self);
/// Dispatch beforescriptexecute event.
fn dispatch_before_script_execute_event(self) -> bool;
/// Dispatch afterscriptexecute event.
fn dispatch_after_script_execute_event(self);
/// Dispatch load event.
fn dispatch_load_event(self);
@ -297,9 +303,9 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
};
// Step 2.b.2.
// TODO: Fire a simple event named beforescriptexecute that bubbles and
// is cancelable at the script element.
// If the event is canceled, then abort these steps.
if !self.dispatch_before_script_execute_event() {
return;
}
// Step 2.b.3.
// TODO: If the script is from an external file, then increment the
@ -330,8 +336,7 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
// doc, if it was incremented in the earlier step.
// Step 2.b.9.
// TODO: Fire a simple event named afterscriptexecute that bubbles (but
// is not cancelable) at the script element.
self.dispatch_after_script_execute_event();
// Step 2.b.10.
if external {
@ -359,26 +364,28 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
chan.send(ScriptMsg::RunnableMsg(dispatcher)).unwrap();
}
fn dispatch_before_script_execute_event(self) -> bool {
self.dispatch_event("beforescriptexecute".to_owned(),
EventBubbles::Bubbles,
EventCancelable::Cancelable)
}
fn dispatch_after_script_execute_event(self) {
self.dispatch_event("afterscriptexecute".to_owned(),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
}
fn dispatch_load_event(self) {
let window = window_from_node(self).root();
let window = window.r();
let event = Event::new(GlobalRef::Window(window),
"load".to_owned(),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable).root();
let target: JSRef<EventTarget> = EventTargetCast::from_ref(self);
event.r().fire(target);
self.dispatch_event("load".to_owned(),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
}
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);
self.dispatch_event("error".to_owned(),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
}
fn is_javascript(self) -> bool {
@ -420,6 +427,30 @@ impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
}
}
trait PrivateHTMLScriptElementHelpers {
fn dispatch_event(self,
type_: DOMString,
bubbles: EventBubbles,
cancelable: EventCancelable) -> bool;
}
impl<'a> PrivateHTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
fn dispatch_event(self,
type_: DOMString,
bubbles: EventBubbles,
cancelable: EventCancelable) -> bool {
let window = window_from_node(self).root();
let window = window.r();
let event = Event::new(GlobalRef::Window(window),
type_,
bubbles,
cancelable).root();
let event = event.r();
let target: JSRef<EventTarget> = EventTargetCast::from_ref(self);
event.fire(target)
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLScriptElement> {
fn super_type<'b>(&'b self) -> Option<&'b VirtualMethods> {
let htmlelement: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);

View file

@ -1,5 +0,0 @@
[script-before-after-events.html]
type: testharness
[\'beforescriptexecute\'/\'afterscriptexecute\' events have been fired]
expected: FAIL