dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_values / counter_style_style_value.lucb
#==============================================================================================
#
# counter_style_style_value - CounterStyleStyleValue: a <counter-style> (a name or symbols())
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/CounterStyleStyleValue.cpp (lines 1-86) and the inline
# members of CounterStyleStyleValue.h: serialization and resolving the counter style in a
# style scope (symbols() defines an anonymous counter style).
#
# Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.h:23 Web::CSS::CounterStyleStyleValue::create.
pub func css_counter_style_style_value_create(value: FlyStringOrCssCounterStyleStyleValueSymbolsFunction) -> CssCounterStyleStyleValue*:
let this = &(new CssCounterStyleStyleValue[1] --- else trap("out of memory"))[0]
css_counter_style_style_value_construct(this, value)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.h:39 Web::CSS::CounterStyleStyleValue::CounterStyleStyleValue.
pub func css_counter_style_style_value_construct(this: CssCounterStyleStyleValue*, value: FlyStringOrCssCounterStyleStyleValueSymbolsFunction):
css_style_value_construct(this.css_style_value(), CssStyleValueType.counter_style)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_counter_style_style_value_vtable, &css_counter_style_style_value_class)
this.m_value = value
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.cpp:15 Web::CSS::CounterStyleStyleValue::serialize (override).
pub func css_counter_style_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, arg1: css_syntax.SerializationMode):
_ = arg1
let this = (CssCounterStyleStyleValue*)base
match this.m_value:
.fly_string(name):
ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&name))
.css_counter_style_style_value_symbols_function(symbols_function):
ak.string_builder_append(builder, "symbols(")
if symbols_function.type_ != css_data.SymbolsType.symbolic:
css_appendff(builder, "{} ", [ak.arg_string_view(css_data.to_string_symbols_type(symbols_function.type_))])
for i in 0..<ak.vector_size(&symbols_function.symbols):
if i > 0:
ak.string_builder_append_u8(builder, ' ')
css_syntax.serialize_a_string_string_builder(builder, ak.fly_string_bytes_as_string_view(ak.vector_at_const(&symbols_function.symbols, i)))
ak.string_builder_append_u8(builder, ')')
## resolve_counter_style's `algorithm` lambda: the algorithm of a symbols() function.
func css_symbols_function_algorithm(symbols_function: const CssCounterStyleStyleValueSymbolsFunction*) -> CssCounterStyleAlgorithm:
# The counter style’s algorithm is constructed by consulting the previous chapter using the provided
# system — or symbolic if the system was omitted — and the provided <string>s and <image>s as the value
# of the symbols property. If the system is fixed, the first symbol value is 1.
match symbols_function.type_:
.cyclic:
return CssCounterStyleAlgorithm.css_generic_counter_style_algorithm(CssGenericCounterStyleAlgorithm(type_ = css_data.CounterStyleSystem.cyclic, symbol_list = symbols_function.symbols))
.numeric:
return CssCounterStyleAlgorithm.css_generic_counter_style_algorithm(CssGenericCounterStyleAlgorithm(type_ = css_data.CounterStyleSystem.numeric, symbol_list = symbols_function.symbols))
.alphabetic:
return CssCounterStyleAlgorithm.css_generic_counter_style_algorithm(CssGenericCounterStyleAlgorithm(type_ = css_data.CounterStyleSystem.alphabetic, symbol_list = symbols_function.symbols))
.symbolic:
return CssCounterStyleAlgorithm.css_generic_counter_style_algorithm(CssGenericCounterStyleAlgorithm(type_ = css_data.CounterStyleSystem.symbolic, symbol_list = symbols_function.symbols))
.fixed:
return CssCounterStyleAlgorithm.css_fixed_counter_style_algorithm(CssFixedCounterStyleAlgorithm(first_symbol = 1, symbol_list = symbols_function.symbols))
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.cpp:35 Web::CSS::CounterStyleStyleValue::resolve_counter_style.
pub func css_counter_style_style_value_resolve_counter_style(this: CssCounterStyleStyleValue*, style_scope: const CssStyleScope*) -> CssCounterStyle*?:
match this.m_value:
.fly_string(name):
return css_style_scope_get_registered_counter_style(style_scope, name)
.css_counter_style_style_value_symbols_function(symbols_function):
# https://drafts.csswg.org/css-counter-styles-3/#symbols-function
let algorithm = css_symbols_function_algorithm(&symbols_function)
# The symbols() function defines an anonymous counter style with no name, a prefix of "" (empty string) and
# suffix of " " (U+0020 SPACE), a range of auto, a fallback of decimal, a negative of "\2D" ("-"
# hyphen-minus), a pad of 0 "", and a speak-as of auto.
# FIXME: Pass the correct speak-as value once we support that.
let definition = css_counter_style_definition_create(ak.fly_string_from_str(""),
CssCounterStyleAlgorithmOrExtends.css_counter_style_algorithm(algorithm),
CssCounterStyleNegativeSign(prefix = ak.fly_string_from_str("-"), suffix = ak.fly_string_from_str("")),
ak.fly_string_from_str(""),
ak.fly_string_from_str(" "),
EmptyOrCssAutoRangeOrVectorCssCounterStyleRangeEntry.css_auto_range(CssAutoRange()),
ak.fly_string_from_str("decimal"),
CssCounterStylePad(minimum_length = 0, symbol = ak.fly_string_from_str("")))
# NB: We use empty string instead of no name for simplicity - this doesn't clash with
# <counter-style-name> since that is a <custom-ident> which can't be an empty string.
# NB: We don't need to pass registered counter styles here since we don't rely on extension.
return css_counter_style_from_counter_style_definition(&definition, style_scope)
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.h:34 Web::CSS::CounterStyleStyleValue::properties_equal.
pub func css_counter_style_style_value_properties_equal(this: CssCounterStyleStyleValue*, other: CssCounterStyleStyleValue*) -> bool:
match this.m_value:
.fly_string(name):
match other.m_value:
.fly_string(other_name): return ak.fly_string_eq_fly_string(&name, &other_name)
_: return false
.css_counter_style_style_value_symbols_function(symbols_function):
match other.m_value:
.css_counter_style_style_value_symbols_function(other_function):
# SymbolsFunction's defaulted operator==.
if symbols_function.type_ != other_function.type_ or ak.vector_size(&symbols_function.symbols) != ak.vector_size(&other_function.symbols):
return false
for i in 0..<ak.vector_size(&symbols_function.symbols):
if not ak.fly_string_eq_fly_string(ak.vector_at_const(&symbols_function.symbols, i), ak.vector_at_const(&other_function.symbols, i)):
return false
return true
_: return false
## Ported from Libraries/LibWeb/CSS/StyleValues/CounterStyleStyleValue.h:36 Web::CSS::CounterStyleStyleValue::is_computationally_independent (override).
pub func css_counter_style_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
_ = base
return true
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::CounterStyleStyleValue (CRTP helper) (override).
pub func css_counter_style_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
if css_style_value_type(base) != css_style_value_type(other):
return false
return css_counter_style_style_value_properties_equal((CssCounterStyleStyleValue*)base, (CssCounterStyleStyleValue*)other)