aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorMatt Brubeck <mbrubeck@limpet.net>2015-03-30 10:39:13 -0700
committerMatt Brubeck <mbrubeck@limpet.net>2015-04-04 10:57:11 -0700
commit791fa3757dd11b53c9ef7ef112f460675eddea17 (patch)
tree730bee60cc0d1a43e248f5a60d2c8616e22fdf48 /components/script/dom
parentad6c511a5eff974087df99f75522d38cb639aaef (diff)
downloadservo-791fa3757dd11b53c9ef7ef112f460675eddea17.tar.gz
servo-791fa3757dd11b53c9ef7ef112f460675eddea17.zip
Implement the :focus pseudo-class selector
Fixes #5460. This supports for simple focusable elements that are their own DOM anchors, like text `input` fields.
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/document.rs13
-rw-r--r--components/script/dom/element.rs7
-rw-r--r--components/script/dom/node.rs20
3 files changed, 40 insertions, 0 deletions
diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs
index 5cb13ac18e9..49eaef294d5 100644
--- a/components/script/dom/document.rs
+++ b/components/script/dom/document.rs
@@ -454,7 +454,20 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
/// transaction, or none if no elements requested it.
fn commit_focus_transaction(self) {
//TODO: dispatch blur, focus, focusout, and focusin events
+
+ if let Some(ref elem) = self.focused.get().root() {
+ let node: JSRef<Node> = NodeCast::from_ref(elem.r());
+ node.set_focus_state(false);
+ }
+
self.focused.assign(self.possibly_focused.get());
+
+ if let Some(ref elem) = self.focused.get().root() {
+ let node: JSRef<Node> = NodeCast::from_ref(elem.r());
+ node.set_focus_state(true);
+ }
+ // TODO: Update the focus state for all elements in the focus chain.
+ // https://html.spec.whatwg.org/multipage/interaction.html#focus-chain
}
/// Handles any updates when the document's title has changed.
diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs
index f69392a4126..498432cfd8a 100644
--- a/components/script/dom/element.rs
+++ b/components/script/dom/element.rs
@@ -1461,6 +1461,13 @@ impl<'a> style::node::TElement<'a> for JSRef<'a, Element> {
let node: JSRef<Node> = NodeCast::from_ref(self);
node.get_hover_state()
}
+ fn get_focus_state(self) -> bool {
+ // TODO: Also check whether the top-level browsing context has the system focus,
+ // and whether this element is a browsing context container.
+ // https://html.spec.whatwg.org/multipage/scripting.html#selector-focus
+ let node: JSRef<Node> = NodeCast::from_ref(self);
+ node.get_focus_state()
+ }
fn get_id(self) -> Option<Atom> {
self.get_attribute(ns!(""), &atom!("id")).map(|attr| {
let attr = attr.root();
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs
index a302308d33a..f9b81cad136 100644
--- a/components/script/dom/node.rs
+++ b/components/script/dom/node.rs
@@ -152,6 +152,8 @@ bitflags! {
#[doc = "Specifies whether or not there is an authentic click in progress on \
this element."]
const CLICK_IN_PROGRESS = 0x100,
+ #[doc = "Specifies whether this node has the focus."]
+ const IN_FOCUS_STATE = 0x200,
}
}
@@ -440,6 +442,9 @@ pub trait NodeHelpers<'a> {
fn get_hover_state(self) -> bool;
fn set_hover_state(self, state: bool);
+ fn get_focus_state(self) -> bool;
+ fn set_focus_state(self, state: bool);
+
fn get_disabled_state(self) -> bool;
fn set_disabled_state(self, state: bool);
@@ -618,6 +623,14 @@ impl<'a> NodeHelpers<'a> for JSRef<'a, Node> {
self.set_flag(IN_HOVER_STATE, state)
}
+ fn get_focus_state(self) -> bool {
+ self.get_flag(IN_FOCUS_STATE)
+ }
+
+ fn set_focus_state(self, state: bool) {
+ self.set_flag(IN_FOCUS_STATE, state)
+ }
+
fn get_disabled_state(self) -> bool {
self.get_flag(IN_DISABLED_STATE)
}
@@ -1036,6 +1049,8 @@ pub trait RawLayoutNodeHelpers {
#[allow(unsafe_code)]
unsafe fn get_hover_state_for_layout(&self) -> bool;
#[allow(unsafe_code)]
+ unsafe fn get_focus_state_for_layout(&self) -> bool;
+ #[allow(unsafe_code)]
unsafe fn get_disabled_state_for_layout(&self) -> bool;
#[allow(unsafe_code)]
unsafe fn get_enabled_state_for_layout(&self) -> bool;
@@ -1050,6 +1065,11 @@ impl RawLayoutNodeHelpers for Node {
}
#[inline]
#[allow(unsafe_code)]
+ unsafe fn get_focus_state_for_layout(&self) -> bool {
+ self.flags.get().contains(IN_FOCUS_STATE)
+ }
+ #[inline]
+ #[allow(unsafe_code)]
unsafe fn get_disabled_state_for_layout(&self) -> bool {
self.flags.get().contains(IN_DISABLED_STATE)
}