From 84bc17e7ad99d11ff416f2126acb2031b680dc19 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 4 Oct 2014 10:31:47 -0400 Subject: Implement document focus context and hook it up to click events. --- components/script/dom/document.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'components/script/dom/document.rs') diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 793bfad38b2..b5a39073dad 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -96,6 +96,10 @@ pub struct Document { anchors: MutNullableJS, applets: MutNullableJS, ready_state: Cell, + /// The element that has most recently requested focus for itself. + possibly_focused: MutNullableJS, + /// The element that currently has the document focus context. + focused: MutNullableJS, } impl DocumentDerived for EventTarget { @@ -178,6 +182,10 @@ pub trait DocumentHelpers<'a> { fn load_anchor_href(self, href: DOMString); fn find_fragment_node(self, fragid: DOMString) -> Option>; fn set_ready_state(self, state: DocumentReadyState); + fn get_focused_element(self) -> Option>; + fn begin_focus_transaction(self); + fn request_focus(self, elem: JSRef); + fn commit_focus_transaction(self); } impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> { @@ -327,6 +335,30 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> { let target: JSRef = EventTargetCast::from_ref(self); let _ = target.DispatchEvent(*event); } + + /// Return the element that currently has focus. + // https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3-Events.html#events-focusevent-doc-focus + fn get_focused_element(self) -> Option> { + self.focused.get() + } + + /// Initiate a new round of checking for elements requesting focus. The last element to call + /// `request_focus` before `commit_focus_transaction` is called will receive focus. + fn begin_focus_transaction(self) { + self.possibly_focused.clear(); + } + + /// Request that the given element receive focus once the current transaction is complete. + fn request_focus(self, elem: JSRef) { + self.possibly_focused.assign(Some(elem)) + } + + /// Reassign the focus context to the element that last requested focus during this + /// transaction, or none if no elements requested it. + fn commit_focus_transaction(self) { + //TODO: dispatch blur, focus, focusout, and focusin events + self.focused.assign(self.possibly_focused.get()); + } } #[deriving(PartialEq)] @@ -390,6 +422,8 @@ impl Document { anchors: Default::default(), applets: Default::default(), ready_state: Cell::new(ready_state), + possibly_focused: Default::default(), + focused: Default::default(), } } -- cgit v1.2.3