Skip to content

Commit

Permalink
Account for left-to-right rather than right-to-left precedence of cla…
Browse files Browse the repository at this point in the history
…sses in selector maps.

MozReview-Commit-ID: 8qIl4k3RxaC
  • Loading branch information
bholley committed Jun 20, 2017
1 parent 7f4c846 commit 1c7f2a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 8 additions & 9 deletions components/style/selector_map.rs
Expand Up @@ -369,15 +369,12 @@ impl<T: SelectorMapEntry> SelectorMap<T> {
}
}

/// Searches the selector from right to left, beginning to the left of the
/// ::pseudo-element (if any), and ending at the first combinator.
/// Searches a compound selector from left to right. If the compound selector
/// is a pseudo-element, it's ignored.
///
/// The first non-None value returned from |f| is returned.
///
/// Effectively, pseudo-elements are ignored, given only state pseudo-classes
/// may appear before them.
#[inline(always)]
fn find_from_right<F, R>(mut iter: SelectorIter<SelectorImpl>,
fn find_from_left<F, R>(mut iter: SelectorIter<SelectorImpl>,
mut f: F)
-> Option<R>
where F: FnMut(&Component<SelectorImpl>) -> Option<R>,
Expand All @@ -388,6 +385,8 @@ fn find_from_right<F, R>(mut iter: SelectorIter<SelectorImpl>,
}
}

// Effectively, pseudo-elements are ignored, given only state pseudo-classes
// may appear before them.
if iter.next_sequence() == Some(Combinator::PseudoElement) {
for ss in &mut iter {
if let Some(r) = f(ss) {
Expand All @@ -403,7 +402,7 @@ fn find_from_right<F, R>(mut iter: SelectorIter<SelectorImpl>,
#[inline(always)]
pub fn get_id_name(iter: SelectorIter<SelectorImpl>)
-> Option<Atom> {
find_from_right(iter, |ss| {
find_from_left(iter, |ss| {
// TODO(pradeep): Implement case-sensitivity based on the
// document type and quirks mode.
if let Component::ID(ref id) = *ss {
Expand All @@ -417,7 +416,7 @@ pub fn get_id_name(iter: SelectorIter<SelectorImpl>)
#[inline(always)]
pub fn get_class_name(iter: SelectorIter<SelectorImpl>)
-> Option<Atom> {
find_from_right(iter, |ss| {
find_from_left(iter, |ss| {
// TODO(pradeep): Implement case-sensitivity based on the
// document type and quirks mode.
if let Component::Class(ref class) = *ss {
Expand All @@ -431,7 +430,7 @@ pub fn get_class_name(iter: SelectorIter<SelectorImpl>)
#[inline(always)]
pub fn get_local_name(iter: SelectorIter<SelectorImpl>)
-> Option<LocalNameSelector<SelectorImpl>> {
find_from_right(iter, |ss| {
find_from_left(iter, |ss| {
if let Component::LocalName(ref n) = *ss {
return Some(LocalNameSelector {
name: n.name.clone(),
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/style/stylist.rs
Expand Up @@ -194,7 +194,7 @@ fn test_get_id_name() {
#[test]
fn test_get_class_name() {
let (rules_list, _) = get_mock_rules(&[".intro.foo", "#top"]);
assert_eq!(selector_map::get_class_name(rules_list[0][0].selector.iter()), Some(Atom::from("foo")));
assert_eq!(selector_map::get_class_name(rules_list[0][0].selector.iter()), Some(Atom::from("intro")));
assert_eq!(selector_map::get_class_name(rules_list[1][0].selector.iter()), None);
}

Expand All @@ -220,8 +220,8 @@ fn test_insert() {
selector_map.insert(rules_list[1][0].clone(), QuirksMode::NoQuirks);
assert_eq!(1, selector_map.id_hash.get(&Atom::from("top"), QuirksMode::NoQuirks).unwrap()[0].source_order);
selector_map.insert(rules_list[0][0].clone(), QuirksMode::NoQuirks);
assert_eq!(0, selector_map.class_hash.get(&Atom::from("foo"), QuirksMode::NoQuirks).unwrap()[0].source_order);
assert!(selector_map.class_hash.get(&Atom::from("intro"), QuirksMode::NoQuirks).is_none());
assert_eq!(0, selector_map.class_hash.get(&Atom::from("intro"), QuirksMode::NoQuirks).unwrap()[0].source_order);
assert!(selector_map.class_hash.get(&Atom::from("foo"), QuirksMode::NoQuirks).is_none());
}

#[test]
Expand Down

0 comments on commit 1c7f2a9

Please sign in to comment.