From 1c7f2a9f09da9111b47aef628662a625cb69d53e Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 20 Jun 2017 11:57:41 -0700 Subject: [PATCH] Account for left-to-right rather than right-to-left precedence of classes in selector maps. MozReview-Commit-ID: 8qIl4k3RxaC --- components/style/selector_map.rs | 17 ++++++++--------- tests/unit/style/stylist.rs | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/components/style/selector_map.rs b/components/style/selector_map.rs index 48d25231b25d..77052990edc1 100644 --- a/components/style/selector_map.rs +++ b/components/style/selector_map.rs @@ -369,15 +369,12 @@ impl SelectorMap { } } -/// 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(mut iter: SelectorIter, +fn find_from_left(mut iter: SelectorIter, mut f: F) -> Option where F: FnMut(&Component) -> Option, @@ -388,6 +385,8 @@ fn find_from_right(mut iter: SelectorIter, } } + // 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) { @@ -403,7 +402,7 @@ fn find_from_right(mut iter: SelectorIter, #[inline(always)] pub fn get_id_name(iter: SelectorIter) -> Option { - 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 { @@ -417,7 +416,7 @@ pub fn get_id_name(iter: SelectorIter) #[inline(always)] pub fn get_class_name(iter: SelectorIter) -> Option { - 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 { @@ -431,7 +430,7 @@ pub fn get_class_name(iter: SelectorIter) #[inline(always)] pub fn get_local_name(iter: SelectorIter) -> Option> { - find_from_right(iter, |ss| { + find_from_left(iter, |ss| { if let Component::LocalName(ref n) = *ss { return Some(LocalNameSelector { name: n.name.clone(), diff --git a/tests/unit/style/stylist.rs b/tests/unit/style/stylist.rs index 1a28e5ea772b..0f371967efb6 100644 --- a/tests/unit/style/stylist.rs +++ b/tests/unit/style/stylist.rs @@ -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); } @@ -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]