From f044659b7c989cddd2a35398c784fe596ac41041 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 21 Nov 2016 19:48:11 +0100 Subject: [PATCH] Use selectors::SelectorList --- components/script/dom/node.rs | 10 +++++----- components/style/stylesheets.rs | 23 ++++------------------- components/style/stylist.rs | 4 ++-- tests/unit/style/stylesheets.rs | 12 ++++++------ tests/unit/style/stylist.rs | 5 ++--- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 1e8baeccce2..8e8bf6edc9e 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -71,7 +71,7 @@ use script_layout_interface::{LayoutElementType, LayoutNodeType, TrustedNodeAddr use script_layout_interface::message::Msg; use script_traits::UntrustedNodeAddress; use selectors::matching::{MatchingReason, matches}; -use selectors::parser::Selector; +use selectors::parser::SelectorList; use servo_url::ServoUrl; use std::borrow::ToOwned; use std::cell::{Cell, UnsafeCell}; @@ -304,12 +304,12 @@ impl Node { } pub struct QuerySelectorIterator { - selectors: Vec>, + selectors: SelectorList, iterator: TreeIterator, } impl<'a> QuerySelectorIterator { - fn new(iter: TreeIterator, selectors: Vec>) + fn new(iter: TreeIterator, selectors: SelectorList) -> QuerySelectorIterator { QuerySelectorIterator { selectors: selectors, @@ -322,7 +322,7 @@ impl<'a> Iterator for QuerySelectorIterator { type Item = Root; fn next(&mut self) -> Option> { - let selectors = &self.selectors; + let selectors = &self.selectors.0; // TODO(cgaebel): Is it worth it to build a bloom filter here // (instead of passing `None`)? Probably. self.iterator.by_ref().filter_map(|node| { @@ -716,7 +716,7 @@ impl Node { let mut descendants = self.traverse_preorder(); // Skip the root of the tree. assert!(&*descendants.next().unwrap() == self); - Ok(QuerySelectorIterator::new(descendants, selectors.0)) + Ok(QuerySelectorIterator::new(descendants, selectors)) } } } diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index d7a6485ce89..7e8388591a8 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -17,7 +17,7 @@ use parking_lot::RwLock; use parser::{ParserContext, ParserContextExtraData, log_css_error}; use properties::{PropertyDeclarationBlock, parse_property_declaration_list}; use selector_parser::{SelectorImpl, SelectorParser}; -use selectors::parser::{Selector, SelectorList}; +use selectors::parser::SelectorList; use servo_url::ServoUrl; use std::cell::Cell; use std::fmt; @@ -196,30 +196,15 @@ impl ToCss for MediaRule { #[derive(Debug)] pub struct StyleRule { - pub selectors: Vec>, + pub selectors: SelectorList, pub block: Arc>, } -impl StyleRule { - /// Serialize the group of selectors for this rule. - /// - /// https://drafts.csswg.org/cssom/#serialize-a-group-of-selectors - pub fn selectors_to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - let mut iter = self.selectors.iter(); - try!(iter.next().unwrap().to_css(dest)); - for selector in iter { - try!(write!(dest, ", ")); - try!(selector.to_css(dest)); - } - Ok(()) - } -} - impl ToCss for StyleRule { // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { // Step 1 - try!(self.selectors_to_css(dest)); + try!(self.selectors.to_css(dest)); // Step 2 try!(dest.write_str(" { ")); // Step 3 @@ -582,7 +567,7 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> { fn parse_block(&mut self, prelude: SelectorList, input: &mut Parser) -> Result { Ok(CssRule::Style(Arc::new(RwLock::new(StyleRule { - selectors: prelude.0, + selectors: prelude, block: Arc::new(RwLock::new(parse_property_declaration_list(self.context, input))) })))) } diff --git a/components/style/stylist.rs b/components/style/stylist.rs index 62d469790bb..294b1b4cece 100644 --- a/components/style/stylist.rs +++ b/components/style/stylist.rs @@ -189,7 +189,7 @@ impl Stylist { match *rule { CssRule::Style(ref style_rule) => { let guard = style_rule.read(); - for selector in &guard.selectors { + for selector in &guard.selectors.0 { let map = if let Some(ref pseudo) = selector.pseudo_element { pseudos_map .entry(pseudo.clone()) @@ -208,7 +208,7 @@ impl Stylist { } *rules_source_order += 1; - for selector in &guard.selectors { + for selector in &guard.selectors.0 { state_deps.note_selector(&selector.complex_selector); if selector.affects_siblings() { sibling_affecting_selectors.push(selector.clone()); diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs index 8af9534ecc0..9bbde985a46 100644 --- a/tests/unit/style/stylesheets.rs +++ b/tests/unit/style/stylesheets.rs @@ -63,7 +63,7 @@ fn test_parse_stylesheet() { url: NsAtom::from("http://www.w3.org/1999/xhtml") }))), CssRule::Style(Arc::new(RwLock::new(StyleRule { - selectors: vec![ + selectors: SelectorList(vec![ Selector { complex_selector: Arc::new(ComplexSelector { compound_selector: vec![ @@ -89,7 +89,7 @@ fn test_parse_stylesheet() { pseudo_element: None, specificity: (0 << 20) + (1 << 10) + (1 << 0), }, - ], + ]), block: Arc::new(RwLock::new(PropertyDeclarationBlock { declarations: vec![ (PropertyDeclaration::Display(DeclaredValue::Value( @@ -102,7 +102,7 @@ fn test_parse_stylesheet() { })), }))), CssRule::Style(Arc::new(RwLock::new(StyleRule { - selectors: vec![ + selectors: SelectorList(vec![ Selector { complex_selector: Arc::new(ComplexSelector { compound_selector: vec![ @@ -137,7 +137,7 @@ fn test_parse_stylesheet() { pseudo_element: None, specificity: (0 << 20) + (0 << 10) + (1 << 0), }, - ], + ]), block: Arc::new(RwLock::new(PropertyDeclarationBlock { declarations: vec![ (PropertyDeclaration::Display(DeclaredValue::Value( @@ -148,7 +148,7 @@ fn test_parse_stylesheet() { })), }))), CssRule::Style(Arc::new(RwLock::new(StyleRule { - selectors: vec![ + selectors: SelectorList(vec![ Selector { complex_selector: Arc::new(ComplexSelector { compound_selector: vec![ @@ -172,7 +172,7 @@ fn test_parse_stylesheet() { pseudo_element: None, specificity: (1 << 20) + (1 << 10) + (0 << 0), }, - ], + ]), block: Arc::new(RwLock::new(PropertyDeclarationBlock { declarations: vec![ (PropertyDeclaration::BackgroundColor(DeclaredValue::Value( diff --git a/tests/unit/style/stylist.rs b/tests/unit/style/stylist.rs index 91072bba9e4..786ce7863f4 100644 --- a/tests/unit/style/stylist.rs +++ b/tests/unit/style/stylist.rs @@ -2,7 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use cssparser::Parser; use html5ever_atoms::LocalName; use parking_lot::RwLock; use selectors::parser::LocalName as LocalNameSelector; @@ -19,7 +18,7 @@ use style::thread_state; /// Each sublist of the result contains the Rules for one StyleRule. fn get_mock_rules(css_selectors: &[&str]) -> Vec> { css_selectors.iter().enumerate().map(|(i, selectors)| { - let selectors = SelectorParser::parse_author_origin_no_namespace(selectors).unwrap().0; + let selectors = SelectorParser::parse_author_origin_no_namespace(selectors).unwrap(); let rule = Arc::new(RwLock::new(StyleRule { selectors: selectors, @@ -34,7 +33,7 @@ fn get_mock_rules(css_selectors: &[&str]) -> Vec> { })); let guard = rule.read(); - guard.selectors.iter().map(|s| { + guard.selectors.0.iter().map(|s| { Rule { selector: s.complex_selector.clone(), style_rule: rule.clone(),