Skip to content

Commit

Permalink
Partially update selectors
Browse files Browse the repository at this point in the history
This finally resolves the dependency on phf 0.8.

- Port to_unconditional from selectors – this is no longer implemented for
  ParsedCaseSensitivity.
- Add PhantomData to ExtraMatchingData which now requires a lifetime.
- Add unsupported Has(_) type
  • Loading branch information
fhanau committed Dec 5, 2024
1 parent 7cb9255 commit 95cc3fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ encoding_rs = "0.8.13"
memchr = "2.1.2"
hashbrown = "0.15.0"
mime = "0.3.16"
selectors = "0.24"
# HACK: Using commit between 0.24 and 0.25, allows us to get rid of phf 0.8.
selectors = {git = "https://github.com/servo/stylo.git", rev = "767bc8bc11424d107cd58c7c2d3a1695e387e4ea"}
thiserror = "2.0"

[dev-dependencies]
Expand Down
47 changes: 28 additions & 19 deletions src/selectors_vm/attribute_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::base::Bytes;
use crate::html::Namespace;
use crate::parser::{AttributeBuffer, AttributeOutline};
use memchr::{memchr, memchr2};
use selectors::attr::CaseSensitivity;
use selectors::attr::{CaseSensitivity, ParsedCaseSensitivity};
use std::cell::OnceCell;

static ID_ATTR: Bytes<'static> = Bytes::from_static("id");
Expand All @@ -14,6 +14,27 @@ const fn is_attr_whitespace(b: u8) -> bool {
b == b' ' || b == b'\n' || b == b'\r' || b == b'\t' || b == b'\x0c'
}

#[inline]
pub fn to_unconditional(
parsed: ParsedCaseSensitivity,
is_html_element_in_html_document: bool,
) -> CaseSensitivity {
match parsed {
ParsedCaseSensitivity::AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument
if is_html_element_in_html_document =>
{
CaseSensitivity::AsciiCaseInsensitive
}
ParsedCaseSensitivity::AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument => {
CaseSensitivity::CaseSensitive
}
ParsedCaseSensitivity::CaseSensitive | ParsedCaseSensitivity::ExplicitCaseSensitive => {
CaseSensitivity::CaseSensitive
}
ParsedCaseSensitivity::AsciiCaseInsensitive => CaseSensitivity::AsciiCaseInsensitive,
}
}

type MemoizedAttrValue<'i> = OnceCell<Option<Bytes<'i>>>;

pub(crate) struct AttributeMatcher<'i> {
Expand Down Expand Up @@ -99,19 +120,15 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn attr_eq(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
operand
.case_sensitivity
.to_unconditional(self.is_html_element)
to_unconditional(operand.case_sensitivity, self.is_html_element)
.eq(&actual_value, &operand.value)
})
}

#[inline]
pub fn matches_splitted_by_whitespace(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
let case_sensitivity = operand
.case_sensitivity
.to_unconditional(self.is_html_element);
let case_sensitivity = to_unconditional(operand.case_sensitivity, self.is_html_element);

actual_value
.split(|&b| is_attr_whitespace(b))
Expand All @@ -122,9 +139,7 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn has_attr_with_prefix(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
let case_sensitivity = operand
.case_sensitivity
.to_unconditional(self.is_html_element);
let case_sensitivity = to_unconditional(operand.case_sensitivity, self.is_html_element);

let prefix_len = operand.value.len();

Expand All @@ -136,9 +151,7 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn has_dash_matching_attr(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
let case_sensitivity = operand
.case_sensitivity
.to_unconditional(self.is_html_element);
let case_sensitivity = to_unconditional(operand.case_sensitivity, self.is_html_element);

if case_sensitivity.eq(&actual_value, &operand.value) {
return true;
Expand All @@ -154,9 +167,7 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn has_attr_with_suffix(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
let case_sensitivity = operand
.case_sensitivity
.to_unconditional(self.is_html_element);
let case_sensitivity = to_unconditional(operand.case_sensitivity, self.is_html_element);

let suffix_len = operand.value.len();
let value_len = actual_value.len();
Expand All @@ -169,9 +180,7 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn has_attr_with_substring(&self, operand: &AttrExprOperands) -> bool {
self.value_matches(&operand.name, |actual_value| {
let case_sensitivity = operand
.case_sensitivity
.to_unconditional(self.is_html_element);
let case_sensitivity = to_unconditional(operand.case_sensitivity, self.is_html_element);

let Some((&first_byte, rest)) = operand.value.split_first() else {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/selectors_vm/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl SelectorImpl for SelectorImplDescriptor {
type NonTSPseudoClass = NonTSPseudoClassStub;
type PseudoElement = PseudoElementStub;

type ExtraMatchingData = ();
type ExtraMatchingData<'a> = std::marker::PhantomData<&'a ()>;
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
Expand Down Expand Up @@ -127,6 +127,7 @@ impl SelectorsParser {
| Component::Part(_)
| Component::Host(_)
| Component::Is(_)
| Component::Has(_)
| Component::LastChild
| Component::LastOfType
| Component::NthLastChild(_, _)
Expand Down

0 comments on commit 95cc3fc

Please sign in to comment.