Skip to content

Commit

Permalink
Add 'input' event for HTMLInputElement
Browse files Browse the repository at this point in the history
  • Loading branch information
chkimes committed Jan 2, 2016
1 parent d4efd20 commit 12a0884
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
32 changes: 32 additions & 0 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -14,6 +14,7 @@ use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::document::Document;
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers, LayoutElementHelpers};
use dom::event::{Event, EventBubbles, EventCancelable};
Expand All @@ -28,6 +29,8 @@ use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan;
use script_task::ScriptTaskEventCategory::InputEvent;
use script_task::{CommonScriptMsg, Runnable};
use script_traits::ScriptMsg as ConstellationMsg;
use selectors::states::*;
use std::borrow::ToOwned;
Expand Down Expand Up @@ -690,6 +693,18 @@ impl VirtualMethods for HTMLInputElement {
},
DispatchInput => {
self.value_changed.set(true);

if event.IsTrusted() {
let window = window_from_node(self);
let window = window.r();
let chan = window.user_interaction_task_source();
let handler = Trusted::new(self.upcast::<Node>(), chan.clone());
let dispatcher = ChangeEventRunnable {
element: handler,
};
let _ = chan.send(CommonScriptMsg::RunnableMsg(InputEvent, box dispatcher));
}

self.force_relayout();
event.PreventDefault();
}
Expand Down Expand Up @@ -920,3 +935,20 @@ impl Activatable for HTMLInputElement {
}
}
}

pub struct ChangeEventRunnable {
pub element: Trusted<Node>,
}

impl Runnable for ChangeEventRunnable {
fn handler(self: Box<ChangeEventRunnable>) {
let target = self.element.root();
let window = window_from_node(target.r());
let window = window.r();
let event = Event::new(GlobalRef::Window(window),
atom!("input"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
target.upcast::<EventTarget>().dispatch_event(&event);
}
}
32 changes: 5 additions & 27 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -8,26 +8,26 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{LayoutJS, Root};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable};
use dom::document::Document;
use dom::element::RawLayoutElementHelpers;
use dom::element::{AttributeMutation, Element};
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::event::{Event};
use dom::htmlelement::HTMLElement;
use dom::htmlfieldsetelement::HTMLFieldSetElement;
use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::htmlinputelement::ChangeEventRunnable;
use dom::keyboardevent::KeyboardEvent;
use dom::node::{ChildrenMutation, Node, NodeDamage, UnbindContext};
use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan;
use script_task::ScriptTaskEventCategory::InputEvent;
use script_task::{CommonScriptMsg, Runnable};
use script_task::{CommonScriptMsg};
use script_traits::ScriptMsg as ConstellationMsg;
use selectors::states::*;
use std::cell::Cell;
Expand Down Expand Up @@ -233,17 +233,6 @@ impl HTMLTextAreaElement {
let doc = document_from_node(self);
doc.content_changed(self.upcast(), NodeDamage::OtherNodeDamage)
}

fn dispatch_change_event(&self) {
let window = window_from_node(self);
let window = window.r();
let event = Event::new(GlobalRef::Window(window),
atom!("input"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);

self.upcast::<EventTarget>().dispatch_event(&event);
}
}

impl VirtualMethods for HTMLTextAreaElement {
Expand Down Expand Up @@ -330,7 +319,7 @@ impl VirtualMethods for HTMLTextAreaElement {
let window = window_from_node(self);
let window = window.r();
let chan = window.user_interaction_task_source();
let handler = Trusted::new(self, chan.clone());
let handler = Trusted::new(self.upcast::<Node>(), chan.clone());
let dispatcher = ChangeEventRunnable {
element: handler,
};
Expand All @@ -352,14 +341,3 @@ impl VirtualMethods for HTMLTextAreaElement {
}

impl FormControl for HTMLTextAreaElement {}

pub struct ChangeEventRunnable {
element: Trusted<HTMLTextAreaElement>,
}

impl Runnable for ChangeEventRunnable {
fn handler(self: Box<ChangeEventRunnable>) {
let target = self.element.root();
target.dispatch_change_event();
}
}

0 comments on commit 12a0884

Please sign in to comment.