Skip to content

Commit 8bc800d

Browse files
authored
Merge pull request #153 from EdJoPaTo/selector-to-css
feat: ToCss for Selector
2 parents 1ec503f + 268d9b9 commit 8bc800d

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ cssparser = "0.31.0"
1919
ego-tree = "0.6.2"
2020
html5ever = "0.26"
2121
selectors = "0.25.0"
22-
smallvec = "1.11.1"
2322
tendril = "0.4.3"
2423
ahash = "0.8"
2524
indexmap = { version = "2.0.2", optional = true }

src/selector.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
use std::convert::TryFrom;
44
use std::fmt;
55

6-
use smallvec::SmallVec;
7-
6+
pub use cssparser::ToCss;
87
use html5ever::{LocalName, Namespace};
98
use selectors::{
109
matching,
11-
parser::{self, ParseRelative, SelectorParseErrorKind},
10+
parser::{self, ParseRelative, SelectorList, SelectorParseErrorKind},
1211
};
1312

1413
use crate::error::SelectorErrorKind;
@@ -20,18 +19,17 @@ use crate::ElementRef;
2019
#[derive(Debug, Clone, PartialEq, Eq)]
2120
pub struct Selector {
2221
/// The CSS selectors.
23-
selectors: SmallVec<[parser::Selector<Simple>; 1]>,
22+
selectors: SelectorList<Simple>,
2423
}
2524

2625
impl Selector {
2726
/// Parses a CSS selector group.
28-
2927
pub fn parse(selectors: &'_ str) -> Result<Self, SelectorErrorKind> {
3028
let mut parser_input = cssparser::ParserInput::new(selectors);
3129
let mut parser = cssparser::Parser::new(&mut parser_input);
3230

33-
parser::SelectorList::parse(&Parser, &mut parser, ParseRelative::No)
34-
.map(|list| Selector { selectors: list.0 })
31+
SelectorList::parse(&Parser, &mut parser, ParseRelative::No)
32+
.map(|selectors| Self { selectors })
3533
.map_err(SelectorErrorKind::from)
3634
}
3735

@@ -55,11 +53,21 @@ impl Selector {
5553
);
5654
context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
5755
self.selectors
56+
.0
5857
.iter()
5958
.any(|s| matching::matches_selector(s, 0, None, element, &mut context))
6059
}
6160
}
6261

62+
impl ToCss for Selector {
63+
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
64+
where
65+
W: fmt::Write,
66+
{
67+
self.selectors.to_css(dest)
68+
}
69+
}
70+
6371
/// An implementation of `Parser` for `selectors`
6472
struct Parser;
6573
impl<'i> parser::Parser<'i> for Parser {
@@ -103,7 +111,7 @@ impl AsRef<str> for CssString {
103111
}
104112
}
105113

106-
impl cssparser::ToCss for CssString {
114+
impl ToCss for CssString {
107115
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
108116
where
109117
W: fmt::Write,
@@ -122,7 +130,7 @@ impl<'a> From<&'a str> for CssLocalName {
122130
}
123131
}
124132

125-
impl cssparser::ToCss for CssLocalName {
133+
impl ToCss for CssLocalName {
126134
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
127135
where
128136
W: fmt::Write,
@@ -147,7 +155,7 @@ impl parser::NonTSPseudoClass for NonTSPseudoClass {
147155
}
148156
}
149157

150-
impl cssparser::ToCss for NonTSPseudoClass {
158+
impl ToCss for NonTSPseudoClass {
151159
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
152160
where
153161
W: fmt::Write,
@@ -164,7 +172,7 @@ impl parser::PseudoElement for PseudoElement {
164172
type Impl = Simple;
165173
}
166174

167-
impl cssparser::ToCss for PseudoElement {
175+
impl ToCss for PseudoElement {
168176
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
169177
where
170178
W: fmt::Write,

0 commit comments

Comments
 (0)