Use selectors::SelectorList

This commit is contained in:
Simon Sapin 2016-11-21 19:48:11 +01:00
parent 81a3de3299
commit f044659b7c
5 changed files with 19 additions and 35 deletions

View file

@ -71,7 +71,7 @@ use script_layout_interface::{LayoutElementType, LayoutNodeType, TrustedNodeAddr
use script_layout_interface::message::Msg; use script_layout_interface::message::Msg;
use script_traits::UntrustedNodeAddress; use script_traits::UntrustedNodeAddress;
use selectors::matching::{MatchingReason, matches}; use selectors::matching::{MatchingReason, matches};
use selectors::parser::Selector; use selectors::parser::SelectorList;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::cell::{Cell, UnsafeCell}; use std::cell::{Cell, UnsafeCell};
@ -304,12 +304,12 @@ impl Node {
} }
pub struct QuerySelectorIterator { pub struct QuerySelectorIterator {
selectors: Vec<Selector<SelectorImpl>>, selectors: SelectorList<SelectorImpl>,
iterator: TreeIterator, iterator: TreeIterator,
} }
impl<'a> QuerySelectorIterator { impl<'a> QuerySelectorIterator {
fn new(iter: TreeIterator, selectors: Vec<Selector<SelectorImpl>>) fn new(iter: TreeIterator, selectors: SelectorList<SelectorImpl>)
-> QuerySelectorIterator { -> QuerySelectorIterator {
QuerySelectorIterator { QuerySelectorIterator {
selectors: selectors, selectors: selectors,
@ -322,7 +322,7 @@ impl<'a> Iterator for QuerySelectorIterator {
type Item = Root<Node>; type Item = Root<Node>;
fn next(&mut self) -> Option<Root<Node>> { fn next(&mut self) -> Option<Root<Node>> {
let selectors = &self.selectors; let selectors = &self.selectors.0;
// TODO(cgaebel): Is it worth it to build a bloom filter here // TODO(cgaebel): Is it worth it to build a bloom filter here
// (instead of passing `None`)? Probably. // (instead of passing `None`)? Probably.
self.iterator.by_ref().filter_map(|node| { self.iterator.by_ref().filter_map(|node| {
@ -716,7 +716,7 @@ impl Node {
let mut descendants = self.traverse_preorder(); let mut descendants = self.traverse_preorder();
// Skip the root of the tree. // Skip the root of the tree.
assert!(&*descendants.next().unwrap() == self); assert!(&*descendants.next().unwrap() == self);
Ok(QuerySelectorIterator::new(descendants, selectors.0)) Ok(QuerySelectorIterator::new(descendants, selectors))
} }
} }
} }

View file

@ -17,7 +17,7 @@ use parking_lot::RwLock;
use parser::{ParserContext, ParserContextExtraData, log_css_error}; use parser::{ParserContext, ParserContextExtraData, log_css_error};
use properties::{PropertyDeclarationBlock, parse_property_declaration_list}; use properties::{PropertyDeclarationBlock, parse_property_declaration_list};
use selector_parser::{SelectorImpl, SelectorParser}; use selector_parser::{SelectorImpl, SelectorParser};
use selectors::parser::{Selector, SelectorList}; use selectors::parser::SelectorList;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use std::cell::Cell; use std::cell::Cell;
use std::fmt; use std::fmt;
@ -196,30 +196,15 @@ impl ToCss for MediaRule {
#[derive(Debug)] #[derive(Debug)]
pub struct StyleRule { pub struct StyleRule {
pub selectors: Vec<Selector<SelectorImpl>>, pub selectors: SelectorList<SelectorImpl>,
pub block: Arc<RwLock<PropertyDeclarationBlock>>, pub block: Arc<RwLock<PropertyDeclarationBlock>>,
} }
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<W>(&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 { impl ToCss for StyleRule {
// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// Step 1 // Step 1
try!(self.selectors_to_css(dest)); try!(self.selectors.to_css(dest));
// Step 2 // Step 2
try!(dest.write_str(" { ")); try!(dest.write_str(" { "));
// Step 3 // Step 3
@ -582,7 +567,7 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> {
fn parse_block(&mut self, prelude: SelectorList<SelectorImpl>, input: &mut Parser) fn parse_block(&mut self, prelude: SelectorList<SelectorImpl>, input: &mut Parser)
-> Result<CssRule, ()> { -> Result<CssRule, ()> {
Ok(CssRule::Style(Arc::new(RwLock::new(StyleRule { 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))) block: Arc::new(RwLock::new(parse_property_declaration_list(self.context, input)))
})))) }))))
} }

View file

@ -189,7 +189,7 @@ impl Stylist {
match *rule { match *rule {
CssRule::Style(ref style_rule) => { CssRule::Style(ref style_rule) => {
let guard = style_rule.read(); 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 { let map = if let Some(ref pseudo) = selector.pseudo_element {
pseudos_map pseudos_map
.entry(pseudo.clone()) .entry(pseudo.clone())
@ -208,7 +208,7 @@ impl Stylist {
} }
*rules_source_order += 1; *rules_source_order += 1;
for selector in &guard.selectors { for selector in &guard.selectors.0 {
state_deps.note_selector(&selector.complex_selector); state_deps.note_selector(&selector.complex_selector);
if selector.affects_siblings() { if selector.affects_siblings() {
sibling_affecting_selectors.push(selector.clone()); sibling_affecting_selectors.push(selector.clone());

View file

@ -63,7 +63,7 @@ fn test_parse_stylesheet() {
url: NsAtom::from("http://www.w3.org/1999/xhtml") url: NsAtom::from("http://www.w3.org/1999/xhtml")
}))), }))),
CssRule::Style(Arc::new(RwLock::new(StyleRule { CssRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![ selectors: SelectorList(vec![
Selector { Selector {
complex_selector: Arc::new(ComplexSelector { complex_selector: Arc::new(ComplexSelector {
compound_selector: vec![ compound_selector: vec![
@ -89,7 +89,7 @@ fn test_parse_stylesheet() {
pseudo_element: None, pseudo_element: None,
specificity: (0 << 20) + (1 << 10) + (1 << 0), specificity: (0 << 20) + (1 << 10) + (1 << 0),
}, },
], ]),
block: Arc::new(RwLock::new(PropertyDeclarationBlock { block: Arc::new(RwLock::new(PropertyDeclarationBlock {
declarations: vec![ declarations: vec![
(PropertyDeclaration::Display(DeclaredValue::Value( (PropertyDeclaration::Display(DeclaredValue::Value(
@ -102,7 +102,7 @@ fn test_parse_stylesheet() {
})), })),
}))), }))),
CssRule::Style(Arc::new(RwLock::new(StyleRule { CssRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![ selectors: SelectorList(vec![
Selector { Selector {
complex_selector: Arc::new(ComplexSelector { complex_selector: Arc::new(ComplexSelector {
compound_selector: vec![ compound_selector: vec![
@ -137,7 +137,7 @@ fn test_parse_stylesheet() {
pseudo_element: None, pseudo_element: None,
specificity: (0 << 20) + (0 << 10) + (1 << 0), specificity: (0 << 20) + (0 << 10) + (1 << 0),
}, },
], ]),
block: Arc::new(RwLock::new(PropertyDeclarationBlock { block: Arc::new(RwLock::new(PropertyDeclarationBlock {
declarations: vec![ declarations: vec![
(PropertyDeclaration::Display(DeclaredValue::Value( (PropertyDeclaration::Display(DeclaredValue::Value(
@ -148,7 +148,7 @@ fn test_parse_stylesheet() {
})), })),
}))), }))),
CssRule::Style(Arc::new(RwLock::new(StyleRule { CssRule::Style(Arc::new(RwLock::new(StyleRule {
selectors: vec![ selectors: SelectorList(vec![
Selector { Selector {
complex_selector: Arc::new(ComplexSelector { complex_selector: Arc::new(ComplexSelector {
compound_selector: vec![ compound_selector: vec![
@ -172,7 +172,7 @@ fn test_parse_stylesheet() {
pseudo_element: None, pseudo_element: None,
specificity: (1 << 20) + (1 << 10) + (0 << 0), specificity: (1 << 20) + (1 << 10) + (0 << 0),
}, },
], ]),
block: Arc::new(RwLock::new(PropertyDeclarationBlock { block: Arc::new(RwLock::new(PropertyDeclarationBlock {
declarations: vec![ declarations: vec![
(PropertyDeclaration::BackgroundColor(DeclaredValue::Value( (PropertyDeclaration::BackgroundColor(DeclaredValue::Value(

View file

@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use html5ever_atoms::LocalName; use html5ever_atoms::LocalName;
use parking_lot::RwLock; use parking_lot::RwLock;
use selectors::parser::LocalName as LocalNameSelector; 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. /// Each sublist of the result contains the Rules for one StyleRule.
fn get_mock_rules(css_selectors: &[&str]) -> Vec<Vec<Rule>> { fn get_mock_rules(css_selectors: &[&str]) -> Vec<Vec<Rule>> {
css_selectors.iter().enumerate().map(|(i, selectors)| { 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 { let rule = Arc::new(RwLock::new(StyleRule {
selectors: selectors, selectors: selectors,
@ -34,7 +33,7 @@ fn get_mock_rules(css_selectors: &[&str]) -> Vec<Vec<Rule>> {
})); }));
let guard = rule.read(); let guard = rule.read();
guard.selectors.iter().map(|s| { guard.selectors.0.iter().map(|s| {
Rule { Rule {
selector: s.complex_selector.clone(), selector: s.complex_selector.clone(),
style_rule: rule.clone(), style_rule: rule.clone(),