Implement EventTarget::fire_simple_event and EventTarget::fire_simple_event_params

This commit is contained in:
Arthur Skobara 2015-12-20 12:52:28 +06:00
parent df087cc6cf
commit d38771e270
8 changed files with 61 additions and 71 deletions

View file

@ -558,12 +558,8 @@ impl Document {
self.ready_state.set(state);
let event = Event::new(GlobalRef::Window(&self.window),
atom!("readystatechange"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
let target = self.upcast::<EventTarget>();
let _ = event.fire(target);
self.upcast::<EventTarget>().fire_simple_event("readystatechange",
GlobalRef::Window(&self.window));
}
/// Return whether scripting is enabled or not

View file

@ -11,12 +11,12 @@ use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
use dom::bindings::codegen::Bindings::EventTargetBinding::EventTargetMethods;
use dom::bindings::codegen::UnionTypes::EventOrString;
use dom::bindings::error::{Error, Fallible, report_pending_exception};
use dom::bindings::global::global_root_from_reflector;
use dom::bindings::global::{GlobalRef, global_root_from_reflector};
use dom::bindings::inheritance::{Castable, EventTargetTypeId};
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::errorevent::ErrorEvent;
use dom::event::Event;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventdispatcher::dispatch_event;
use dom::virtualmethods::VirtualMethods;
use dom::window::Window;
@ -329,6 +329,24 @@ impl EventTarget {
pub fn has_handlers(&self) -> bool {
!self.handlers.borrow().is_empty()
}
/// Implements https://html.spec.whatwg.org/multipage/#fire-a-simple-event
pub fn fire_simple_event(&self, name: &str, win: GlobalRef) -> Root<Event> {
self.fire_simple_event_params(name, EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable, win)
}
/// Implements more customizable variant of EventTarget::fire_simple_event.
pub fn fire_simple_event_params(&self, name: &str,
bubbles: EventBubbles,
cancelable: EventCancelable,
win: GlobalRef) -> Root<Event> {
let event = Event::new(win, Atom::from(name), bubbles, cancelable);
event.fire(self);
event
}
}
impl EventTargetMethods for EventTarget {

View file

@ -16,7 +16,7 @@ use dom::bindings::js::{Root};
use dom::bindings::reflector::Reflectable;
use dom::document::Document;
use dom::element::Element;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::event::{EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::htmlbuttonelement::HTMLButtonElement;
use dom::htmldatalistelement::HTMLDataListElement;
@ -169,23 +169,18 @@ impl HTMLFormElement {
{
if self.interactive_validation().is_err() {
// TODO: Implement event handlers on all form control elements
// XXXKiChjang: We're also calling the following two statements quite often,
// we should refactor it into a function
let event = Event::new(GlobalRef::Window(win.r()),
atom!("invalid"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(self.upcast());
self.upcast::<EventTarget>()
.fire_simple_event("invalid", GlobalRef::Window(win.r()));
return;
}
}
// Step 5
if submit_method_flag == SubmittedFrom::NotFromFormSubmitMethod {
let event = Event::new(GlobalRef::Window(win.r()),
atom!("submit"),
let event = self.upcast::<EventTarget>()
.fire_simple_event_params("submit",
EventBubbles::Bubbles,
EventCancelable::Cancelable);
event.fire(self.upcast());
EventCancelable::Cancelable,
GlobalRef::Window(win.r()));
if event.DefaultPrevented() {
return;
}
@ -280,11 +275,11 @@ impl HTMLFormElement {
// Step 5-6
let win = window_from_node(self);
let unhandled_invalid_controls = invalid_controls.into_iter().filter_map(|field| {
let event = Event::new(GlobalRef::Window(win.r()),
atom!("invalid"),
let event = field.as_event_target()
.fire_simple_event_params("invalid",
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable);
event.fire(field.as_event_target());
EventCancelable::Cancelable,
GlobalRef::Window(win.r()));
if !event.DefaultPrevented() { return Some(field); }
None
}).collect::<Vec<FormSubmittableElement>>();
@ -393,11 +388,11 @@ impl HTMLFormElement {
}
let win = window_from_node(self);
let event = Event::new(GlobalRef::Window(win.r()),
atom!("reset"),
let event = self.upcast::<EventTarget>()
.fire_simple_event_params("reset",
EventBubbles::Bubbles,
EventCancelable::Cancelable);
event.fire(self.upcast());
EventCancelable::Cancelable,
GlobalRef::Window(win.r()));
if event.DefaultPrevented() {
return;
}
@ -430,6 +425,7 @@ impl HTMLFormElement {
};
self.marked_for_reset.set(false);
}
}
// TODO: add file support

View file

@ -17,7 +17,8 @@ use dom::bindings::reflector::Reflectable;
use dom::customevent::CustomEvent;
use dom::document::Document;
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::event::Event;
use dom::eventtarget::EventTarget;
use dom::htmlelement::HTMLElement;
use dom::node::{Node, window_from_node};
use dom::urlhelper::UrlHelper;
@ -209,12 +210,7 @@ impl HTMLIFrameElement {
// Step 4
let window = window_from_node(self);
let event = Event::new(GlobalRef::Window(window.r()),
atom!("load"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(self.upcast());
self.upcast::<EventTarget>().fire_simple_event("load", GlobalRef::Window(window.r()));
// TODO Step 5 - unset child document `mut iframe load` flag
}
}

View file

@ -15,7 +15,7 @@ use dom::bindings::js::{LayoutJS, Root};
use dom::bindings::refcounted::Trusted;
use dom::document::Document;
use dom::element::AttributeMutation;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::htmlelement::HTMLElement;
use dom::node::{Node, NodeDamage, document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods;
@ -77,12 +77,7 @@ impl Runnable for ImageResponseHandlerRunnable {
// Fire image.onload
let window = window_from_node(document.r());
let event = Event::new(GlobalRef::Window(window.r()),
atom!("load"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(element.upcast());
element.upcast::<EventTarget>().fire_simple_event("load", GlobalRef::Window(window.r()));
// Trigger reflow
window.add_pending_reflow();
}

View file

@ -858,19 +858,16 @@ impl Activatable for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#radio-button-state-(type=radio):activation-behavior
if self.mutable() {
let win = window_from_node(self);
let target = self.upcast();
let target = self.upcast::<EventTarget>();
let event = Event::new(GlobalRef::Window(win.r()),
atom!("input"),
target.fire_simple_event_params("input",
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
event.fire(target);
let event = Event::new(GlobalRef::Window(win.r()),
atom!("change"),
EventCancelable::NotCancelable,
GlobalRef::Window(win.r()));
target.fire_simple_event_params("change",
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
event.fire(target);
EventCancelable::NotCancelable,
GlobalRef::Window(win.r()));
}
},
_ => ()

View file

@ -498,10 +498,7 @@ impl Runnable for ConnectionEstablishedTask {
// Step 5: Cookies.
// Step 6.
let event = Event::new(global.r(), atom!("open"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(ws.upcast());
ws.upcast().fire_simple_event("open", global.r());
}
}
@ -539,11 +536,10 @@ impl Runnable for CloseTask {
ws.full.set(false);
//A Bad close
ws.clean_close.set(false);
let event = Event::new(global.r(),
atom!("error"),
ws.upcast().fire_simple_event_params("error",
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable);
event.fire(ws.upcast());
EventCancelable::Cancelable,
global.r());
}
let reason = ws.reason.borrow().clone();
/*In addition, we also have to fire a close even if error event fired

View file

@ -126,11 +126,7 @@ impl Worker {
pub fn dispatch_simple_error(address: TrustedWorkerAddress) {
let worker = address.root();
let global = worker.r().global.root();
let event = Event::new(global.r(),
atom!("error"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(worker.upcast());
worker.upcast().fire_simple_event("error", global.r());
}
pub fn handle_error_message(address: TrustedWorkerAddress, message: DOMString,