Skip to content

Truly independent web browser

LibWeb: Hold the argument selectors of a CSS pseudo-class in place

Andreas Kling0ed9a81b8ecf
diff --git a/Libraries/LibWeb/Rust/src/css/selector_arena.rs b/Libraries/LibWeb/Rust/src/css/selector_arena.rs
index 5da9628c1fb..5a3160e8c17 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_arena.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_arena.rs
@@ -2442,7 +2442,7 @@ impl SelectorArena {
impl Selector<'_> {
/// The selector as a parse tree again, for a transformation that builds another selector from
/// it. The tree names the arena's atoms, which the arena keeps live while it is packed again.
- pub(crate) fn to_tree(self) -> Arc<tree::CompiledSelector> {
+ pub(crate) fn to_tree(self) -> tree::CompiledSelector {
let compounds: Box<[tree::CompoundSelector]> = self
.compound_selectors()
.iter()
@@ -2507,7 +2507,7 @@ fn simple_to_tree(simple: &SimpleSelector<'_>) -> tree::SimpleSelector {
value: match pseudo_element.value {
PseudoElementValue::None => tree::PseudoElementValue::None,
PseudoElementValue::CompoundSelector(selector) => {
- tree::PseudoElementValue::CompoundSelector(selector.to_tree())
+ tree::PseudoElementValue::CompoundSelector(Arc::new(selector.to_tree()))
}
PseudoElementValue::Identifiers(identifiers) => {
tree::PseudoElementValue::Identifiers(identifiers.iter().map(SelectorString::from).collect())
diff --git a/Libraries/LibWeb/Rust/src/css/selector_operations.rs b/Libraries/LibWeb/Rust/src/css/selector_operations.rs
index e0e77c102a1..d39a78b5595 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_operations.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_operations.rs
@@ -6,8 +6,6 @@
//! Queries and transformations over Rust-owned CSS selectors.
-use std::sync::Arc;
-
use super::css_tokenizer::{ParserTokenKind, tokenize_for_parser};
use super::selector::{Combinator, PseudoClassType, PseudoElementType, RustSelector};
use super::selector_arena::{
@@ -33,7 +31,7 @@ fn pseudo_class(pseudo_class: PseudoClassType, arguments: SelectorList) -> Simpl
}))
}
-fn scope_selector() -> Arc<CompiledSelector> {
+fn scope_selector() -> CompiledSelector {
CompiledSelector::new(Box::new([CompoundSelector {
combinator: Combinator::None,
is_implicit_universal_anchor: false,
@@ -64,10 +62,9 @@ pub(crate) fn contains_nesting(selector: &CompiledSelector) -> bool {
selector.compound_selectors().iter().any(|compound| {
compound.simple_selectors.iter().any(|simple| match simple {
SimpleSelector::Nesting => true,
- SimpleSelector::PseudoClass(pseudo_class) => pseudo_class
- .argument_selector_list
- .iter()
- .any(|selector| contains_nesting(selector)),
+ SimpleSelector::PseudoClass(pseudo_class) => {
+ pseudo_class.argument_selector_list.iter().any(contains_nesting)
+ }
SimpleSelector::Invalid(source) => invalid_selector_names_nesting(source.as_ref()),
_ => false,
})
@@ -146,16 +143,13 @@ pub(crate) fn invalid_for_has(selector: &CompiledSelector) -> bool {
SimpleSelector::PseudoElement(_) => true,
SimpleSelector::PseudoClass(pseudo_class) => {
pseudo_class.pseudo_class == PseudoClassType::Has
- || pseudo_class
- .argument_selector_list
- .iter()
- .any(|selector| invalid_for_has(selector))
+ || pseudo_class.argument_selector_list.iter().any(invalid_for_has)
}
_ => false,
})
}
-fn absolutize(selector: &CompiledSelector, replacement: &SimpleSelector) -> Option<Arc<CompiledSelector>> {
+fn absolutize(selector: &CompiledSelector, replacement: &SimpleSelector) -> Option<CompiledSelector> {
if !contains_nesting(selector) {
return Some(CompiledSelector::new(selector.to_compounds()));
}
@@ -177,10 +171,7 @@ fn absolutize(selector: &CompiledSelector, replacement: &SimpleSelector) -> Opti
}
pseudo_class.argument_selector_list = arguments.into_boxed_slice();
if pseudo_class.pseudo_class == PseudoClassType::Has
- && pseudo_class
- .argument_selector_list
- .iter()
- .any(|selector| invalid_for_has(selector))
+ && pseudo_class.argument_selector_list.iter().any(invalid_for_has)
{
return None;
}
@@ -194,7 +185,7 @@ fn absolutize(selector: &CompiledSelector, replacement: &SimpleSelector) -> Opti
Some(CompiledSelector::new(compounds))
}
-pub(crate) fn relative_to(selector: &CompiledSelector, parent: SimpleSelector) -> Arc<CompiledSelector> {
+pub(crate) fn relative_to(selector: &CompiledSelector, parent: SimpleSelector) -> CompiledSelector {
let mut compounds = Vec::with_capacity(selector.compound_selectors().len() + 1);
compounds.push(CompoundSelector {
combinator: Combinator::None,
diff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rs
index c69934015a1..e9c1a95d313 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs
@@ -372,7 +372,7 @@ impl<'a> SelectorParser<'a> {
// NB: This is the list of a pseudo-class, which a selector can nest to the depth limit. Its lists are small
// here, so that the stack of the parser does not hold the inline memory of a list at each depth.
self.parse_selectors_in::<1, 1>(values, selector_type, parsing_mode)
- .map(|selectors| selectors.into_iter().map(Arc::new).collect())
+ .map(|selectors| selectors.into_iter().collect())
}
/// Parses a selector list into inline memory, for a list that goes into an arena at once.
@@ -409,7 +409,7 @@ impl<'a> SelectorParser<'a> {
Ok(selector) => selectors.push(selector),
Err(()) if parsing_mode == SelectorParsingMode::Forgiving => {
let source = selector_values.source_text(selector_values.source_range());
- selectors.push(CompiledSelector::owned_from_compounds::<1>(smallvec::smallvec![
+ selectors.push(CompiledSelector::from_compounds::<1>(smallvec::smallvec![
CompoundSelector {
combinator: if selector_type == SelectorType::Standalone {
Combinator::None
@@ -563,7 +563,7 @@ impl<'a> SelectorParser<'a> {
return Err(());
}
}
- Ok(CompiledSelector::owned_from_compounds(compounds))
+ Ok(CompiledSelector::from_compounds(compounds))
}
// NB: This keeps the memory for a name out of the stack of the functions that a nested selector calls again.
@@ -894,10 +894,7 @@ impl<'a> SelectorParser<'a> {
selector.argument_selector_list =
self.parse_selector_list(values, selector_type, SelectorParsingMode::Standard)?;
if parameter_type == PseudoClassParameterType::SelectorList
- && selector
- .argument_selector_list
- .iter()
- .any(|argument| contains_pseudo_element(argument))
+ && selector.argument_selector_list.iter().any(contains_pseudo_element)
{
return Err(());
}
@@ -1078,9 +1075,9 @@ impl<'a> SelectorParser<'a> {
return Err(());
}
compound.combinator = Combinator::None;
- Ok(PseudoElementValue::CompoundSelector(CompiledSelector::new(Box::new([
- compound,
- ]))))
+ Ok(PseudoElementValue::CompoundSelector(Arc::new(CompiledSelector::new(
+ Box::new([compound]),
+ ))))
}
// https://drafts.csswg.org/css-pseudo-4/#typedef-highlight-name-selector
// <highlight-name-selector> = '*' | <custom-ident>
@@ -1388,12 +1385,11 @@ impl ParsedSelectorTree {
&& (!matches!(first, Combinator::None | Combinator::Descendant)
|| !super::selector_operations::contains_nesting(selector));
if insert_nesting {
- let relative = super::selector_operations::relative_to(selector, SimpleSelector::Nesting);
- *selector = Arc::into_inner(relative).expect("a selector that was just made is not shared");
+ *selector = super::selector_operations::relative_to(selector, SimpleSelector::Nesting);
} else if first == Combinator::Descendant {
let mut compounds = selector.to_compounds();
compounds[0].combinator = Combinator::None;
- *selector = CompiledSelector::owned_from_compounds::<4>(compounds.into_vec().into());
+ *selector = CompiledSelector::from_compounds::<4>(compounds.into_vec().into());
}
}
self
@@ -1479,7 +1475,7 @@ fn parse_pseudo_element_selector(
is_implicit_universal_anchor: false,
simple_selectors: smallvec::smallvec![simple],
});
- let selector = CompiledSelector::from_compounds(normalize_pseudo_element_transitions(compounds));
+ let selector = CompiledSelector::from_compounds::<4>(normalize_pseudo_element_transitions(compounds));
let list = SelectorListRef::pack(&[selector]);
let selector = list.owned().next().expect("the list holds the selector");
Ok((selector, pseudo_element_type))
diff --git a/Libraries/LibWeb/Rust/src/css/selector_tree.rs b/Libraries/LibWeb/Rust/src/css/selector_tree.rs
index 860804488a7..02044d24dad 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_tree.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_tree.rs
@@ -16,7 +16,7 @@ use crate::css::selector::{
};
use crate::css::style::index::StyleAtomID;
-pub(crate) type SelectorList = Box<[Arc<CompiledSelector>]>;
+pub(crate) type SelectorList = Box<[CompiledSelector]>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttributeSelector {
@@ -140,23 +140,18 @@ impl CompoundRef<'_> {
/// One complex selector of the tree, its compounds flat: each compound is a
/// [`SimpleSelector::Compound`] naming its combinator, followed by its simple selectors.
-#[derive(Debug, PartialEq, Eq)]
+#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompiledSelector {
/// In place where the selector is short, so that it is one allocation.
components: SmallVec<[SimpleSelector; 6]>,
}
impl CompiledSelector {
- pub(crate) fn new(compound_selectors: Box<[CompoundSelector]>) -> Arc<Self> {
- Self::from_compounds(compound_selectors.into_vec().into())
- }
-
- pub(crate) fn from_compounds(compound_selectors: SmallVec<[CompoundSelector; 4]>) -> Arc<Self> {
- Arc::new(Self::owned_from_compounds(compound_selectors))
+ pub(crate) fn new(compound_selectors: Box<[CompoundSelector]>) -> Self {
+ Self::from_compounds::<4>(compound_selectors.into_vec().into())
}
- /// A selector of `compound_selectors` that is not shared yet.
- pub(crate) fn owned_from_compounds<const COMPOUNDS: usize>(
+ pub(crate) fn from_compounds<const COMPOUNDS: usize>(
compound_selectors: SmallVec<[CompoundSelector; COMPOUNDS]>,
) -> Self {
let count = compound_selectors