Implement the :focus pseudo-class selector

Fixes #5460. This supports for simple focusable elements that are their own
DOM anchors, like text `input` fields.
This commit is contained in:
Matt Brubeck 2015-03-30 10:39:13 -07:00
parent ad6c511a5e
commit 791fa3757d
10 changed files with 106 additions and 9 deletions

View file

@ -581,6 +581,14 @@ impl<'le> TElement<'le> for LayoutElement<'le> {
} }
} }
#[inline]
fn get_focus_state(self) -> bool {
unsafe {
let node: &Node = NodeCast::from_actual(self.element);
node.get_focus_state_for_layout()
}
}
#[inline] #[inline]
fn get_id(self) -> Option<Atom> { fn get_id(self) -> Option<Atom> {
unsafe { unsafe {

View file

@ -454,7 +454,20 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
/// transaction, or none if no elements requested it. /// transaction, or none if no elements requested it.
fn commit_focus_transaction(self) { fn commit_focus_transaction(self) {
//TODO: dispatch blur, focus, focusout, and focusin events //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()); 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. /// Handles any updates when the document's title has changed.

View file

@ -1461,6 +1461,13 @@ impl<'a> style::node::TElement<'a> for JSRef<'a, Element> {
let node: JSRef<Node> = NodeCast::from_ref(self); let node: JSRef<Node> = NodeCast::from_ref(self);
node.get_hover_state() 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> { fn get_id(self) -> Option<Atom> {
self.get_attribute(ns!(""), &atom!("id")).map(|attr| { self.get_attribute(ns!(""), &atom!("id")).map(|attr| {
let attr = attr.root(); let attr = attr.root();

View file

@ -152,6 +152,8 @@ bitflags! {
#[doc = "Specifies whether or not there is an authentic click in progress on \ #[doc = "Specifies whether or not there is an authentic click in progress on \
this element."] this element."]
const CLICK_IN_PROGRESS = 0x100, 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 get_hover_state(self) -> bool;
fn set_hover_state(self, state: 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 get_disabled_state(self) -> bool;
fn set_disabled_state(self, state: 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) 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 { fn get_disabled_state(self) -> bool {
self.get_flag(IN_DISABLED_STATE) self.get_flag(IN_DISABLED_STATE)
} }
@ -1036,6 +1049,8 @@ pub trait RawLayoutNodeHelpers {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_hover_state_for_layout(&self) -> bool; unsafe fn get_hover_state_for_layout(&self) -> bool;
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_focus_state_for_layout(&self) -> bool;
#[allow(unsafe_code)]
unsafe fn get_disabled_state_for_layout(&self) -> bool; unsafe fn get_disabled_state_for_layout(&self) -> bool;
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_enabled_state_for_layout(&self) -> bool; unsafe fn get_enabled_state_for_layout(&self) -> bool;
@ -1050,6 +1065,11 @@ impl RawLayoutNodeHelpers for Node {
} }
#[inline] #[inline]
#[allow(unsafe_code)] #[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 { unsafe fn get_disabled_state_for_layout(&self) -> bool {
self.flags.get().contains(IN_DISABLED_STATE) self.flags.get().contains(IN_DISABLED_STATE)
} }

View file

@ -831,7 +831,7 @@ dependencies = [
[[package]] [[package]]
name = "selectors" name = "selectors"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#12d3ce84a12ded4cf1def63651ccab06e1cfa80e" source = "git+https://github.com/servo/rust-selectors#0d7d846090c21d71ebb1bc17921806933a38f52b"
dependencies = [ dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)",

29
ports/cef/Cargo.lock generated
View file

@ -104,6 +104,7 @@ dependencies = [
"libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net 0.0.1",
"net_traits 0.0.1",
"png 0.1.0 (git+https://github.com/servo/rust-png)", "png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1", "profile 0.0.1",
"script_traits 0.0.1", "script_traits 0.0.1",
@ -327,7 +328,7 @@ dependencies = [
"layers 0.1.0 (git+https://github.com/servo/rust-layers)", "layers 0.1.0 (git+https://github.com/servo/rust-layers)",
"libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net_traits 0.0.1",
"plugins 0.0.1", "plugins 0.0.1",
"png 0.1.0 (git+https://github.com/servo/rust-png)", "png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1", "profile 0.0.1",
@ -533,7 +534,7 @@ dependencies = [
"layout_traits 0.0.1", "layout_traits 0.0.1",
"libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net_traits 0.0.1",
"plugins 0.0.1", "plugins 0.0.1",
"png 0.1.0 (git+https://github.com/servo/rust-png)", "png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1", "profile 0.0.1",
@ -554,7 +555,7 @@ version = "0.0.1"
dependencies = [ dependencies = [
"gfx 0.0.1", "gfx 0.0.1",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net_traits 0.0.1",
"profile 0.0.1", "profile 0.0.1",
"script_traits 0.0.1", "script_traits 0.0.1",
"url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
@ -645,18 +646,31 @@ dependencies = [
"flate2 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"geom 0.1.0 (git+https://github.com/servo/rust-geom)", "geom 0.1.0 (git+https://github.com/servo/rust-geom)",
"hyper 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"net_traits 0.0.1",
"openssl 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"png 0.1.0 (git+https://github.com/servo/rust-png)", "png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1", "profile 0.0.1",
"regex 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
"regex_macros 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "regex_macros 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"stb_image 0.1.0 (git+https://github.com/servo/rust-stb-image)",
"time 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
"url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
"util 0.0.1", "util 0.0.1",
] ]
[[package]]
name = "net_traits"
version = "0.0.1"
dependencies = [
"geom 0.1.0 (git+https://github.com/servo/rust-geom)",
"hyper 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1",
"stb_image 0.1.0 (git+https://github.com/servo/rust-stb-image)",
"url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
"util 0.0.1",
]
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.5.1" version = "0.5.1"
@ -791,7 +805,7 @@ dependencies = [
"js 0.1.0 (git+https://github.com/servo/rust-mozjs)", "js 0.1.0 (git+https://github.com/servo/rust-mozjs)",
"libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net_traits 0.0.1",
"plugins 0.0.1", "plugins 0.0.1",
"profile 0.0.1", "profile 0.0.1",
"rustc-serialize 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
@ -814,7 +828,7 @@ dependencies = [
"geom 0.1.0 (git+https://github.com/servo/rust-geom)", "geom 0.1.0 (git+https://github.com/servo/rust-geom)",
"libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net_traits 0.0.1",
"url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
"util 0.0.1", "util 0.0.1",
] ]
@ -822,7 +836,7 @@ dependencies = [
[[package]] [[package]]
name = "selectors" name = "selectors"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#12d3ce84a12ded4cf1def63651ccab06e1cfa80e" source = "git+https://github.com/servo/rust-selectors#0d7d846090c21d71ebb1bc17921806933a38f52b"
dependencies = [ dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)",
@ -843,6 +857,7 @@ dependencies = [
"layout 0.0.1", "layout 0.0.1",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1", "net 0.0.1",
"net_traits 0.0.1",
"png 0.1.0 (git+https://github.com/servo/rust-png)", "png 0.1.0 (git+https://github.com/servo/rust-png)",
"profile 0.0.1", "profile 0.0.1",
"script 0.0.1", "script 0.0.1",

2
ports/gonk/Cargo.lock generated
View file

@ -761,7 +761,7 @@ dependencies = [
[[package]] [[package]]
name = "selectors" name = "selectors"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#12d3ce84a12ded4cf1def63651ccab06e1cfa80e" source = "git+https://github.com/servo/rust-selectors#0d7d846090c21d71ebb1bc17921806933a38f52b"
dependencies = [ dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)",

View file

@ -102,6 +102,7 @@ flaky_cpu == append_style_a.html append_style_b.html
== floated_generated_content_a.html floated_generated_content_b.html == floated_generated_content_a.html floated_generated_content_b.html
== floated_list_item_a.html floated_list_item_ref.html == floated_list_item_a.html floated_list_item_ref.html
== floated_table_with_margin_a.html floated_table_with_margin_ref.html == floated_table_with_margin_a.html floated_table_with_margin_ref.html
== focus_selector.html focus_selector_ref.html
== font_advance.html font_advance_ref.html == font_advance.html font_advance_ref.html
== font_size.html font_size_ref.html == font_size.html font_size_ref.html
== font_style.html font_style_ref.html == font_style.html font_style_ref.html

View file

@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<style>
input:focus {
outline: 2px solid orange;
}
</style>
</head>
<body>
<input id="a">
<input id="b">
<script>
document.getElementById("a").focus();
</script>
</body>
</html>

View file

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<style>
#a {
outline: 2px solid orange;
}
</style>
</head>
<body>
<input id="a">
<input id="b">
</body>
</html>