Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ToCss for Selector #153

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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