From 7da356cd05813d8da8e6070aab7d95a412046df5 Mon Sep 17 00:00:00 2001 From: Maciej Skrzypkowski Date: Thu, 19 Mar 2015 23:00:13 +0100 Subject: [PATCH] Added document.activeElement attribute. --- components/script/dom/document.rs | 13 +++++++++++++ components/script/dom/element.rs | 10 ++++++++++ components/script/dom/webidls/Document.webidl | 1 + .../content/test_document_activeElement.html | 19 +++++++++++++++++++ .../wpt/metadata/html/dom/interfaces.html.ini | 6 ------ .../disabledElement.html.ini | 3 --- 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tests/content/test_document_activeElement.html diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 8a7d87ba6b6..fe1c46a031a 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -834,6 +834,19 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { self.url().serialize() } + // https://html.spec.whatwg.org/multipage/interaction.html#dom-document-activeelement + fn GetActiveElement(self) -> Option> { + // TODO: Step 2. + + match self.get_focused_element() { + Some(element) => Some(element), // Step 3. and 4. + None => match self.GetBody() { // Step 5. + Some(body) => Some(ElementCast::from_temporary(body)), + None => self.GetDocumentElement(), + } + } + } + // http://dom.spec.whatwg.org/#dom-document-documenturi fn DocumentURI(self) -> DOMString { self.URL() diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 3d7ddff05a5..b6c0d689a1c 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -435,6 +435,7 @@ pub trait ElementHelpers<'a> { fn get_inline_style_declaration(self, property: &Atom) -> Option; fn get_important_inline_style_declaration(self, property: &Atom) -> Option; fn serialize(self, traversal_scope: TraversalScope) -> Fallible; + fn get_root_element(self) -> Option>; } impl<'a> ElementHelpers<'a> for JSRef<'a, Element> { @@ -593,6 +594,15 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> { Err(_) => panic!("Cannot serialize element"), } } + + // https://html.spec.whatwg.org/multipage/infrastructure.html#root-element + fn get_root_element(self) -> Option> { + let node: JSRef = NodeCast::from_ref(self); + match node.ancestors().last().map(ElementCast::to_ref) { + Some(n) => n.map(Temporary::from_rooted), + None => Some(self).map(Temporary::from_rooted), + } + } } pub trait AttributeHandlers { diff --git a/components/script/dom/webidls/Document.webidl b/components/script/dom/webidls/Document.webidl index a591057fa1d..984f79eb528 100644 --- a/components/script/dom/webidls/Document.webidl +++ b/components/script/dom/webidls/Document.webidl @@ -13,6 +13,7 @@ interface Document : Node { readonly attribute DOMImplementation implementation; readonly attribute DOMString URL; + readonly attribute Element? activeElement; readonly attribute DOMString documentURI; readonly attribute DOMString compatMode; readonly attribute DOMString characterSet; diff --git a/tests/content/test_document_activeElement.html b/tests/content/test_document_activeElement.html new file mode 100644 index 00000000000..8cce6cfa210 --- /dev/null +++ b/tests/content/test_document_activeElement.html @@ -0,0 +1,19 @@ + + + + + + + + + + + diff --git a/tests/wpt/metadata/html/dom/interfaces.html.ini b/tests/wpt/metadata/html/dom/interfaces.html.ini index 8f47201cb86..5977047f9e5 100644 --- a/tests/wpt/metadata/html/dom/interfaces.html.ini +++ b/tests/wpt/metadata/html/dom/interfaces.html.ini @@ -30,9 +30,6 @@ [Document interface: operation writeln(DOMString)] expected: FAIL - [Document interface: attribute activeElement] - expected: FAIL - [Document interface: operation hasFocus()] expected: FAIL @@ -1125,9 +1122,6 @@ [Document interface: document.implementation.createDocument(null, "", null) must inherit property "defaultView" with the proper type (59)] expected: FAIL - [Document interface: document.implementation.createDocument(null, "", null) must inherit property "activeElement" with the proper type (60)] - expected: FAIL - [Document interface: document.implementation.createDocument(null, "", null) must inherit property "hasFocus" with the proper type (61)] expected: FAIL diff --git a/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini b/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini index badb1eabe69..734ef783331 100644 --- a/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini +++ b/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini @@ -1,8 +1,5 @@ [disabledElement.html] type: testharness - [The body element must be the active element if no element is focused] - expected: FAIL - [A disabled