Implementation of pseudo class 'first-child'.

This commit is contained in:
Jaeman Park 2013-10-25 15:01:01 +09:00 committed by Simon Sapin
parent 572ba98ac9
commit 6dba191efe
4 changed files with 64 additions and 25 deletions

View file

@ -164,6 +164,10 @@ impl<View> TreeNodeRef<Node<View>> for AbstractNode<View> {
_ => false
}
}
fn is_root(&self) -> bool {
self.parent_node().is_none()
}
}
impl<View> TreeNodeRefAsElement<Node<View>, Element> for AbstractNode<View> {

View file

@ -150,13 +150,10 @@ fn matches_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLik
matches_compound_selector::<N, T, E>(&selector.compound_selectors, element)
}
fn matches_compound_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLike>(
selector: &CompoundSelector, element: &T) -> bool {
if do element.with_imm_element_like |element: &E| {
!do selector.simple_selectors.iter().all |simple_selector| {
if !do selector.simple_selectors.iter().all |simple_selector| {
matches_simple_selector(simple_selector, element)
}
} {
return false
}
@ -193,25 +190,37 @@ fn matches_compound_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: E
}
#[inline]
fn matches_simple_selector<E: ElementLike>(selector: &SimpleSelector, element: &E) -> bool {
fn matches_simple_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLike>(
selector: &SimpleSelector, element: &T) -> bool {
static WHITESPACE: &'static [char] = &'static [' ', '\t', '\n', '\r', '\x0C'];
match *selector {
// TODO: case-sensitivity depends on the document type
// TODO: intern element names
LocalNameSelector(ref name)
=> element.get_local_name().eq_ignore_ascii_case(name.as_slice()),
LocalNameSelector(ref name) => {
do element.with_imm_element_like |element: &E| {
element.get_local_name().eq_ignore_ascii_case(name.as_slice())
}
}
NamespaceSelector(_) => false, // TODO, when the DOM supports namespaces on elements.
// TODO: case-sensitivity depends on the document type and quirks mode
// TODO: cache and intern IDs on elements.
IDSelector(ref id) => element.get_attr("id") == Some(id.as_slice()),
IDSelector(ref id) => {
do element.with_imm_element_like |element: &E| {
element.get_attr("id") == Some(id.as_slice())
}
}
// TODO: cache and intern classe names on elements.
ClassSelector(ref class) => match element.get_attr("class") {
ClassSelector(ref class) => {
do element.with_imm_element_like |element: &E| {
match element.get_attr("class") {
None => false,
// TODO: case-sensitivity depends on the document type and quirks mode
Some(ref class_attr)
=> class_attr.split_iter(WHITESPACE).any(|c| c == class.as_slice()),
},
}
}
}
AttrExists(ref attr) => match_attribute(attr, element, |_| true),
AttrEqual(ref attr, ref value) => match_attribute(attr, element, |v| v == value.as_slice()),
@ -232,15 +241,35 @@ fn matches_simple_selector<E: ElementLike>(selector: &SimpleSelector, element: &
attr_value.ends_with(value.as_slice())
},
FirstChild => matches_first_child(element),
Negation(ref negated) => {
!negated.iter().all(|s| matches_simple_selector(s, element))
},
}
}
#[inline]
fn matches_first_child<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLike>(
element: &T) -> bool {
let mut node = element.clone();
loop {
match node.node().prev_sibling() {
Some(prev_sibling) => {
node = prev_sibling;
if node.is_element() {
return false
}
}
None => return !element.is_root(),
}
}
}
#[inline]
fn match_attribute<E: ElementLike>(attr: &AttrSelector, element: &E, f: &fn(&str)-> bool) -> bool {
fn match_attribute<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: ElementLike>(
attr: &AttrSelector, element: &T, f: &fn(&str)-> bool) -> bool {
do element.with_imm_element_like |element: &E| {
match attr.namespace {
Some(_) => false, // TODO, when the DOM supports namespaces on attributes
None => match element.get_attr(attr.name) {
@ -249,3 +278,4 @@ fn match_attribute<E: ElementLike>(attr: &AttrSelector, element: &E, f: &fn(&str
}
}
}
}

View file

@ -58,6 +58,7 @@ pub enum SimpleSelector {
AttrSuffixMatch(AttrSelector, ~str), // [foo$=bar]
// Pseudo-classes
FirstChild,
// Empty,
// Root,
// Lang(~str),
@ -180,6 +181,7 @@ fn compute_specificity(mut selector: &CompoundSelector,
&ClassSelector(*)
| &AttrExists(*) | &AttrEqual(*) | &AttrIncludes(*) | &AttrDashMatch(*)
| &AttrPrefixMatch(*) | &AttrSubstringMatch(*) | &AttrSuffixMatch(*)
| &FirstChild
// | &Empty | &Root | &Lang(*) | &NthChild(*)
=> specificity.class_like_selectors += 1,
&NamespaceSelector(*) => (),
@ -272,7 +274,7 @@ fn parse_one_simple_selector(iter: &mut Iter, namespaces: &NamespaceMap, inside_
},
_ => fail!("Implementation error, this should not happen."),
},
Some(&Delim(':')) => {
Some(&Colon) => {
iter.next();
match iter.next() {
Some(Ident(name)) => match parse_simple_pseudo_class(name) {
@ -292,7 +294,7 @@ fn parse_one_simple_selector(iter: &mut Iter, namespaces: &NamespaceMap, inside_
None => None,
Some(simple_selector) => Some(Some(Left(simple_selector))),
},
Some(Delim(':')) => {
Some(Colon) => {
match iter.next() {
Some(Ident(name)) => match parse_pseudo_element(name) {
Some(pseudo_element) => Some(Some(Right(pseudo_element))),
@ -414,6 +416,7 @@ fn parse_attribute_selector(content: ~[ComponentValue], namespaces: &NamespaceMa
fn parse_simple_pseudo_class(name: &str) -> Option<SimpleSelector> {
match name.to_ascii_lower().as_slice() {
"first-child" => Some(FirstChild),
// "root" => Some(Root),
// "empty" => Some(Empty),
_ => None

View file

@ -239,6 +239,8 @@ pub trait TreeNodeRef<Node>: Clone {
}
fn is_element(&self) -> bool;
fn is_root(&self) -> bool;
}
pub trait TreeNodeRefAsElement<Node, E: ElementLike>: TreeNodeRef<Node> {