Skip to content

Commit

Permalink
Merge pull request #72 from brave/cssparser-0.28
Browse files Browse the repository at this point in the history
Update `cssparser` and `selectors`, remove `matches`
  • Loading branch information
antonok-edm authored Oct 31, 2024
2 parents c48dca9 + 651facb commit 81a17cb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kuchikiki"
version = "0.8.5-speedreader"
version = "0.8.6-speedreader"
authors = [
"Brave Authors",
"Ralph Giles <[email protected]>",
Expand All @@ -15,10 +15,9 @@ edition = "2018"
name = "kuchikiki"

[dependencies]
cssparser = "0.27"
matches = "0.1.4"
cssparser = "0.28"
html5ever = "0.25.1"
selectors = "0.22"
selectors = "0.23"
indexmap = "2.2.6"

[dev-dependencies]
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Kuchikiki (口利き) is an HTML tree manipulation library for Rust.

#[macro_use]
extern crate html5ever;
#[macro_use]
extern crate matches;

mod attributes;
mod cell_extras;
Expand Down
69 changes: 41 additions & 28 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,38 @@ static SELECTOR_WHITESPACE: &[char] = &[' ', '\t', '\n', '\r', '\x0C'];
#[derive(Debug, Clone)]
pub struct KuchikiSelectors;

#[derive(Default, Clone, Eq, PartialEq)]
pub struct CssLocalName(LocalName);

impl cssparser::ToCss for CssLocalName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
{
write!(dest, "{}", self.0)
}
}

impl<'a> From<&'a str> for CssLocalName {
fn from(value: &'a str) -> Self {
Self(LocalName::from(value))
}
}

impl AsRef<str> for CssLocalName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl SelectorImpl for KuchikiSelectors {
type AttrValue = String;
type Identifier = LocalName;
type ClassName = LocalName;
type LocalName = LocalName;
type PartName = LocalName;
type NamespacePrefix = LocalName;
type AttrValue = CssLocalName;
type Identifier = CssLocalName;
type LocalName = CssLocalName;
type NamespacePrefix = CssLocalName;
type NamespaceUrl = Namespace;
type BorrowedNamespaceUrl = Namespace;
type BorrowedLocalName = LocalName;
type BorrowedLocalName = CssLocalName;

type NonTSPseudoClass = PseudoClass;
type PseudoElement = PseudoElement;
Expand Down Expand Up @@ -107,10 +129,6 @@ impl NonTSPseudoClass for PseudoClass {
PseudoClass::Active | PseudoClass::Hover | PseudoClass::Focus
)
}

fn has_zero_specificity(&self) -> bool {
false
}
}

impl ToCss for PseudoClass {
Expand Down Expand Up @@ -206,26 +224,21 @@ impl selectors::Element for NodeDataRef<ElementData> {
}

#[inline]
fn has_local_name(&self, name: &LocalName) -> bool {
self.name.local == *name
fn has_local_name(&self, name: &CssLocalName) -> bool {
self.name.local == *name.0
}
#[inline]
fn has_namespace(&self, namespace: &Namespace) -> bool {
self.name.ns == *namespace
}

#[inline]
fn is_part(&self, _name: &LocalName) -> bool {
fn is_part(&self, _name: &CssLocalName) -> bool {
false
}

#[inline]
fn exported_part(&self, _: &LocalName) -> Option<LocalName> {
None
}

#[inline]
fn imported_part(&self, _: &LocalName) -> Option<LocalName> {
fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName> {
None
}

Expand Down Expand Up @@ -254,18 +267,18 @@ impl selectors::Element for NodeDataRef<ElementData> {
}

#[inline]
fn has_id(&self, id: &LocalName, case_sensitivity: CaseSensitivity) -> bool {
fn has_id(&self, id: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
self.attributes
.borrow()
.get(local_name!("id"))
.map_or(false, |id_attr| {
case_sensitivity.eq(id.as_bytes(), id_attr.as_bytes())
case_sensitivity.eq(id.0.as_bytes(), id_attr.as_bytes())
})
}

#[inline]
fn has_class(&self, name: &LocalName, case_sensitivity: CaseSensitivity) -> bool {
let name = name.as_bytes();
fn has_class(&self, name: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
let name = name.0.as_bytes();
!name.is_empty()
&& if let Some(class_attr) = self.attributes.borrow().get(local_name!("class")) {
class_attr
Expand All @@ -280,18 +293,18 @@ impl selectors::Element for NodeDataRef<ElementData> {
fn attr_matches(
&self,
ns: &NamespaceConstraint<&Namespace>,
local_name: &LocalName,
operation: &AttrSelectorOperation<&String>,
local_name: &CssLocalName,
operation: &AttrSelectorOperation<&CssLocalName>,
) -> bool {
let attrs = self.attributes.borrow();
match *ns {
NamespaceConstraint::Any => attrs
.map
.iter()
.any(|(name, attr)| name.local == *local_name && operation.eval_str(&attr.value)),
.any(|(name, attr)| name.local == *local_name.0 && operation.eval_str(&attr.value)),
NamespaceConstraint::Specific(ns_url) => attrs
.map
.get(&ExpandedName::new(ns_url, local_name.clone()))
.get(&ExpandedName::new(ns_url, local_name.0.clone()))
.map_or(false, |attr| operation.eval_str(&attr.value)),
}
}
Expand Down

0 comments on commit 81a17cb

Please sign in to comment.