Skip to content

Truly independent web browser

LibWeb: Share the initial values a CSS shorthand leaves out

Andreas Klingbfe00bd58b21
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index bc889bc3a90..5bd73ea33d9 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -4078,13 +4078,30 @@ pub(crate) fn parse_initial_value(property: u16) -> Option<StyleValueArc> {
}
}
-pub(crate) fn parse_initial_longhand(context: &ParseContext, property: u16) -> Option<StyleValueData> {
- let initial = property_initial_value(property);
- let values = consume_a_list_of_component_values(tokenize_for_parser(initial.as_bytes())).ok()?;
- match parse_css_value(context, property, &values) {
- ParseOutcome::Parsed(value) => Some((*value).clone()),
- ParseOutcome::Invalid | ParseOutcome::NotHandled => None,
+pub(crate) fn parse_initial_longhand(_: &ParseContext, property: u16) -> Option<StyleValueData> {
+ initial_longhand_value(property).map(|value| (*value).clone())
+}
+
+/// The initial value of the longhand `property`, which is parsed once for each thread, as shorthands that leave a
+/// longhand out ask for it each time. It is parsed outside any document, as every context then shares it.
+fn initial_longhand_value(property: u16) -> Option<StyleValueArc> {
+ thread_local! {
+ static INITIAL_VALUES: std::cell::RefCell<std::collections::HashMap<u16, Option<StyleValueArc>>> =
+ std::cell::RefCell::new(std::collections::HashMap::new());
}
+ if let Some(value) = INITIAL_VALUES.with(|cache| cache.borrow().get(&property).cloned()) {
+ return value;
+ }
+ let initial = property_initial_value(property);
+ let context = ParseContext::default();
+ let value = consume_a_list_of_component_values(tokenize_for_parser(initial.as_bytes()))
+ .ok()
+ .and_then(|values| match parse_css_value(&context, property, &values) {
+ ParseOutcome::Parsed(value) => Some(value),
+ ParseOutcome::Invalid | ParseOutcome::NotHandled => None,
+ });
+ INITIAL_VALUES.with(|cache| cache.borrow_mut().insert(property, value.clone()));
+ value
}
pub(crate) fn shorthand_value(property: u16, sub_properties: Vec<u16>, values: Vec<StyleValueData>) -> StyleValueData {
@@ -5749,11 +5766,11 @@ fn parse_mask_shorthand(context: &ParseContext, values: &[ComponentValue]) -> Pa
)))
}
-fn coordinating_initial_item(context: &ParseContext, property: u16) -> Option<StyleValueData> {
- let initial = parse_initial_longhand(context, property)?;
- match initial {
+fn coordinating_initial_item(_: &ParseContext, property: u16) -> Option<StyleValueData> {
+ let initial = initial_longhand_value(property)?;
+ match &*initial {
StyleValueData::ValueList { values, .. } => values.as_slice().first()?.optional_data().cloned(),
- value => Some(value),
+ value => Some(value.clone()),
}
}
@@ -5905,16 +5922,20 @@ fn parse_animation_transition_shorthand(
let mut sub_properties = parsed_longhands.to_vec();
let mut output = coordinated
.into_iter()
- .map(|values| value_list(values, 1, true))
+ .map(|values| RetainedStyleValueData::from_owned(value_list(values, 1, true)))
.collect::<Vec<_>>();
for &reset_only in reset_only {
sub_properties.push(reset_only);
- let Some(initial) = parse_initial_longhand(context, reset_only) else {
+ let Some(initial) = initial_longhand_value(reset_only) else {
return ParseOutcome::Invalid;
};
- output.push(initial);
+ output.push(RetainedStyleValueData::from_arc(initial));
}
- ParseOutcome::Parsed(shared_style_value(shorthand_value(property, sub_properties, output)))
+ ParseOutcome::Parsed(shared_style_value(retained_shorthand_value(
+ property,
+ sub_properties,
+ output,
+ )))
}
pub(crate) fn unresolved_value_from_string(
diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.py
index f5cf9ee65e7..f3620163f06 100755
--- a/Meta/Linters/check_rust_process_wide_state.py
+++ b/Meta/Linters/check_rust_process_wide_state.py
@@ -128,6 +128,7 @@ RENDER_STATE_ALLOWED = {
"per-thread list of what the thread let go of in its hold of the render owner's slot, dropped as the hold ends",
["render_state/owner.rs:DROPPED_AFTER_HOLD"],
),
+ f"{RENDER_STATE_CRATE}/src/css/parser/value_parser.rs:INITIAL_VALUES": "per-thread cache of the initial values of longhands, parsed outside any document; every thread parses the same values",
f"{RENDER_STATE_CRATE}/src/render_state.rs:STACK_BASE": "per-thread cache of the thread's own stack bounds",
f"{RENDER_STATE_CRATE}/src/layout/parallel_layout.rs:SUBTREE_LEASE_OF_THIS_THREAD": "per-thread lease of the subtree the layout task running on the thread may write, held only while LIBWEB_VERIFY_LAYOUT_FORK_SCOPE checks it and only for that task",
f"{RENDER_STATE_CRATE}/src/layout/layout_pass.rs:WORKER_SCRATCH_OF_THIS_THREAD": "per-thread spare layout buffers and the fragment identities the thread reserved; a thread without one only allocates more and reserves another range",