Skip to content

Commit

Permalink
Use selectors::SelectorList
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Nov 22, 2016
1 parent 81a3de3 commit f044659
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
10 changes: 5 additions & 5 deletions components/script/dom/node.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -304,12 +304,12 @@ impl Node {
}

pub struct QuerySelectorIterator {
selectors: Vec<Selector<SelectorImpl>>,
selectors: SelectorList<SelectorImpl>,
iterator: TreeIterator,
}

impl<'a> QuerySelectorIterator {
fn new(iter: TreeIterator, selectors: Vec<Selector<SelectorImpl>>)
fn new(iter: TreeIterator, selectors: SelectorList<SelectorImpl>)
-> QuerySelectorIterator {
QuerySelectorIterator {
selectors: selectors,
Expand All @@ -322,7 +322,7 @@ impl<'a> Iterator for QuerySelectorIterator {
type Item = 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
// (instead of passing `None`)? Probably.
self.iterator.by_ref().filter_map(|node| {
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
23 changes: 4 additions & 19 deletions components/style/stylesheets.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -196,30 +196,15 @@ impl ToCss for MediaRule {

#[derive(Debug)]
pub struct StyleRule {
pub selectors: Vec<Selector<SelectorImpl>>,
pub selectors: SelectorList<SelectorImpl>,
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 {
// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule
fn to_css<W>(&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
Expand Down Expand Up @@ -582,7 +567,7 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> {
fn parse_block(&mut self, prelude: SelectorList<SelectorImpl>, input: &mut Parser)
-> Result<CssRule, ()> {
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)))
}))))
}
Expand Down
4 changes: 2 additions & 2 deletions components/style/stylist.rs
Expand Up @@ -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())
Expand All @@ -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());
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/style/stylesheets.rs
Expand Up @@ -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![
Expand All @@ -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(
Expand All @@ -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![
Expand Down Expand Up @@ -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(
Expand All @@ -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![
Expand All @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/style/stylist.rs
Expand Up @@ -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;
Expand All @@ -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<Vec<Rule>> {
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,
Expand All @@ -34,7 +33,7 @@ fn get_mock_rules(css_selectors: &[&str]) -> Vec<Vec<Rule>> {
}));

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(),
Expand Down

0 comments on commit f044659

Please sign in to comment.