Skip to content

Truly independent web browser

LibWeb: Build the value of a CSSOM shorthand in Rust

Andreas Kling12e784a1129e
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
index f0bebbb3d80..d3fc065b8d0 100644
--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
@@ -21,19 +21,10 @@
#include <LibWeb/CSS/StyleEngineInput.h>
#include <LibWeb/CSS/StyleSheetInvalidation.h>
#include <LibWeb/CSS/StyleSheetState.h>
-#include <LibWeb/CSS/StyleValues/ColorFunctionStyleValue.h>
#include <LibWeb/CSS/StyleValues/FilterStyleValue.h>
-#include <LibWeb/CSS/StyleValues/FunctionStyleValue.h>
-#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
-#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
-#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/PendingSubstitutionStyleValue.h>
-#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
-#include <LibWeb/CSS/StyleValues/StyleValueList.h>
-#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
-#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
#include <LibWeb/CSS/Zoom.h>
#include <LibWeb/DOM/Document.h>
@@ -432,11 +423,11 @@ Optional<StyleProperty> CSSStyleProperties::get_property_internal(PropertyNameAn
if (property_is_shorthand(property.id())) {
// 1. Let list be a new empty array.
Vector<ValueComparingNonnullRefPtr<StyleValue const>> list;
+ Vector<void const*> longhand_values;
Optional<Important> last_important_flag;
// 2. For each longhand property longhand that property maps to, in canonical order, follow these substeps:
- Vector<PropertyID> longhand_ids = longhands_for_shorthand(property.id());
- for (auto longhand_property_id : longhand_ids) {
+ for (auto longhand_property_id : longhands_for_shorthand(property.id())) {
// 1. If longhand is a case-sensitive match for a property name of a CSS declaration in the declarations,
// let declaration be that CSS declaration, or null otherwise.
auto declaration = get_property_internal(PropertyNameAndID::from_id(longhand_property_id));
@@ -446,6 +437,7 @@ Optional<StyleProperty> CSSStyleProperties::get_property_internal(PropertyNameAn
return {};
// 3. Append the declaration to list.
+ longhand_values.append(declaration->value->rust_style_value_data());
list.append(declaration->value);
if (last_important_flag.has_value() && declaration->important != *last_important_flag)
@@ -453,48 +445,14 @@ Optional<StyleProperty> CSSStyleProperties::get_property_internal(PropertyNameAn
last_important_flag = declaration->important;
}
- // https://drafts.csswg.org/css-values-5/#pending-substitution-value
- // If all of the component longhand properties for a given shorthand are pending-substitution values from
- // the same original shorthand value, the shorthand property must serialize to that original
- // (arbitrary substitution function-containing) value.
- // Otherwise, if any of the component longhand properties for a given shorthand are pending-substitution
- // values, or contain arbitrary substitution functions of their own that have not yet been substituted, the
- // shorthand property must serialize to the empty string.
- if (list.first()->is_pending_substitution()) {
- auto original_shorthand_value = list.first()->as_pending_substitution().original_shorthand_value();
- // NB: A longhand of a shorthand nested in the one written pends the nested shorthand, which pends the
- // written one in turn. Writing an equal value again leaves longhands holding equal values, not
- // the same one, so what they have in common is the shorthand written and its value as written.
- auto written_shorthand = [](ValueComparingNonnullRefPtr<StyleValue const> value) {
- while (value->as_pending_substitution().original_shorthand_value()->is_pending_substitution())
- value = value->as_pending_substitution().original_shorthand_value();
- return value;
- };
- auto const first_written_shorthand = written_shorthand(list.first());
- auto all_from_same_original = all_of(list, [&](auto const& value) {
- if (!value->is_pending_substitution())
- return false;
- auto const this_written_shorthand = written_shorthand(value);
- return this_written_shorthand->as_pending_substitution().original_shorthand() == first_written_shorthand->as_pending_substitution().original_shorthand()
- && this_written_shorthand->as_pending_substitution().original_shorthand_value() == first_written_shorthand->as_pending_substitution().original_shorthand_value();
- });
- if (all_from_same_original) {
- return StyleProperty {
- .important = last_important_flag.value(),
- .property_id = property.id(),
- .value = original_shorthand_value,
- };
- }
- }
- if (any_of(list, [](auto const& value) { return value->is_pending_substitution() || value->is_unresolved(); }))
- return {};
-
// 3. If important flags of all declarations in list are same, then return the serialization of list.
- // NOTE: Currently we implement property-specific shorthand serialization in ShorthandStyleValue::to_string().
+ auto const* shorthand_value = Parser::ValueParserFFI::rust_shorthand_value_of_longhands(to_underlying(property.id()), longhand_values.data(), longhand_values.size());
+ if (!shorthand_value)
+ return {};
return StyleProperty {
.important = last_important_flag.value(),
.property_id = property.id(),
- .value = ShorthandStyleValue::create(property.id(), longhand_ids, list),
+ .value = StyleValue::adopt_rust_style_value_data(static_cast<StyleValueFFI::StyleValueData const*>(shorthand_value)),
};
// 4. Return the empty string.
diff --git a/Libraries/LibWeb/Rust/src/css/declaration_block.rs b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
index 9721d37a41f..eb43c0e0b78 100644
--- a/Libraries/LibWeb/Rust/src/css/declaration_block.rs
+++ b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
@@ -1133,6 +1133,25 @@ pub extern "C" fn rust_declaration_block_visit(
rust_declaration_data_visit(&block.data(), context, visit);
}
+/// The value of the shorthand whose longhands, in canonical order, have the values, or null if it serializes to the
+/// empty string.
+///
+/// # Safety
+/// `values` must point to `count` live style values.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_shorthand_value_of_longhands(
+ shorthand: u16,
+ values: *const *const c_void,
+ count: usize,
+) -> *const c_void {
+ let values = unsafe { ak::slice_from_raw_parts(values, count) }
+ .iter()
+ .map(|&value| RetainedStyleValueData::from_arc(unsafe { retain_value(value) }))
+ .collect();
+ crate::css::serialize::shorthand_value_of_longhands(shorthand, values)
+ .map_or(std::ptr::null(), |value| StyleValueArc::into_raw(value).cast())
+}
+
/// The declarations that the declaration stands for: itself for a longhand, and otherwise its longhands.
unsafe fn declarations_expanding_shorthands(property: &FfiDeclaredProperty) -> Vec<DeclaredProperty> {
let important = property.important;
diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rs
index da297929213..c0498f0eb85 100644
--- a/Libraries/LibWeb/Rust/src/css/serialize.rs
+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs
@@ -28,7 +28,7 @@ mod shorthand;
use crate::css::css_enums::keyword;
use crate::css::css_tokenizer::TokenizerInput;
use crate::css::parser::component_value::{ComponentSerializationMode, serialize_component_values_into};
-pub(crate) use declarations::serialize_a_css_declaration_block;
+pub(crate) use declarations::{serialize_a_css_declaration_block, shorthand_value_of_longhands};
use crate::css::style_value::{
BasicShapeData, COUNTER_FUNCTION_COUNTERS, RetainedColorStopList, RetainedString, StyleValueData,
diff --git a/Libraries/LibWeb/Rust/src/css/serialize/declarations.rs b/Libraries/LibWeb/Rust/src/css/serialize/declarations.rs
index 6b7ae5e8fc9..5669ca15ecd 100644
--- a/Libraries/LibWeb/Rust/src/css/serialize/declarations.rs
+++ b/Libraries/LibWeb/Rust/src/css/serialize/declarations.rs
@@ -71,9 +71,74 @@ fn serialize_a_css_declaration(list: &mut Vec<TextSink>, property: &StringUnits,
list.push(s);
}
+fn shorthand_value(shorthand: u16, longhand_values: Vec<RetainedStyleValueData>) -> StyleValueArc {
+ StyleValueArc::new(StyleValueData::Shorthand(StyleValueBox::new(ShorthandPayload {
+ shorthand_property: shorthand,
+ sub_properties: RetainedPropertyIdList::from_property_ids(longhands_for_shorthand(shorthand).to_vec()),
+ values: RetainedStyleValueDataList::from_retained_values(longhand_values),
+ })))
+}
+
+/// Whether the value of a longhand still waits for an arbitrary substitution function.
+fn awaits_substitution(value: &StyleValueData) -> bool {
+ matches!(
+ value,
+ StyleValueData::PendingSubstitution { .. } | StyleValueData::Unresolved(_)
+ )
+}
+
+/// The shorthand and the value that the shorthand was written with, which a longhand that pends it holds. A longhand of
+/// a shorthand nested in the one written pends the nested shorthand, which pends the written one in turn.
+fn written_shorthand(value: &RetainedStyleValueData) -> Option<(u16, &RetainedStyleValueData)> {
+ let StyleValueData::PendingSubstitution {
+ original_shorthand,
+ original_shorthand_value,
+ } = value.data()
+ else {
+ return None;
+ };
+ written_shorthand(original_shorthand_value).or(Some((*original_shorthand, original_shorthand_value)))
+}
+
+/// The value of the shorthand whose longhands, in canonical order, have the values, or None if it serializes to the
+/// empty string.
+pub(crate) fn shorthand_value_of_longhands(
+ shorthand: u16,
+ longhand_values: Vec<RetainedStyleValueData>,
+) -> Option<StyleValueArc> {
+ // https://drafts.csswg.org/css-values-5/#pending-substitution-value
+ // If all of the component longhand properties for a given shorthand are pending-substitution values from the same
+ // original shorthand value, the shorthand property must serialize to that original (arbitrary substitution
+ // function-containing) value.
+ // NB: Writing an equal value again leaves longhands holding equal values, not the same one, so what they have in
+ // common is the shorthand written and its value as written.
+ let first = longhand_values.first()?;
+ if let StyleValueData::PendingSubstitution {
+ original_shorthand_value,
+ ..
+ } = first.data()
+ {
+ let first_written_shorthand = written_shorthand(first);
+ if longhand_values
+ .iter()
+ .all(|value| written_shorthand(value).is_some() && written_shorthand(value) == first_written_shorthand)
+ {
+ return Some(original_shorthand_value.clone().into_arc());
+ }
+ }
+
+ // Otherwise, if any of the component longhand properties for a given shorthand are pending-substitution values, or
+ // contain arbitrary substitution functions of their own that have not yet been substituted, the shorthand property
+ // must serialize to the empty string.
+ if longhand_values.iter().any(|value| awaits_substitution(value.data())) {
+ return None;
+ }
+
+ Some(shorthand_value(shorthand, longhand_values))
+}
+
fn make_shorthand_value(shorthand: u16, list: &[DeclarationRef]) -> StyleValueArc {
- let longhand_ids = longhands_for_shorthand(shorthand);
- let longhand_values = longhand_ids
+ let longhand_values = longhands_for_shorthand(shorthand)
.iter()
.map(|&longhand_id| {
RetainedStyleValueData::from_arc(if property_is_shorthand(longhand_id) {
@@ -87,11 +152,7 @@ fn make_shorthand_value(shorthand: u16, list: &[DeclarationRef]) -> StyleValueAr
})
})
.collect();
- StyleValueArc::new(StyleValueData::Shorthand(StyleValueBox::new(ShorthandPayload {
- shorthand_property: shorthand,
- sub_properties: RetainedPropertyIdList::from_property_ids(longhand_ids.to_vec()),
- values: RetainedStyleValueDataList::from_retained_values(longhand_values),
- })))
+ shorthand_value(shorthand, longhand_values)
}
// https://www.w3.org/TR/cssom/#serialize-a-css-value
@@ -129,12 +190,7 @@ fn serialize_a_css_value_of_list(list: &[DeclarationRef]) -> TextSink {
// If any of the component longhand properties for a given shorthand are pending-substitution
// values, or contain arbitrary substitution functions of their own that have not yet been substituted, the
// shorthand property must serialize to the empty string.
- if list.iter().any(|declaration| {
- matches!(
- &**declaration.value,
- StyleValueData::PendingSubstitution { .. } | StyleValueData::Unresolved(_)
- )
- }) {
+ if list.iter().any(|declaration| awaits_substitution(declaration.value)) {
return value;
}