diff options
author | Josh Matthews <josh@joshmatthews.net> | 2014-10-04 08:52:15 -0400 |
---|---|---|
committer | Josh Matthews <josh@joshmatthews.net> | 2014-11-13 11:24:14 -0500 |
commit | e999843183f27286337705a032ed3d88d361b372 (patch) | |
tree | 643377cebe4adccf2d64294440a22721c4d716f3 /components | |
parent | bb7074698afbe02776ea02861d795d885ace923e (diff) | |
download | servo-e999843183f27286337705a032ed3d88d361b372.tar.gz servo-e999843183f27286337705a032ed3d88d361b372.zip |
Fill in KeyboardEvent.
Diffstat (limited to 'components')
-rw-r--r-- | components/script/dom/keyboardevent.rs | 167 | ||||
-rw-r--r-- | components/script/dom/mouseevent.rs | 1 | ||||
-rw-r--r-- | components/script/dom/webidls/KeyboardEvent.webidl | 42 |
3 files changed, 192 insertions, 18 deletions
diff --git a/components/script/dom/keyboardevent.rs b/components/script/dom/keyboardevent.rs index de9c8992d9d..be96a181b30 100644 --- a/components/script/dom/keyboardevent.rs +++ b/components/script/dom/keyboardevent.rs @@ -4,19 +4,34 @@ use dom::bindings::codegen::Bindings::KeyboardEventBinding; use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods; -use dom::bindings::codegen::InheritTypes::KeyboardEventDerived; +use dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods; +use dom::bindings::codegen::InheritTypes::{UIEventCast, KeyboardEventDerived}; use dom::bindings::error::Fallible; use dom::bindings::global::GlobalRef; -use dom::bindings::js::{JSRef, Temporary}; -use dom::bindings::utils::{Reflectable, Reflector/*, reflect_dom_object*/}; +use dom::bindings::global; +use dom::bindings::js::{JSRef, Temporary, RootedReference}; +use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::event::{Event, KeyboardEventTypeId}; use dom::uievent::UIEvent; +use dom::window::Window; use servo_util::str::DOMString; +use std::cell::{RefCell, Cell}; #[jstraceable] #[must_root] pub struct KeyboardEvent { uievent: UIEvent, + key: RefCell<DOMString>, + code: RefCell<DOMString>, + location: Cell<u32>, + ctrl: Cell<bool>, + alt: Cell<bool>, + shift: Cell<bool>, + meta: Cell<bool>, + repeat: Cell<bool>, + is_composing: Cell<bool>, + char_code: Cell<Option<u32>>, + key_code: Cell<u32>, } impl KeyboardEventDerived for Event { @@ -26,14 +41,152 @@ impl KeyboardEventDerived for Event { } impl KeyboardEvent { - pub fn Constructor(_global: &GlobalRef, - _type_: DOMString, - _init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Temporary<KeyboardEvent>> { - fail!() + fn new_inherited() -> KeyboardEvent { + KeyboardEvent { + uievent: UIEvent::new_inherited(KeyboardEventTypeId), + key: RefCell::new("".to_string()), + code: RefCell::new("".to_string()), + location: Cell::new(0), + ctrl: Cell::new(false), + alt: Cell::new(false), + shift: Cell::new(false), + meta: Cell::new(false), + repeat: Cell::new(false), + is_composing: Cell::new(false), + char_code: Cell::new(None), + key_code: Cell::new(0), + } + } + + fn new_uninitialized(window: JSRef<Window>) -> Temporary<KeyboardEvent> { + reflect_dom_object(box KeyboardEvent::new_inherited(), + &global::Window(window), + KeyboardEventBinding::Wrap) + } + + pub fn new(window: JSRef<Window>, + type_: DOMString, + canBubble: bool, + cancelable: bool, + view: Option<JSRef<Window>>, + _detail: i32, + key: DOMString, + code: DOMString, + location: u32, + repeat: bool, + isComposing: bool, + ctrlKey: bool, + altKey: bool, + shiftKey: bool, + metaKey: bool, + char_code: Option<u32>, + key_code: u32) -> Temporary<KeyboardEvent> { + let ev = KeyboardEvent::new_uninitialized(window).root(); + ev.deref().InitKeyboardEvent(type_, canBubble, cancelable, view, key, location, + "".to_string(), repeat, "".to_string()); + *ev.code.borrow_mut() = code; + ev.ctrl.set(ctrlKey); + ev.alt.set(altKey); + ev.shift.set(shiftKey); + ev.meta.set(metaKey); + ev.char_code.set(char_code); + ev.key_code.set(key_code); + ev.is_composing.set(isComposing); + Temporary::from_rooted(*ev) + } + + pub fn Constructor(global: &GlobalRef, + type_: DOMString, + init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Temporary<KeyboardEvent>> { + let event = KeyboardEvent::new(global.as_window(), type_, + init.parent.parent.parent.bubbles, + init.parent.parent.parent.cancelable, + init.parent.parent.view.root_ref(), + init.parent.parent.detail, + init.key.clone(), init.code.clone(), init.location, + init.repeat, init.isComposing, init.parent.ctrlKey, + init.parent.altKey, init.parent.shiftKey, init.parent.metaKey, + None, 0); + Ok(event) } } impl<'a> KeyboardEventMethods for JSRef<'a, KeyboardEvent> { + fn InitKeyboardEvent(self, + typeArg: DOMString, + canBubbleArg: bool, + cancelableArg: bool, + viewArg: Option<JSRef<Window>>, + keyArg: DOMString, + locationArg: u32, + _modifiersListArg: DOMString, + repeat: bool, + _locale: DOMString) { + let uievent: JSRef<UIEvent> = UIEventCast::from_ref(self); + uievent.InitUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, 0); + *self.key.borrow_mut() = keyArg; + self.location.set(locationArg); + self.repeat.set(repeat); + } + + fn Key(self) -> DOMString { + self.key.borrow().clone() + } + + fn Code(self) -> DOMString { + self.code.borrow().clone() + } + + fn Location(self) -> u32 { + self.location.get() + } + + fn CtrlKey(self) -> bool { + self.ctrl.get() + } + + fn ShiftKey(self) -> bool { + self.shift.get() + } + + fn AltKey(self) -> bool { + self.alt.get() + } + + fn MetaKey(self) -> bool { + self.meta.get() + } + + fn Repeat(self) -> bool { + self.repeat.get() + } + + fn IsComposing(self) -> bool { + self.is_composing.get() + } + + fn GetModifierState(self, keyArg: DOMString) -> bool { + match keyArg.as_slice() { + "Ctrl" => self.CtrlKey(), + "Alt" => self.AltKey(), + "Shift" => self.ShiftKey(), + "Meta" => self.MetaKey(), + "AltGraph" | "CapsLock" | "NumLock" | "ScrollLock" => false, //FIXME + _ => false, + } + } + + fn CharCode(self) -> u32 { + self.char_code.get().unwrap_or(0) + } + + fn KeyCode(self) -> u32 { + self.key_code.get() + } + + fn Which(self) -> u32 { + self.char_code.get().unwrap_or(self.KeyCode()) + } } impl Reflectable for KeyboardEvent { diff --git a/components/script/dom/mouseevent.rs b/components/script/dom/mouseevent.rs index c65a53217b7..b617505c41e 100644 --- a/components/script/dom/mouseevent.rs +++ b/components/script/dom/mouseevent.rs @@ -175,7 +175,6 @@ impl<'a> MouseEventMethods for JSRef<'a, MouseEvent> { } } - impl Reflectable for MouseEvent { fn reflector<'a>(&'a self) -> &'a Reflector { self.uievent.reflector() diff --git a/components/script/dom/webidls/KeyboardEvent.webidl b/components/script/dom/webidls/KeyboardEvent.webidl index 589e39393b5..d0a185a14de 100644 --- a/components/script/dom/webidls/KeyboardEvent.webidl +++ b/components/script/dom/webidls/KeyboardEvent.webidl @@ -15,18 +15,33 @@ interface KeyboardEvent : UIEvent { const unsigned long DOM_KEY_LOCATION_LEFT = 0x01; const unsigned long DOM_KEY_LOCATION_RIGHT = 0x02; const unsigned long DOM_KEY_LOCATION_NUMPAD = 0x03; - //readonly attribute DOMString key; - //readonly attribute DOMString code; - //readonly attribute unsigned long location; - //readonly attribute boolean ctrlKey; - //readonly attribute boolean shiftKey; - //readonly attribute boolean altKey; - //readonly attribute boolean metaKey; - //readonly attribute boolean repeat; - //readonly attribute boolean isComposing; - //boolean getModifierState (DOMString keyArg); + readonly attribute DOMString key; + readonly attribute DOMString code; + readonly attribute unsigned long location; + readonly attribute boolean ctrlKey; + readonly attribute boolean shiftKey; + readonly attribute boolean altKey; + readonly attribute boolean metaKey; + readonly attribute boolean repeat; + readonly attribute boolean isComposing; + boolean getModifierState (DOMString keyArg); }; +// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#idl-interface-KeyboardEvent-initializers +partial interface KeyboardEvent { + // Originally introduced (and deprecated) in DOM Level 3 + void initKeyboardEvent (DOMString typeArg, boolean bubblesArg, boolean cancelableArg, Window? viewArg, DOMString keyArg, unsigned long locationArg, DOMString modifiersListArg, boolean repeat, DOMString locale); +}; + +// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#KeyboardEvent-supplemental-interface +partial interface KeyboardEvent { + // The following support legacy user agents + readonly attribute unsigned long charCode; + readonly attribute unsigned long keyCode; + readonly attribute unsigned long which; +}; + +// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#interface-KeyboardEvent dictionary KeyboardEventInit : SharedKeyboardAndMouseEventInit { DOMString key = ""; DOMString code = ""; @@ -34,3 +49,10 @@ dictionary KeyboardEventInit : SharedKeyboardAndMouseEventInit { boolean repeat = false; boolean isComposing = false; }; + +// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#events-KeyboardEventInit-supplemental +/*partial dictionary KeyboardEventInit { + unsigned long charCode = 0; + unsigned long keyCode = 0; + unsigned long which = 0; +};*/ |