Truly independent web browser
LibWeb: Decide how a declared value computes as it is written
Andreas Kling3d6ed3a036e1
diff --git a/Libraries/LibWeb/Rust/src/css/style/element_declarations.rs b/Libraries/LibWeb/Rust/src/css/style/element_declarations.rsindex 47f38a8378c..f198325701b 100644--- a/Libraries/LibWeb/Rust/src/css/style/element_declarations.rs+++ b/Libraries/LibWeb/Rust/src/css/style/element_declarations.rs@@ -28,6 +28,19 @@ pub struct ElementDeclarations<'a> {}impl<'a> ElementDeclarations<'a> {+ /// A block of declarations of its parts, parallel to one another.+ pub(in crate::css::style) fn of_parts(+ declared: &'a [DeclarationEntry],+ written: &'a [RetainedStyleValueData],+ checks: &'a [Checks],+ ) -> Self {+ Self {+ declared,+ written,+ checks,+ }+ }+/// The value the declaration at `index` of `declared` was written with.#[must_use]pub fn written(self, index: usize) -> &'a RetainedStyleValueData {diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rsindex a2903f435b6..7a8f5868b27 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs@@ -345,7 +345,7 @@ impl RetainedState {// A logical alias computes as its physical counterpart for the element's writing mode and// direction, which an element declaring neither has from its parent.let parent_flow =- match winners+ || match winners.iter().any(|winner| winner.property == prop::WRITING_MODE || winner.property == prop::DIRECTION){@@ -366,6 +366,7 @@ impl RetainedState {&& !pseudo_kind::is_highlight(target_pseudo_kind)&& parent_longhands.is_some_and(|longhands| longhands.effective_zoom() == 1.0);let mut plain_dependencies = takes_no_drive.then_some(0_u32);+ let mut plain_block = None;for &Winner {property,source,@@ -376,34 +377,49 @@ impl RetainedState {let Some(dependencies) = plain_dependencies else {break;};- let (declaration, written, checks) = self.declaration_of(&element_declarations, source, index);- let prepared = checks.dependencies();- let value = match written.data() {- data @ StyleValueData::Shorthand { .. } if !checks.waits_on_substitution() => {- shorthand_longhand_data(property, data).expect("the style engine answers every row")- }- _ => written,- };- let is_plain = !checks.waits_on_substitution()- && declaration.operator() == CascadeOperator::Declared- && prepared & winner_store::DEPENDENCIES_COMPUTED != 0- && table_row_bits(property) & LOGICAL_ALIAS_BIT == 0- && moved_longhand_groups(property) & (1 << group_index::FONT) == 0- && value.optional_data().is_some_and(|data| {- crate::css::style_compute::value_computes_directly(- property,- data,- Some(winner_store::unpack_dependencies(prepared)),- )- });- if !is_plain {+ // NB: A block's winners come one after another.+ if plain_block.is_none_or(|(held, _)| held != source) {+ plain_block = Some((+ source,+ match source {+ WinnerSource::Rule(rule) => {+ crate::css::style::element_declarations::ElementDeclarations::of_parts(+ self.rules.declared_properties_of(rule),+ self.rules.written_values_of(rule),+ self.rules.written_value_checks_of(rule),+ )+ }+ WinnerSource::Element(kind) => {+ let (_, declarations) = element_declarations+ .iter()+ .flatten()+ .find(|(declarations_kind, _)| *declarations_kind == kind)+ .expect("the style engine answers every row");+ *declarations+ }+ },+ ));+ }+ let (_, block) = plain_block.expect("the block was just taken");+ let checks = block.checks(index);+ let declaration = block+ .declared_property(index)+ .expect("the style engine answers every row");+ let written = block.written(index);+ if !checks.is_plain() || declaration.operator() != CascadeOperator::Declared {plain_dependencies = None;break;}+ let prepared = checks.dependencies();+ let value = match checks.is_shorthand() {+ true => shorthand_longhand_data(property, written.data()).expect("the style engine answers every row"),+ false => written,+ };easy.push((property,std::borrow::Cow::Borrowed(value),Some(winner_store::unpack_dependencies(prepared)),+ checks.known_computation(),));direct_inheritance.push((property, false));environment_reads |= property_environment_reads(property);@@ -589,7 +605,7 @@ impl RetainedState {let mut inherited_property = property;let keyword_value = reads.css_wide_keyword.and_then(|keyword| {if is_logical_alias {- let (writing_mode, direction) = parent_flow?;+ let (writing_mode, direction) = parent_flow()?;inherited_property = map_logical_alias_to_physical(property, writing_mode, direction);}let inherits = match keyword {@@ -632,7 +648,12 @@ impl RetainedState {.optional_data().is_some_and(|data| crate::css::style_compute::font_value_computes_natively(property, data));match (computation, keyword_value) {- (_, Some(value)) => easy.push((property, std::borrow::Cow::Owned(value), Some(Default::default()))),+ (_, Some(value)) => easy.push((+ property,+ std::borrow::Cow::Owned(value),+ Some(Default::default()),+ Default::default(),+ )),(WinnerComputation::Direct(dependencies), None) => easy.push((property,match &value {@@ -640,6 +661,7 @@ impl RetainedState {_ => std::borrow::Cow::Owned(written.clone()),},dependencies,+ Default::default(),)),(WinnerComputation::Full, None) if font_computes_natively => native_font.push((property,@@ -929,7 +951,7 @@ impl RetainedState {&& !crate::css::style_compute::compute_declared_values(&mut writer,easy.iter()- .map(|(property, value, dependencies)| (*property, &**value, *dependencies)),+ .map(|(property, value, dependencies, known)| (*property, &**value, *dependencies, *known)),&length,scheme,&box_type_input,@@ -999,7 +1021,7 @@ impl RetainedState {if !crate::css::style_compute::compute_declared_values(&mut writer,easy.iter()- .map(|(property, value, dependencies)| (*property, &**value, *dependencies)),+ .map(|(property, value, dependencies, known)| (*property, &**value, *dependencies, *known)),&length,scheme,&box_type_input,@@ -1997,6 +2019,7 @@ type DirectWinner<'a> = (u16,std::borrow::Cow<'a, RetainedStyleValueData>,Option<crate::css::style_compute::ExternalValueDependencies>,+ crate::css::style_compute::KnownComputation,);/// The engine: what an element's group values start from.@@ -2451,7 +2474,11 @@ const _: () = assert!(winner_store::DEPENDENCY_BITS& (WrittenValueChecks::WHOLE_CONTEXT_FREE| WrittenValueChecks::LONGHAND_CONTEXT_FREE- | WrittenValueChecks::WAITS_ON_SUBSTITUTION)+ | WrittenValueChecks::WAITS_ON_SUBSTITUTION+ | WrittenValueChecks::IS_SHORTHAND+ | WrittenValueChecks::PLAIN+ | WrittenValueChecks::COMPUTES_TO_ITSELF+ | WrittenValueChecks::IS_INITIAL)== 0);@@ -2459,6 +2486,30 @@ impl WrittenValueChecks {const WHOLE_CONTEXT_FREE: u32 = 1 << 21;const LONGHAND_CONTEXT_FREE: u32 = 1 << 22;const WAITS_ON_SUBSTITUTION: u32 = 1 << 23;+ const IS_SHORTHAND: u32 = 1 << 24;+ const PLAIN: u32 = 1 << 25;+ const COMPUTES_TO_ITSELF: u32 = 1 << 26;+ const IS_INITIAL: u32 = 1 << 27;++ /// Whether the value is a shorthand declared whole, of which a longhand computes from its part.+ fn is_shorthand(self) -> bool {+ self.0 & Self::IS_SHORTHAND != 0+ }++ /// Whether the longhand computes from this value, written as a declared value, by having it+ /// absolutized against the element's context, with what it computes against told by these+ /// checks: no drive, no font resolution and no logical mapping have anything to do with it.+ fn is_plain(self) -> bool {+ self.0 & Self::PLAIN != 0+ }++ /// What is known of how the longhand computes from the value where nothing zooms it.+ fn known_computation(self) -> crate::css::style_compute::KnownComputation {+ crate::css::style_compute::KnownComputation {+ to_itself: self.0 & Self::COMPUTES_TO_ITSELF != 0,+ is_initial: self.0 & Self::IS_INITIAL != 0,+ }+ }/// The packed external dependencies of the value, when they were prepared.pub(in crate::css::style) fn dependencies(self) -> u32 {@@ -2504,6 +2555,43 @@ impl WrittenValueChecks {Some(true) => bits |= Self::LONGHAND_CONTEXT_FREE,Some(false) => {}}+ if matches!(value.data(), StyleValueData::Shorthand { .. }) {+ bits |= Self::IS_SHORTHAND;+ }+ let shorthand_part;+ let longhand_value = match bits & (Self::IS_SHORTHAND | Self::WAITS_ON_SUBSTITUTION) {+ 0 => Some(value),+ Self::IS_SHORTHAND => {+ shorthand_part = shorthand_longhand_value(property, value.data());+ shorthand_part.as_ref()+ }+ _ => None,+ };+ if let Some(longhand_value) = longhand_value+ && dependencies & winner_store::DEPENDENCIES_COMPUTED != 0+ {+ use crate::css::style_compute::{+ LOGICAL_ALIAS_BIT, table_row_bits, value_absolutization_is_identity_where_nothing_zooms,+ value_computes_directly,+ };+ let unpacked = winner_store::unpack_dependencies(dependencies);+ if table_row_bits(property) & LOGICAL_ALIAS_BIT == 0+ && moved_longhand_groups(property) & (1 << crate::css::table_group_builder::group_index::FONT) == 0+ && value_computes_directly(property, longhand_value.data(), Some(unpacked))+ {+ bits |= Self::PLAIN;+ }+ if value_absolutization_is_identity_where_nothing_zooms(longhand_value.data()) {+ bits |= Self::COMPUTES_TO_ITSELF;+ }+ if longhand_value.pointer() == crate::css::style_compute::initial_value_data(property)+ && !crate::css::property_metadata::property_is_inherited(property)+ && table_row_bits(property) == 0+ && !unpacked.inheritance_dependent+ {+ bits |= Self::IS_INITIAL;+ }+ }Self(bits)}}diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/rule_store.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/rule_store.rsindex 1f952ef4a9a..37b41934011 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/rule_store.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/rule_store.rs@@ -1344,6 +1344,14 @@ impl RuleStore {.is_some_and(|data| data.may_affect_layout_geometry)}+ /// The checks of the values the rule's declarations were written with, parallel to them.+ pub(in crate::css::style) fn written_value_checks_of(+ &self,+ rule: RuleID,+ ) -> &[super::pipeline::compute::WrittenValueChecks] {+ self.cascade_data_of(rule).map_or(&[], |data| data.checks())+ }+pub(in crate::css::style) fn written_value_checks(&self,rule: RuleID,diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rsindex 0c5b0bfa655..9f35902cb69 100644--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs@@ -3418,12 +3418,27 @@ pub(crate) fn value_absolutization_is_identity(value: &StyleValueData) -> bool {}}+/// What is known of how a declared value computes from how it was written, for the winners it is+/// known of.+#[derive(Clone, Copy, Default)]+pub(crate) struct KnownComputation {+ /// The value computes to itself where nothing zooms it.+ pub(crate) to_itself: bool,+ /// The value is the initial value of a longhand that does not inherit, and depends on nothing.+ pub(crate) is_initial: bool,+}+/// Whether `value` computes to itself against the length context `length`, without looking any-/// further into it: a length in pixels where nothing zooms it, or a value whose absolutization is-/// the identity.+/// further into it.fn value_computes_to_itself(value: &StyleValueData, length: &FfiLengthResolutionContext) -> bool {+ length.zoom == 1.0 && value_absolutization_is_identity_where_nothing_zooms(value)+}++/// Whether `value` computes to itself where nothing zooms it: a length in pixels, or a value whose+/// absolutization is the identity.+pub(crate) fn value_absolutization_is_identity_where_nothing_zooms(value: &StyleValueData) -> bool {match value {- StyleValueData::Length { unit, .. } => *unit == px_length_unit() && length.zoom == 1.0,+ StyleValueData::Length { unit, .. } => *unit == px_length_unit(),_ => {value_absolutization_is_identity(value)|| crate::css::absolutize::color_function_is_canonical_legacy_rgb(value)@@ -5646,7 +5661,14 @@ pub(crate) type AbsolutizedValues = crate::fast_hash::FastMap<#[allow(clippy::too_many_arguments)]pub(crate) fn compute_declared_values<'a>(values: &mut GroupValuesWriter<'_>,- winners: impl Iterator<Item = (u16, &'a RetainedStyleValueData, Option<ExternalValueDependencies>)>,+ winners: impl Iterator<+ Item = (+ u16,+ &'a RetainedStyleValueData,+ Option<ExternalValueDependencies>,+ KnownComputation,+ ),+ >,length: &FfiLengthResolutionContext,scheme: Option<u8>,box_type_input: &FfiBoxTypeTransformationInput,@@ -5689,7 +5711,7 @@ pub(crate) fn compute_declared_values<'a>(style_sheet_resource_context: None,current_color: None,};- for (property_id, value, prepared_dependencies) in winners {+ for (property_id, value, prepared_dependencies, known) in winners {let Some(data) = value.optional_data() else {return false;};@@ -5697,13 +5719,7 @@ pub(crate) fn compute_declared_values<'a>(debug_assert!(value_computes_directly(property_id, data, prepared_dependencies));// A longhand that does not inherit, declared with the very value its initial value is, holds// what the element's seed does: its initial value computed. Nothing computes against it.- if value.pointer() == initial_value_data(property_id)- && !is_decided(&decided, property_id)- && !was_driven(property_id)- && !crate::css::property_metadata::property_is_inherited(property_id)- && table_row_bits(property_id) == 0- && prepared_dependencies.is_some_and(|dependencies| !dependencies.inheritance_dependent)- {+ if known.is_initial && !is_decided(&decided, property_id) && !was_driven(property_id) {let (word, bit) = longhand_bit(property_id);decided[word] |= bit;values.flags_mut().mark_evaluated(property_id);@@ -5724,7 +5740,7 @@ pub(crate) fn compute_declared_values<'a>(} else {&context};- let mut computed = if value_computes_to_itself(data, length) {+ let mut computed = if (known.to_itself && length.zoom == 1.0) || value_computes_to_itself(data, length) {std::borrow::Cow::Borrowed(value)} else {// The color property absolutizes against the inherited color, which no key holds.