aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Brubeck <mbrubeck@limpet.net>2015-03-30 13:32:48 -0700
committerMatt Brubeck <mbrubeck@limpet.net>2015-04-04 10:57:11 -0700
commitad6c511a5eff974087df99f75522d38cb639aaef (patch)
tree0d450c8766a633e0752f4bf599e43e09e3336567
parent8758d7d11abd3a0e84e2af5c41911b767723513e (diff)
downloadservo-ad6c511a5eff974087df99f75522d38cb639aaef.tar.gz
servo-ad6c511a5eff974087df99f75522d38cb639aaef.zip
Basic element.focus and blur methods
Fixes #5462.
-rw-r--r--components/script/dom/htmlelement.rs32
-rw-r--r--components/script/dom/webidls/HTMLElement.webidl4
-rw-r--r--tests/content/test_document_activeElement.html5
-rw-r--r--tests/content/test_focus_blur.html25
-rw-r--r--tests/wpt/metadata/html/dom/interfaces.html.ini12
-rw-r--r--tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini7
6 files changed, 58 insertions, 27 deletions
diff --git a/components/script/dom/htmlelement.rs b/components/script/dom/htmlelement.rs
index a0d0adaf82b..55c0fc6fcec 100644
--- a/components/script/dom/htmlelement.rs
+++ b/components/script/dom/htmlelement.rs
@@ -10,21 +10,21 @@ use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFrameSetElementDerived};
-use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLInputElementCast};
+use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLInputElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLElementDerived, HTMLBodyElementDerived};
use dom::bindings::js::{JSRef, Temporary, MutNullableJS};
use dom::bindings::error::ErrorResult;
use dom::bindings::error::Error::Syntax;
use dom::bindings::utils::Reflectable;
use dom::cssstyledeclaration::{CSSStyleDeclaration, CSSModificationAccess};
-use dom::document::Document;
+use dom::document::{Document, DocumentHelpers};
use dom::domstringmap::DOMStringMap;
use dom::element::{Element, ElementTypeId, ActivationElementHelpers, AttributeHandlers};
use dom::eventtarget::{EventTarget, EventTargetHelpers, EventTargetTypeId};
use dom::htmlinputelement::HTMLInputElement;
use dom::htmlmediaelement::HTMLMediaElementTypeId;
use dom::htmltablecellelement::HTMLTableCellElementTypeId;
-use dom::node::{Node, NodeTypeId, window_from_node};
+use dom::node::{Node, NodeHelpers, NodeTypeId, document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods;
use dom::window::WindowHelpers;
@@ -135,6 +135,32 @@ impl<'a> HTMLElementMethods for JSRef<'a, HTMLElement> {
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=27430 ?
element.as_maybe_activatable().map(|a| a.synthetic_click_activation(false, false, false, false));
}
+
+ // https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
+ fn Focus(self) {
+ // TODO: Mark the element as locked for focus and run the focusing steps.
+ // https://html.spec.whatwg.org/multipage/interaction.html#focusing-steps
+ let element: JSRef<Element> = ElementCast::from_ref(self);
+ let document = document_from_node(self).root();
+ let document = document.r();
+ document.begin_focus_transaction();
+ document.request_focus(element);
+ document.commit_focus_transaction();
+ }
+
+ // https://html.spec.whatwg.org/multipage/interaction.html#dom-blur
+ fn Blur(self) {
+ // TODO: Run the unfocusing steps.
+ let node: JSRef<Node> = NodeCast::from_ref(self);
+ if !node.get_focus_state() {
+ return;
+ }
+ // https://html.spec.whatwg.org/multipage/interaction.html#unfocusing-steps
+ let document = document_from_node(self).root();
+ document.r().begin_focus_transaction();
+ // If `request_focus` is not called, focus will be set to None.
+ document.r().commit_focus_transaction();
+ }
}
// https://html.spec.whatwg.org/#attr-data-*
diff --git a/components/script/dom/webidls/HTMLElement.webidl b/components/script/dom/webidls/HTMLElement.webidl
index 90ef09d7a9f..fa3ecb6bbb2 100644
--- a/components/script/dom/webidls/HTMLElement.webidl
+++ b/components/script/dom/webidls/HTMLElement.webidl
@@ -25,8 +25,8 @@ interface HTMLElement : Element {
attribute boolean hidden;
void click();
// attribute long tabIndex;
- //void focus();
- //void blur();
+ void focus();
+ void blur();
// attribute DOMString accessKey;
//readonly attribute DOMString accessKeyLabel;
// attribute boolean draggable;
diff --git a/tests/content/test_document_activeElement.html b/tests/content/test_document_activeElement.html
index 8cce6cfa210..fee51456282 100644
--- a/tests/content/test_document_activeElement.html
+++ b/tests/content/test_document_activeElement.html
@@ -10,10 +10,9 @@
is_not(document.activeElement, null, "test_1.1, document.activeElement");
is(document.activeElement, document.body, "test_1.2, document.activeElement");
- //TODO: uncomment following lines when focus() method will be available
- //document.getElementById('foo').focus();
+ document.getElementById('foo').focus();
is_not(document.activeElement, null, "test_2.1, document.activeElement");
- //is(document.activeElement, document.getElementById("foo"), "test_2.2, document.activeElement");
+ is(document.activeElement, document.getElementById("foo"), "test_2.2, document.activeElement");
</script>
</body>
</html>
diff --git a/tests/content/test_focus_blur.html b/tests/content/test_focus_blur.html
new file mode 100644
index 00000000000..8c91f2a14a8
--- /dev/null
+++ b/tests/content/test_focus_blur.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="UTF-8">
+ <script src="harness.js"></script>
+</head>
+<body>
+ <input id="a">
+ <input id="b">
+ <script>
+ var a = document.getElementById("a");
+ var b = document.getElementById("b");
+
+ is(document.activeElement, document.body);
+ a.focus();
+ is(document.activeElement, a);
+ b.focus();
+ is(document.activeElement, b);
+ a.blur();
+ is(document.activeElement, b);
+ b.blur();
+ is(document.activeElement, document.body);
+ </script>
+</body>
+</html>
diff --git a/tests/wpt/metadata/html/dom/interfaces.html.ini b/tests/wpt/metadata/html/dom/interfaces.html.ini
index 3954b237f63..5a726d79ac0 100644
--- a/tests/wpt/metadata/html/dom/interfaces.html.ini
+++ b/tests/wpt/metadata/html/dom/interfaces.html.ini
@@ -1704,12 +1704,6 @@
[HTMLElement interface: attribute tabIndex]
expected: FAIL
- [HTMLElement interface: operation focus()]
- expected: FAIL
-
- [HTMLElement interface: operation blur()]
- expected: FAIL
-
[HTMLElement interface: attribute accessKey]
expected: FAIL
@@ -1959,12 +1953,6 @@
[HTMLElement interface: document.createElement("noscript") must inherit property "tabIndex" with the proper type (14)]
expected: FAIL
- [HTMLElement interface: document.createElement("noscript") must inherit property "focus" with the proper type (15)]
- expected: FAIL
-
- [HTMLElement interface: document.createElement("noscript") must inherit property "blur" with the proper type (16)]
- expected: FAIL
-
[HTMLElement interface: document.createElement("noscript") must inherit property "accessKey" with the proper type (17)]
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 734ef783331..f381b21bccb 100644
--- a/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini
+++ b/tests/wpt/metadata/html/semantics/disabled-elements/disabledElement.html.ini
@@ -20,10 +20,3 @@
[A disabled <input[type=radio\]> should not be focusable]
expected: FAIL
-
- [A disabled <a> should be focusable]
- expected: FAIL
-
- [A disabled <span> should be focusable]
- expected: FAIL
-