dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_values / shorthand_style_value_1.lucb
#==============================================================================================
#
# shorthand_style_value_1 - ShorthandStyleValue: a shorthand's longhand values, and serializing them
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp (lines 1-193, 864-991) and the
# inline members of ShorthandStyleValue.h (lines 1-48). serialize is one switch in C++; each
# case with more than a few lines is a function of its own in shorthand_style_value_2, in the
# switch's order, and the lambdas are functions named after them.
#
# Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
# Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: ShorthandStyleValue =====================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h:15 Web::CSS::ShorthandStyleValue::create.
pub func css_shorthand_style_value_create(shorthand: css_data.PropertyId, sub_properties: ak.Vector[css_data.PropertyId], values: ak.Vector[CssStyleValue*]) -> CssShorthandStyleValue*:
let this = &(new CssShorthandStyleValue[1] --- else trap("out of memory"))[0]
css_shorthand_style_value_construct(this, shorthand, sub_properties, values)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp:23 Web::CSS::ShorthandStyleValue::ShorthandStyleValue.
pub func css_shorthand_style_value_construct(this: CssShorthandStyleValue*, shorthand: css_data.PropertyId, sub_properties: ak.Vector[css_data.PropertyId], values: ak.Vector[CssStyleValue*]):
css_style_value_construct(this.css_style_value(), CssStyleValueType.shorthand)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_shorthand_style_value_vtable, &css_shorthand_style_value_class)
this.m_properties = CssShorthandStyleValueProperties(shorthand_property = shorthand, sub_properties = sub_properties, values = values)
if ak.vector_size(&this.m_properties.sub_properties) != ak.vector_size(&this.m_properties.values):
ak.dbgln(f"ShorthandStyleValue: sub_properties and values must be the same size! {ak.vector_size(&this.m_properties.sub_properties)} != {ak.vector_size(&this.m_properties.values)}")
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h:21 Web::CSS::ShorthandStyleValue::sub_properties.
pub func css_shorthand_style_value_sub_properties(this: CssShorthandStyleValue*) -> ak.Vector[css_data.PropertyId]:
return this.m_properties.sub_properties
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h:22 Web::CSS::ShorthandStyleValue::values.
pub func css_shorthand_style_value_values(this: CssShorthandStyleValue*) -> ak.Vector[CssStyleValue*]:
return this.m_properties.values
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp:35 Web::CSS::ShorthandStyleValue::longhand.
pub func css_shorthand_style_value_longhand(this: CssShorthandStyleValue*, longhand: css_data.PropertyId) -> CssStyleValue*?:
for i in 0..<ak.vector_size(&this.m_properties.sub_properties):
if *ak.vector_at_const(&this.m_properties.sub_properties, i) == longhand:
return *ak.vector_at_const(&this.m_properties.values, i)
return none
## `longhand(id)` where the C++ dereferences the result (`longhand(id)->...`): the longhand must exist.
func css_shorthand_style_value_required_longhand(this: CssShorthandStyleValue*, longhand: css_data.PropertyId) -> CssStyleValue*:
return css_shorthand_style_value_longhand(this, longhand) else trap("VERIFY: the shorthand has this longhand")
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h:28 Web::CSS::ShorthandStyleValue::properties_equal.
pub func css_shorthand_style_value_properties_equal(this: CssShorthandStyleValue*, other: CssShorthandStyleValue*) -> bool:
let a = &this.m_properties
let b = &other.m_properties
if a.shorthand_property != b.shorthand_property:
return false
if ak.vector_size(&a.sub_properties) != ak.vector_size(&b.sub_properties):
return false
for i in 0..<ak.vector_size(&a.sub_properties):
if *ak.vector_at_const(&a.sub_properties, i) != *ak.vector_at_const(&b.sub_properties, i):
return false
return css_style_value_vectors_equal(&a.values, &b.values)
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::ShorthandStyleValue (CRTP helper) (override).
pub func css_shorthand_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
if css_style_value_type(base) != css_style_value_type(other):
return false
return css_shorthand_style_value_properties_equal((CssShorthandStyleValue*)base, (CssShorthandStyleValue*)other)
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h:30 Web::CSS::ShorthandStyleValue::is_computationally_independent (override).
pub func css_shorthand_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
let this = (CssShorthandStyleValue*)base
for value in ak.vector_span_const(&this.m_properties.values):
if not css_style_value_is_computationally_independent(value):
return false
return true
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp:984 Web::CSS::ShorthandStyleValue::set_style_sheet (override).
pub func css_shorthand_style_value_set_style_sheet_impl(base: CssStyleValue*, style_sheet: CssCssStyleSheet*?):
let this = (CssShorthandStyleValue*)base
css_style_value_set_style_sheet_impl(base, style_sheet)
for value in ak.vector_span_const(&this.m_properties.values):
css_style_value_set_style_sheet(value, style_sheet)
# mark: Serialization helpers ===================================================================
## `Vector<PropertyID>::contains_slow`.
func css_property_ids_contain(ids: const css_data.PropertyId[], id: css_data.PropertyId) -> bool:
for candidate in ids:
if candidate == id:
return true
return false
## Two Strings compare equal (String's `==`).
func css_strings_equal(a: ak.String, b: ak.String) -> bool:
return ak.string_equals(&a, &b)
## The captures of the lambda ShorthandStyleValue::serialize passes to for_each_property_expanding_shorthands.
struct CssShorthandStyleValueSerializeKeywords:
var built_in_keyword: css_data.Keyword?
var all_same_keyword: bool
## The lambda ShorthandStyleValue::serialize passes to for_each_property_expanding_shorthands: whether all the
## longhands are the same CSS-wide keyword.
func css_shorthand_style_value_serialize_check_keyword(context: void*?, property_id: css_data.PropertyId, value: CssStyleValue*):
_ = property_id
let captures = (CssShorthandStyleValueSerializeKeywords*)(context else trap("VERIFY: captures"))
if not css_style_value_is_css_wide_keyword(value):
captures.all_same_keyword = false
return
let keyword = css_style_value_to_keyword(value)
if captures.built_in_keyword == none:
captures.built_in_keyword = keyword
return
if captures.built_in_keyword != keyword:
captures.all_same_keyword = false
return
## The `should_serialize_longhand` lambda of `coordinating_value_list_shorthand_serialize`.
func css_shorthand_style_value_should_serialize_longhand(this: CssShorthandStyleValue*, mode: css_syntax.SerializationMode, required_longhands: const css_data.PropertyId[], reset_only_longhands: const css_data.PropertyId[], entry_index: usize, longhand_index: usize) -> bool:
let longhand_id = *ak.vector_at_const(&this.m_properties.sub_properties, longhand_index)
if css_property_ids_contain(reset_only_longhands, longhand_id):
return false
if css_property_ids_contain(required_longhands, longhand_id):
return true
let longhand_value = *ak.vector_at_const(&css_style_value_as_value_list(css_shorthand_style_value_required_longhand(this, longhand_id)).m_properties.values, entry_index)
if not css_style_value_equals(longhand_value, *ak.vector_at_const(&css_style_value_as_value_list(css_property_initial_value(longhand_id)).m_properties.values, 0)):
return true
for other_longhand_index in longhand_index + 1..<ak.vector_size(&this.m_properties.sub_properties):
let other_longhand_id = *ak.vector_at_const(&this.m_properties.sub_properties, other_longhand_index)
if css_property_ids_contain(reset_only_longhands, other_longhand_id):
continue
let other_longhand_value = *ak.vector_at_const(&css_style_value_as_value_list(css_shorthand_style_value_required_longhand(this, other_longhand_id)).m_properties.values, entry_index)
# FIXME: This should really account for the other longhand being included in the serialization for any reason, not just because it is not the initial value.
if css_style_value_equals(other_longhand_value, *ak.vector_at_const(&css_style_value_as_value_list(css_property_initial_value(other_longhand_id)).m_properties.values, 0)):
continue
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(&parsing_params)
let other_longhand_string = css_style_value_to_string(other_longhand_value, mode)
if parse_css_value(parsing_params, ak.string_bytes_as_string_view(&other_longhand_string), longhand_id) != none:
return true
return false
## The `coordinating_value_list_shorthand_serialize` lambda of ShorthandStyleValue::serialize.
func css_shorthand_style_value_coordinating_value_list_shorthand_serialize(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode, entry_when_all_longhands_initial: str, required_longhands: const css_data.PropertyId[], reset_only_longhands: const css_data.PropertyId[]):
for reset_only_longhand in reset_only_longhands:
if not css_style_value_equals(css_shorthand_style_value_required_longhand(this, reset_only_longhand), css_property_initial_value(reset_only_longhand)):
return
# If any non-reset-only longhand is not a value list, we can't serialize as a coordinating-list shorthand.
for sub_property in ak.vector_span_const(&this.m_properties.sub_properties):
if not css_property_ids_contain(reset_only_longhands, sub_property) and not css_style_value_is_value_list(css_shorthand_style_value_required_longhand(this, sub_property)):
return
let entry_count = css_style_value_list_size(css_style_value_as_value_list(css_shorthand_style_value_required_longhand(this, *ak.vector_at_const(&this.m_properties.sub_properties, 0))))
# If we don't have the same number of values for each non-reset-only longhand, we can't serialize this shorthand.
for longhand_id in ak.vector_span_const(&this.m_properties.sub_properties):
if not css_property_ids_contain(reset_only_longhands, longhand_id) and css_style_value_list_size(css_style_value_as_value_list(css_shorthand_style_value_required_longhand(this, longhand_id))) != entry_count:
return
# We should serialize a longhand if it is not a reset-only longhand and one of the following is true:
# - The longhand is required
# - The value is not the initial value
# - Another longhand value which will be included later in the serialization is valid for this longhand.
for entry_index in 0..<entry_count:
var first = true
for longhand_index in 0..<ak.vector_size(&this.m_properties.sub_properties):
let longhand_id = *ak.vector_at_const(&this.m_properties.sub_properties, longhand_index)
if not css_shorthand_style_value_should_serialize_longhand(this, mode, required_longhands, reset_only_longhands, entry_index, longhand_index):
continue
if not ak.string_builder_is_empty(builder) and not first:
ak.string_builder_append_u8(builder, ' ')
let longhand_value = *ak.vector_at_const(&css_style_value_as_value_list(css_shorthand_style_value_required_longhand(this, longhand_id)).m_properties.values, entry_index)
css_style_value_serialize(longhand_value, builder, mode)
first = false
if first:
ak.string_builder_append(builder, entry_when_all_longhands_initial)
if entry_index != entry_count - 1:
ak.string_builder_append(builder, ", ")
## The `default_serialize` lambda of ShorthandStyleValue::serialize.
func css_shorthand_style_value_default_serialize(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
var all_properties_same_value = true
let first_property_value = *ak.vector_first_const(&this.m_properties.values)
for i in 1..<ak.vector_size(&this.m_properties.values):
if not css_style_values_equal(*ak.vector_at_const(&this.m_properties.values, i), first_property_value):
all_properties_same_value = false
break
if all_properties_same_value:
css_style_value_serialize(first_property_value, builder, mode)
return
var first = true
for i in 0..<ak.vector_size(&this.m_properties.values):
let value = *ak.vector_at_const(&this.m_properties.values, i)
let value_string = css_style_value_to_string(value, mode)
let initial_value_string = css_style_value_to_string(css_property_initial_value(*ak.vector_at_const(&this.m_properties.sub_properties, i)), mode)
if css_strings_equal(value_string, initial_value_string):
continue
if first:
first = false
else:
ak.string_builder_append_u8(builder, ' ')
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&value_string))
if ak.string_builder_is_empty(builder):
css_style_value_serialize(*ak.vector_first_const(&this.m_properties.values), builder, mode)
## `style_value->is_value_list() ? style_value->as_value_list().size() : 1`, the `get_layer_count` lambdas.
func css_shorthand_layer_count(style_value: CssStyleValue*) -> usize:
if css_style_value_is_value_list(style_value):
return css_style_value_list_size(css_style_value_as_value_list(style_value))
return 1
## The `get_layer_value` lambdas: a layer's value of a (possibly) layered longhand.
func css_shorthand_layer_value(style_value: CssStyleValue*, index: usize) -> CssStyleValue*:
if css_style_value_is_value_list(style_value):
return css_style_value_list_value_at(css_style_value_as_value_list(style_value), index, true)
return style_value
# mark: Serialization ===========================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp:44 Web::CSS::ShorthandStyleValue::serialize (override).
pub func css_shorthand_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
let this = (CssShorthandStyleValue*)base
# If all the longhands are the same CSS-wide keyword, just return that once.
var keywords = CssShorthandStyleValueSerializeKeywords(built_in_keyword = none, all_same_keyword = true)
css_style_computer_for_each_property_expanding_shorthands(this.m_properties.shorthand_property, base, ak.Function2[css_data.PropertyId, CssStyleValue*, unit](call = css_shorthand_style_value_serialize_check_keyword, context = &keywords))
if let built_in_keyword = keywords.built_in_keyword:
if keywords.all_same_keyword:
ak.string_builder_append_string_view(builder, css_data.string_from_keyword(built_in_keyword))
return
# NB: The empty `Vector<PropertyID>` defaults of coordinating_value_list_shorthand_serialize.
var no_longhands: const css_data.PropertyId[]
# Then special cases
# FIXME: overflow-clip-margin needs a special case here for when its longhands aren't identical.
match this.m_properties.shorthand_property:
.all:
# NOTE: 'all' can only be serialized in the case all sub-properties share the same CSS-wide keyword, this is
# handled above, thus, if we get to here that mustn't be the case and we should return the empty string.
return
.animation:
css_shorthand_style_value_coordinating_value_list_shorthand_serialize(this, builder, mode, "none", no_longhands, [css_data.PropertyId.animation_timeline])
.background:
css_shorthand_style_value_serialize_background(this, builder, mode)
.background_position:
css_shorthand_style_value_serialize_background_position(this, builder, mode)
.border:
css_shorthand_style_value_serialize_border(this, builder, mode)
.border_image:
css_shorthand_style_value_serialize_border_image(this, builder, mode)
.border_radius:
css_shorthand_style_value_serialize_border_radius(this, builder, mode)
.columns:
css_shorthand_style_value_serialize_columns(this, builder, mode)
.flex:
css_style_value_serialize(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.flex_grow), builder, mode)
ak.string_builder_append_u8(builder, ' ')
css_style_value_serialize(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.flex_shrink), builder, mode)
ak.string_builder_append_u8(builder, ' ')
css_style_value_serialize(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.flex_basis), builder, mode)
.font:
css_shorthand_style_value_serialize_font(this, builder, mode)
.font_variant:
css_shorthand_style_value_serialize_font_variant(this, builder, mode)
.grid_area:
css_shorthand_style_value_serialize_grid_area(this, builder, mode)
.grid:
# <'grid-template'> falls through to the grid-template case.
if css_shorthand_style_value_serialize_grid(this, builder, mode):
css_shorthand_style_value_serialize_grid_template(this, builder, mode)
.grid_template:
css_shorthand_style_value_serialize_grid_template(this, builder, mode)
.grid_column:
css_shorthand_style_value_serialize_grid_line_pair(this, builder, mode, css_data.PropertyId.grid_column_start, css_data.PropertyId.grid_column_end)
.grid_row:
css_shorthand_style_value_serialize_grid_line_pair(this, builder, mode, css_data.PropertyId.grid_row_start, css_data.PropertyId.grid_row_end)
.mask:
css_shorthand_style_value_serialize_mask(this, builder, mode)
.place_content, .place_items, .place_self:
let serialized = css_serialize_a_positional_value_list(&this.m_properties.values, mode)
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&serialized))
.scroll_timeline:
# NB: We don't need to specify a value to use when the entry is empty as all values are initial since
# scroll-timeline-name is always included
css_shorthand_style_value_coordinating_value_list_shorthand_serialize(this, builder, mode, "", [css_data.PropertyId.scroll_timeline_name], no_longhands)
.text_decoration:
css_shorthand_style_value_serialize_text_decoration(this, builder, mode)
.transition:
css_shorthand_style_value_coordinating_value_list_shorthand_serialize(this, builder, mode, "all", no_longhands, no_longhands)
.view_timeline:
# NB: We don't need to specify a value to use when the entry is empty as all values are initial since
# view-timeline-name is always included
css_shorthand_style_value_coordinating_value_list_shorthand_serialize(this, builder, mode, "", [css_data.PropertyId.view_timeline_name], no_longhands)
.white_space:
css_shorthand_style_value_serialize_white_space(this, builder, mode)
_:
if css_data.property_is_positional_value_list_shorthand(this.m_properties.shorthand_property):
let serialized = css_serialize_a_positional_value_list(&this.m_properties.values, mode)
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&serialized))
return
css_shorthand_style_value_default_serialize(this, builder, mode)