Truly independent web browser
LibWeb: Keep the selector list of a CSS style rule in place
Andreas Klingbd33d6aaab90
diff --git a/Libraries/LibWeb/Rust/src/css/parser/memory_tests.rs b/Libraries/LibWeb/Rust/src/css/parser/memory_tests.rsindex f8226fafb9b..fcd95b0b454 100644--- a/Libraries/LibWeb/Rust/src/css/parser/memory_tests.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/memory_tests.rs@@ -93,7 +93,8 @@ fn check(units: &[Unit]) {"{:<44} {:>8.1} B (ceiling {:>6}; {})",unit.name, unit.bytes, unit.ceiling, unit.typical);- if unit.bytes > unit.ceiling {+ // NB: A unit can be a difference of averages, which is only near its exact value.+ if unit.bytes > unit.ceiling + 1e-6 {over.push(unit.name);}}@@ -187,7 +188,7 @@ fn parsed_sheet_bytes_per_unit() {Unit {name: "style rule `.c` with a shared block",bytes: rule.0,- ceiling: 144.0,+ ceiling: 112.0,typical: "24 B + 16 B selector + 16 B facts of its compound",},Unit {diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rsindex 09276d530b9..eb92d37efdc 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs@@ -1669,7 +1669,7 @@ const _: () = assert!(size_of::<ParsedRule>() == 56);enum ParsedRulePayload {None,Style {- selector_list: Option<Arc<RustParsedSelectorList>>,+ selector_list: Option<RustParsedSelectorList>,declaration_block: SharedDeclarationBlock,},Keyframe(Arc<KeyframeData>),@@ -1724,7 +1724,7 @@ impl ParsedRule {.then_some((self.start_line as usize, self.start_column as usize))}- fn selector_list(&self) -> Option<&Arc<RustParsedSelectorList>> {+ fn selector_list(&self) -> Option<&RustParsedSelectorList> {match &self.payload {ParsedRulePayload::Style { selector_list, .. } => selector_list.as_ref(),_ => None,@@ -1874,7 +1874,7 @@ struct OpenRule {takes_declarations: bool,declarations_start: usize,declaration_count: usize,- selector_list: Option<Arc<RustParsedSelectorList>>,+ selector_list: Option<RustParsedSelectorList>,outer_nesting: NestingTarget,outer_parent: NestingTarget,/// Where the children of the rule start in the open children.@@ -3184,7 +3184,7 @@ impl<'a> SyntaxParseBuilder<'a> {selector_type: SelectorType,nesting_parent: StyleNestingParent,nesting: NestingTarget,- ) -> Option<(Arc<RustParsedSelectorList>, Option<RuleOrdinal>)> {+ ) -> Option<(RustParsedSelectorList, Option<RuleOrdinal>)> {let declared_namespaces = self.declared_namespaces.iter()@@ -3202,7 +3202,7 @@ impl<'a> SyntaxParseBuilder<'a> {let (list, ordinal) = selector_list.adapt_for_nesting(nesting_parent).into_rule_list_in(&mut self.selectors.borrow_mut(), nesting);- Some((Arc::new(list), ordinal))+ Some((list, ordinal))}fn rule_descriptors(children: &[ParsedRuleChild], kind: ParsedRuleKind) -> Option<Arc<DescriptorBlockData>> {@@ -3482,15 +3482,15 @@ impl<'a> SyntaxParseBuilder<'a> {selectors: selectors.push_copy(&list.selectors, nesting),};scope_selectors = Some(Arc::new(ScopeSelectors {- start: start.map(|start| Arc::new(repack(start, outer_parent))),- end: end.map(|end| Arc::new(repack(end, NestingTarget::Root))),+ start: start.map(|start| repack(start, outer_parent)),+ end: end.map(|end| repack(end, NestingTarget::Root)),}));}ParsedRulePrelude::Import(import) => {if import.has_scope {scope_selectors = Some(Arc::new(ScopeSelectors {- start: import.scope_start.map(Arc::new),- end: import.scope_end.map(Arc::new),+ start: import.scope_start,+ end: import.scope_end,}));}import_rule = Some(Arc::new(ImportRuleData {diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rsindex 3386d9de8ee..b8945888e07 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs@@ -84,7 +84,7 @@ fn style_rule_mutation_forks_current_selectors_and_declarations() {drop(sheet);let shared = StyleRule::new(rule.selectors(), rule.declarations.data());let original_selectors = rule.selectors();- assert!(Arc::ptr_eq(&original_selectors, &shared.selectors()));+ assert!(original_selectors.is_same_list(&shared.selectors()));let mut block = rule.declarations.clone();let retained_block = rule.declarations.clone();block.replace(&declarations("width: 31px; --色: 青"));@@ -97,18 +97,18 @@ fn style_rule_mutation_forks_current_selectors_and_declarations() {StyleNestingParent::Style));let changed_selectors = rule.selectors();- assert!(!Arc::ptr_eq(&changed_selectors, &original_selectors));+ assert!(!changed_selectors.is_same_list(&original_selectors));assert!(!rule.set_selector_text(TokenizerInput::Utf16(&utf16("[")),&[],Default::default(),StyleNestingParent::None));- assert!(Arc::ptr_eq(&changed_selectors, &rule.selectors()));+ assert!(changed_selectors.is_same_list(&rule.selectors()));let snapshot = StyleRule::new(rule.selectors(), rule.declarations.data());- assert!(Arc::ptr_eq(&changed_selectors, &snapshot.selectors()));+ assert!(changed_selectors.is_same_list(&snapshot.selectors()));for original in [&shared, &independent] {- assert!(Arc::ptr_eq(&original_selectors, &original.selectors()));+ assert!(original_selectors.is_same_list(&original.selectors()));assert_eq!(first_value(&original.declarations), utf16("13px"));}drop(rule);diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/shared_rules.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/shared_rules.rsindex f8f1dad1927..2c680ed924c 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/shared_rules.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/shared_rules.rs@@ -326,7 +326,7 @@ impl<'a> SharedRule<'a> {rule.keyframe().map(|value| &**value)}- pub(crate) fn selectors(self) -> Option<Arc<RustParsedSelectorList>> {+ pub(crate) fn selectors(self) -> Option<RustParsedSelectorList> {let rule = self.parsed()?;rule.selector_list().cloned()}diff --git a/Libraries/LibWeb/Rust/src/css/rule.rs b/Libraries/LibWeb/Rust/src/css/rule.rsindex 01cdde6cf19..1cc0e05afec 100644--- a/Libraries/LibWeb/Rust/src/css/rule.rs+++ b/Libraries/LibWeb/Rust/src/css/rule.rs@@ -355,9 +355,9 @@ impl NativeRule {RulePayload::Style(style) => {let selectors = style.selectors();if !selectors.selectors.is_nested_in(context) {- style.replace_selectors(Arc::new(RustParsedSelectorList {+ style.replace_selectors(RustParsedSelectorList {selectors: selectors.selectors.nested_in(context),- }));+ });}let selectors = style.selectors();let own = NestingContext::Rule(&selectors.selectors);@@ -369,9 +369,9 @@ impl NativeRule {&& !start.selectors.is_nested_in(parent){*scope.borrow_mut() = Arc::new(ScopeSelectors {- start: Some(Arc::new(RustParsedSelectorList {+ start: Some(RustParsedSelectorList {selectors: start.selectors.nested_in(parent),- })),+ }),end: current.end.clone(),});}@@ -943,7 +943,7 @@ pub unsafe extern "C" fn rust_rule_set_selector_text(#[unsafe(no_mangle)]pub extern "C" fn rust_rule_selectors(rule: &NativeRule) -> *const c_void {- Arc::as_ptr(&rule.style_rule().selectors()).cast()+ rule.style_rule().selectors_pointer().cast()}/// Call only for style or nested-declaration rules. The returned list must be released withdiff --git a/Libraries/LibWeb/Rust/src/css/rule/compilation.rs b/Libraries/LibWeb/Rust/src/css/rule/compilation.rsindex 385c84da69a..564490dcaf1 100644--- a/Libraries/LibWeb/Rust/src/css/rule/compilation.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/compilation.rs@@ -157,7 +157,7 @@ impl CompilationContext {environment: MediaEnvironment<'_>,host: CompilationHost<'_>,publication: Option<&Publisher<'_>>,- matching: Option<std::sync::Arc<crate::css::selector_parser::RustParsedSelectorList>>,+ matching: Option<crate::css::selector_parser::RustParsedSelectorList>,) -> Self {let mut context = self.clone();if self.purpose == NativeCompilationPurpose::Rules {@@ -1024,7 +1024,7 @@ mod tests {let parent_selectors = parent.style_rule().selectors();let edited = NestingContext::Rule(&parent_selectors.selectors);let child_selectors = child.style_rule().selectors();- assert!(!std::sync::Arc::ptr_eq(&parsed_child, &child_selectors));+ assert!(!parsed_child.is_same_list(&child_selectors));assert!(child_selectors.selectors.is_nested_in(edited));let Some(Nesting::Rule(named)) = child_selectors.selectors.view().nesting() else {panic!("the child's `&` names a style rule");@@ -1032,10 +1032,13 @@ mod tests {assert_eq!(named.iter().next().unwrap().specificity().classes, 1);let bound_scope = scope.scope();assert!(bound_scope.start.as_ref().unwrap().selectors.is_nested_in(edited));- assert!(std::sync::Arc::ptr_eq(- parsed_scope.end.as_ref().unwrap(),- bound_scope.end.as_ref().unwrap()- ));+ assert!(+ parsed_scope+ .end+ .as_ref()+ .unwrap()+ .is_same_list(bound_scope.end.as_ref().unwrap())+ );}#[test]@@ -1563,7 +1566,7 @@ mod tests {use crate::css::selector_parser::RustParsedSelectorList;type Bound = (- std::sync::Arc<RustParsedSelectorList>,+ RustParsedSelectorList,std::sync::Arc<crate::css::style::bridge::BoundScopeChain>,);fn visit(rule: &NativeRule, inputs: &SelectorInputs, bound: &mut Vec<Bound>) {diff --git a/Libraries/LibWeb/Rust/src/css/rule/compilation/publication.rs b/Libraries/LibWeb/Rust/src/css/rule/compilation/publication.rsindex 7652c8bfe54..8154c03cd50 100644--- a/Libraries/LibWeb/Rust/src/css/rule/compilation/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/compilation/publication.rs@@ -43,7 +43,7 @@ enum Enclosing {None,Scope,/// A style rule, with its selectors.- Style(Arc<RustParsedSelectorList>),+ Style(RustParsedSelectorList),}// Binding belongs to this document-thread traversal, not to the shared rule graph.@@ -56,14 +56,14 @@ pub(super) struct SelectorInputs {impl SelectorInputs {/// The selectors the engine matches `rule` by, whose `&` names what the sheet's arena says.- pub(super) fn matching_selectors(&self, rule: RuleRef<'_>) -> Option<Arc<RustParsedSelectorList>> {+ pub(super) fn matching_selectors(&self, rule: RuleRef<'_>) -> Option<RustParsedSelectorList> {match rule.rule_type() {NativeRuleType::Style => rule.selectors(),NativeRuleType::NestedDeclarations => match &self.enclosing {Enclosing::Style(selectors) => Some(selectors.clone()),- Enclosing::Scope => Some(Arc::new(RustParsedSelectorList {+ Enclosing::Scope => Some(RustParsedSelectorList {selectors: SelectorListRef::pack(&scope_root_selector_list()),- })),+ }),Enclosing::None => unreachable!("nested declarations are inside a style rule or `@scope`"),},_ => None,@@ -78,7 +78,7 @@ impl SelectorInputs {pub(super) fn within(&self,rule: RuleRef<'_>,- matching: Option<Arc<RustParsedSelectorList>>,+ matching: Option<RustParsedSelectorList>,implicit_root: u32,) -> Self {let mut nested = self.clone();@@ -88,7 +88,7 @@ impl SelectorInputs {nested.enclosing = Enclosing::Style(selectors);}if let Some(scope) = rule.scope() {- Arc::make_mut(&mut nested.scope).push(scope.start.as_deref(), scope.end.as_deref(), implicit_root);+ Arc::make_mut(&mut nested.scope).push(scope.start.as_ref(), scope.end.as_ref(), implicit_root);}match rule.rule_type() {NativeRuleType::Scope => nested.enclosing = Enclosing::Scope,@@ -176,7 +176,7 @@ impl<'a> Publisher<'a> {&self,rule: RuleRef<'_>,context: &CompilationContext,- selectors: Arc<RustParsedSelectorList>,+ selectors: RustParsedSelectorList,) {self.host().write_rules(RuleWrite::ReplaceSelectors {identity: rule.identity(),@@ -194,7 +194,7 @@ impl<'a> Publisher<'a> {rule: RuleRef<'_>,source: &NativeStyleSheet,context: &CompilationContext,- selectors: Option<&Arc<RustParsedSelectorList>>,+ selectors: Option<&RustParsedSelectorList>,) -> NativeCompilationResult {let kind = match rule.rule_type() {NativeRuleType::Style | NativeRuleType::NestedDeclarations => {diff --git a/Libraries/LibWeb/Rust/src/css/rule/cssom.rs b/Libraries/LibWeb/Rust/src/css/rule/cssom.rsindex 30ceb018f61..93d7acf933d 100644--- a/Libraries/LibWeb/Rust/src/css/rule/cssom.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/cssom.rs@@ -462,7 +462,7 @@ impl NativeRule {/// The selector list of a style rule, as a pointer that C++ turns into text.pub fn style_selector_list(&self) -> *const core::ffi::c_void {- Arc::as_ptr(&self.style_rule().selectors()).cast()+ self.style_rule().selectors_pointer().cast()}/// The selector lists that a @scope or a scoped @import rule starts and ends with, as pointers that C++ turns into@@ -473,8 +473,9 @@ impl NativeRule {RulePayload::Import { scope: Some(scope), .. } => Arc::clone(scope),_ => return None,};- let pointer = |list: &Option<Arc<crate::css::selector_parser::RustParsedSelectorList>>| {- list.as_ref().map_or(core::ptr::null(), |list| Arc::as_ptr(list).cast())+ let pointer = |list: &Option<crate::css::selector_parser::RustParsedSelectorList>| {+ list.as_ref()+ .map_or(core::ptr::null(), |list| std::ptr::from_ref(list).cast())};Some((pointer(&scope.start), pointer(&scope.end)))}diff --git a/Libraries/LibWeb/Rust/src/css/rule/read.rs b/Libraries/LibWeb/Rust/src/css/rule/read.rsindex 01f8970b03c..fecb95f3b19 100644--- a/Libraries/LibWeb/Rust/src/css/rule/read.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/read.rs@@ -488,7 +488,7 @@ impl<'a> RuleRef<'a> {}}- pub(crate) fn selectors(self) -> Option<Arc<RustParsedSelectorList>> {+ pub(crate) fn selectors(self) -> Option<RustParsedSelectorList> {match self {Self::Materialized(rule) => match &rule.payload {RulePayload::Style(style) => Some(style.selectors()),diff --git a/Libraries/LibWeb/Rust/src/css/scope_selectors.rs b/Libraries/LibWeb/Rust/src/css/scope_selectors.rsindex 1ad2c9737b0..c803d1947e8 100644--- a/Libraries/LibWeb/Rust/src/css/scope_selectors.rs+++ b/Libraries/LibWeb/Rust/src/css/scope_selectors.rs@@ -5,11 +5,10 @@*/use crate::css::selector_parser::RustParsedSelectorList;-use std::sync::Arc;pub struct ScopeSelectors {- pub(crate) start: Option<Arc<RustParsedSelectorList>>,- pub(crate) end: Option<Arc<RustParsedSelectorList>>,+ pub(crate) start: Option<RustParsedSelectorList>,+ pub(crate) end: Option<RustParsedSelectorList>,}const _: () = {diff --git a/Libraries/LibWeb/Rust/src/css/selector_arena.rs b/Libraries/LibWeb/Rust/src/css/selector_arena.rsindex 0e5b0038e3d..5da9628c1fb 100644--- a/Libraries/LibWeb/Rust/src/css/selector_arena.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_arena.rs@@ -2601,6 +2601,14 @@ impl PartialEq for SelectorArena {}}+impl SelectorListRef {+ /// Whether both are the same list of the same arena, not only equal ones.+ #[cfg(test)]+ pub(crate) fn is_same_list(&self, other: &Self) -> bool {+ Arc::ptr_eq(&self.arena, &other.arena) && self.start == other.start+ }+}+impl PartialEq for SelectorListRef {fn eq(&self, other: &Self) -> bool {self.start == other.startdiff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rsindex 8bb29b18b62..c69934015a1 100644--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs@@ -1498,6 +1498,12 @@ const _: fn() = || {};impl RustParsedSelectorList {+ /// Whether both are the same list of the same arena, not only equal ones.+ #[cfg(test)]+ pub(crate) fn is_same_list(&self, other: &Self) -> bool {+ self.selectors.is_same_list(&other.selectors)+ }+#[cfg(test)]pub(crate) fn selectors(&self) -> &SelectorListRef {&self.selectorsdiff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rsindex 2df54beee0e..8790294572c 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs@@ -271,7 +271,7 @@ pub(super) fn add_sheet_with_origin(engine: &mut StyleEngine, scope: TreeScopeID/// A style rule parsed as the host parses a sheet's: its selectors and its declarations.pub(super) struct ParsedRule {- selectors: Arc<crate::css::selector_parser::RustParsedSelectorList>,+ selectors: crate::css::selector_parser::RustParsedSelectorList,pub(super) declarations: crate::css::declaration_block::SharedDeclarationBlock,}diff --git a/Libraries/LibWeb/Rust/src/css/style/rule_writes.rs b/Libraries/LibWeb/Rust/src/css/style/rule_writes.rsindex 104ad285202..04f36449909 100644--- a/Libraries/LibWeb/Rust/src/css/style/rule_writes.rs+++ b/Libraries/LibWeb/Rust/src/css/style/rule_writes.rs@@ -101,7 +101,7 @@ fn bit(identity: u64) -> u128 {/// A style rule's bound selectors and the scopes it is inside.pub(crate) struct BoundSelectors {- pub(crate) selectors: Arc<RustParsedSelectorList>,+ pub(crate) selectors: RustParsedSelectorList,pub(crate) scope: Arc<BoundScopeChain>,}diff --git a/Libraries/LibWeb/Rust/src/css/style_rule.rs b/Libraries/LibWeb/Rust/src/css/style_rule.rsindex 90cd04ac206..c2bbe16f460 100644--- a/Libraries/LibWeb/Rust/src/css/style_rule.rs+++ b/Libraries/LibWeb/Rust/src/css/style_rule.rs@@ -10,26 +10,30 @@ use crate::css::selector_parser::{RustParsedSelectorList, SelectorParsingMode, SelectorType, StyleNestingParent, parse_selector_list,};use std::cell::RefCell;-use std::sync::Arc;pub(crate) struct StyleRule {- selectors: RefCell<Arc<RustParsedSelectorList>>,+ selectors: RefCell<RustParsedSelectorList>,pub(crate) declarations: DeclarationBlock,}impl StyleRule {- pub(crate) fn new(selectors: Arc<RustParsedSelectorList>, declarations: SharedDeclarationBlock) -> Self {+ pub(crate) fn new(selectors: RustParsedSelectorList, declarations: SharedDeclarationBlock) -> Self {Self {selectors: RefCell::new(selectors),declarations: DeclarationBlock::new(declarations),}}- pub(crate) fn selectors(&self) -> Arc<RustParsedSelectorList> {+ pub(crate) fn selectors(&self) -> RustParsedSelectorList {self.selectors.borrow().clone()}- pub(crate) fn replace_selectors(&self, selectors: Arc<RustParsedSelectorList>) {+ /// The selectors, which live until they are next replaced.+ pub(crate) fn selectors_pointer(&self) -> *const RustParsedSelectorList {+ self.selectors.as_ptr()+ }++ pub(crate) fn replace_selectors(&self, selectors: RustParsedSelectorList) {*self.selectors.borrow_mut() = selectors;}@@ -48,11 +52,9 @@ impl StyleRule {let Ok(selectors) = parse_selector_list(input, namespaces, selector_type, SelectorParsingMode::Standard) else {return false;};- *self.selectors.borrow_mut() = Arc::new(- selectors- .adapt_for_nesting(nesting_parent)- .into_list_declaring(nesting_parent, declarations),- );+ *self.selectors.borrow_mut() = selectors+ .adapt_for_nesting(nesting_parent)+ .into_list_declaring(nesting_parent, declarations);true}}@@ -83,7 +85,7 @@ mod tests {let rule = &rules.rule_at(0);let style = rule.style_rule();assert!(style.declarations.is_immutable());- assert!(Arc::ptr_eq(&style.selectors(), &style.selectors.borrow()));+ assert!(style.selectors().is_same_list(&style.selectors.borrow()));let declarations = rule.cascade_declarations().unwrap();assert!(SharedDeclarationBlock::ptr_eq(&declarations,@@ -238,7 +240,7 @@ mod tests {Default::default(),StyleNestingParent::None));- assert!(Arc::ptr_eq(&style.selectors(), &retained.selectors()));- assert!(!Arc::ptr_eq(&style.selectors(), &fork.selectors()));+ assert!(style.selectors().is_same_list(&retained.selectors()));+ assert!(!style.selectors().is_same_list(&fork.selectors()));}}