Skip to content

Commit

Permalink
feat: ToCss for Selector
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Oct 19, 2023
1 parent 1ec503f commit 268d9b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cssparser = "0.31.0"
ego-tree = "0.6.2"
html5ever = "0.26"
selectors = "0.25.0"
smallvec = "1.11.1"
tendril = "0.4.3"
ahash = "0.8"
indexmap = { version = "2.0.2", optional = true }
Expand Down
30 changes: 19 additions & 11 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
use std::convert::TryFrom;
use std::fmt;

use smallvec::SmallVec;

pub use cssparser::ToCss;
use html5ever::{LocalName, Namespace};
use selectors::{
matching,
parser::{self, ParseRelative, SelectorParseErrorKind},
parser::{self, ParseRelative, SelectorList, SelectorParseErrorKind},
};

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

impl Selector {
/// Parses a CSS selector group.

pub fn parse(selectors: &'_ str) -> Result<Self, SelectorErrorKind> {
let mut parser_input = cssparser::ParserInput::new(selectors);
let mut parser = cssparser::Parser::new(&mut parser_input);

parser::SelectorList::parse(&Parser, &mut parser, ParseRelative::No)
.map(|list| Selector { selectors: list.0 })
SelectorList::parse(&Parser, &mut parser, ParseRelative::No)
.map(|selectors| Self { selectors })
.map_err(SelectorErrorKind::from)
}

Expand All @@ -55,11 +53,21 @@ impl Selector {
);
context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
self.selectors
.0
.iter()
.any(|s| matching::matches_selector(s, 0, None, element, &mut context))
}
}

impl ToCss for Selector {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
{
self.selectors.to_css(dest)
}
}

/// An implementation of `Parser` for `selectors`
struct Parser;
impl<'i> parser::Parser<'i> for Parser {
Expand Down Expand Up @@ -103,7 +111,7 @@ impl AsRef<str> for CssString {
}
}

impl cssparser::ToCss for CssString {
impl ToCss for CssString {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
Expand All @@ -122,7 +130,7 @@ impl<'a> From<&'a str> for CssLocalName {
}
}

impl cssparser::ToCss for CssLocalName {
impl ToCss for CssLocalName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
Expand All @@ -147,7 +155,7 @@ impl parser::NonTSPseudoClass for NonTSPseudoClass {
}
}

impl cssparser::ToCss for NonTSPseudoClass {
impl ToCss for NonTSPseudoClass {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
Expand All @@ -164,7 +172,7 @@ impl parser::PseudoElement for PseudoElement {
type Impl = Simple;
}

impl cssparser::ToCss for PseudoElement {
impl ToCss for PseudoElement {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
Expand Down

0 comments on commit 268d9b9

Please sign in to comment.