Skip to content

Commit

Permalink
auto merge of #5070 : luniv/servo/script-before-after-events, r=Ms2ger
Browse files Browse the repository at this point in the history
  • Loading branch information
bors-servo committed Feb 26, 2015
2 parents a8b55e8 + 0b085df commit c1645bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
6 changes: 3 additions & 3 deletions components/script/dom/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)
}
}
73 changes: 52 additions & 21 deletions components/script/dom/htmlscriptelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

0 comments on commit c1645bd

Please sign in to comment.