Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / style_values / style_value_1.lucb

#==============================================================================================
#
#   style_value_1 - StyleValue: the base of every CSS value, and the helpers on values
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/StyleValue.cpp (lines 1-139, 152-237) and the inline
#       members of StyleValue.h (lines 1-243) other than the per-type `is_*`/`as_*` of
#       ENUMERATE_CSS_STYLE_VALUE_TYPES (style_value_2, style_value_3). StyleValue is reference
#       counted in C++; here values live in managed memory (DESIGN.md §3.3) with the same
#       embedded-base, type and vtable scheme as cells (§2.3): a value's `vtable` and
#       `class_id` are set by its most-derived constructor, as the C++ vptr is.
#       StyleValueWithDefaultOperators<T>::equals is ported as its instantiations, one
#       `<class>_equals_impl` per class in that class's fragment.
#
#       Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: ColorResolutionContext ==================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:94 Web::CSS::ColorResolutionContext::for_element.
pub func css_color_resolution_context_for_element(element: DomAbstractElement) -> CssColorResolutionContext:
    let computed_properties = dom_abstract_element_computed_properties(&element) else trap("VERIFY: computed properties")
    let document = dom_abstract_element_document(&element)
    let supported_color_schemes = dom_document_supported_color_schemes(document)
    var document_supported_schemes: const ak.Vector[ak.String]*? = none
    if let schemes = supported_color_schemes:
        document_supported_schemes = &schemes
    let color_scheme = css_computed_properties_color_scheme(computed_properties, page_preferred_color_scheme(dom_document_page(document)), document_supported_schemes)

    let calculation_resolution_context = CssCalculationResolutionContext(length_resolution_context = css_length_resolution_context_for_element(element))

    # NB: C++ builds this context twice, once for each call below.
    let resolution_context = CssColorResolutionContext(color_scheme = color_scheme, current_color = css_initial_values_color(), accent_color = css_system_color_accent_color(color_scheme), document = document, calculation_resolution_context = calculation_resolution_context)
    return CssColorResolutionContext(
        color_scheme = color_scheme,
        current_color = css_computed_properties_color(computed_properties, css_data.PropertyId.color, resolution_context),
        accent_color = css_computed_properties_accent_color(computed_properties, resolution_context),
        document = document,
        calculation_resolution_context = calculation_resolution_context,
    )

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:109 Web::CSS::ColorResolutionContext::for_layout_node_with_style.
pub func css_color_resolution_context_for_layout_node_with_style(layout_node: LayoutNodeWithStyle*) -> CssColorResolutionContext:
    let computed_values = layout_node_with_style_computed_values(layout_node)
    return CssColorResolutionContext(
        color_scheme = css_computed_values_color_scheme(&computed_values.computed_values),
        current_color = css_computed_values_color(&computed_values.computed_values),
        accent_color = css_computed_values_accent_color(&computed_values.computed_values),
        document = layout_node_document((LayoutNode*)layout_node),
        calculation_resolution_context = CssCalculationResolutionContext(length_resolution_context = css_length_resolution_context_for_layout_node((LayoutNode*)layout_node)),
    )

# mark: StyleValue ==============================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:120 Web::CSS::StyleValue::StyleValue.
pub func css_style_value_construct(this: CssStyleValue*, type_: CssStyleValueType):
    this.m_type = type_

## The value's vtable and class id: what the C++ vptr of the most-derived class gives it. Each
## most-derived constructor calls this after its base constructor.
pub func css_style_value_set_class(this: CssStyleValue*, vtable: const CssStyleValueVTable*, info: const ak.ClassInfo*):
    this.vtable = vtable
    this.class_id = info.first_id

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:125 Web::CSS::StyleValue::to_string.
pub func css_style_value_to_string(this: CssStyleValue*, mode: css_syntax.SerializationMode) -> ak.String:
    var builder = ak.string_builder_new()
    css_style_value_serialize(this, &builder, mode)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:127 Web::CSS::StyleValue::type.
pub func css_style_value_type(this: CssStyleValue*) -> CssStyleValueType:
    return this.m_type

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:129 Web::CSS::StyleValue::is_abstract_image.
pub func css_style_value_is_abstract_image(this: CssStyleValue*) -> bool:
    let type_ = css_style_value_type(this)
    return type_ == CssStyleValueType.image or type_ == CssStyleValueType.image_set or type_ == CssStyleValueType.linear_gradient or type_ == CssStyleValueType.conic_gradient or type_ == CssStyleValueType.radial_gradient

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:132 Web::CSS::StyleValue::as_abstract_image.
pub func css_style_value_as_abstract_image_const(this: CssStyleValue*) -> CssAbstractImageStyleValue*:
    assert(css_style_value_is_abstract_image(this))
    return (CssAbstractImageStyleValue*)this

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:134 Web::CSS::StyleValue::as_abstract_image.
pub func css_style_value_as_abstract_image(this: CssStyleValue*) -> CssAbstractImageStyleValue*:
    return css_style_value_as_abstract_image_const(this)

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:136 Web::CSS::StyleValue::is_dimension.
pub func css_style_value_is_dimension(this: CssStyleValue*) -> bool:
    let type_ = css_style_value_type(this)
    return type_ == CssStyleValueType.angle or type_ == CssStyleValueType.flex or type_ == CssStyleValueType.frequency or type_ == CssStyleValueType.length or type_ == CssStyleValueType.percentage or type_ == CssStyleValueType.resolution or type_ == CssStyleValueType.time

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:138 Web::CSS::StyleValue::as_dimension.
pub func css_style_value_as_dimension_const(this: CssStyleValue*) -> CssDimensionStyleValue*:
    assert(css_style_value_is_dimension(this))
    return (CssDimensionStyleValue*)this

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:141 Web::CSS::StyleValue::as_dimension.
pub func css_style_value_as_dimension(this: CssStyleValue*) -> CssDimensionStyleValue*:
    return css_style_value_as_dimension_const(this)

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:143 Web::CSS::StyleValue::is_color_function (virtual).
pub func css_style_value_is_color_function_impl(this: CssStyleValue*) -> bool:
    _ = this
    return false

# https://www.w3.org/TR/css-values-4/#common-keywords
# https://drafts.csswg.org/css-cascade-4/#valdef-all-revert
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:154 Web::CSS::StyleValue::is_css_wide_keyword.
pub func css_style_value_is_css_wide_keyword(this: CssStyleValue*) -> bool:
    return css_style_value_is_inherit(this) or css_style_value_is_initial(this) or css_style_value_is_revert(this) or css_style_value_is_unset(this) or css_style_value_is_revert_layer(this)

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:155 Web::CSS::StyleValue::is_inherit.
pub func css_style_value_is_inherit(this: CssStyleValue*) -> bool:
    return css_style_value_to_keyword(this) == css_data.Keyword.inherit

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:156 Web::CSS::StyleValue::is_initial.
pub func css_style_value_is_initial(this: CssStyleValue*) -> bool:
    return css_style_value_to_keyword(this) == css_data.Keyword.initial

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:157 Web::CSS::StyleValue::is_revert.
pub func css_style_value_is_revert(this: CssStyleValue*) -> bool:
    return css_style_value_to_keyword(this) == css_data.Keyword.revert

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:158 Web::CSS::StyleValue::is_revert_layer.
pub func css_style_value_is_revert_layer(this: CssStyleValue*) -> bool:
    return css_style_value_to_keyword(this) == css_data.Keyword.revert_layer

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:159 Web::CSS::StyleValue::is_unset.
pub func css_style_value_is_unset(this: CssStyleValue*) -> bool:
    return css_style_value_to_keyword(this) == css_data.Keyword.unset

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:153 Web::CSS::StyleValue::absolutized (virtual).
pub func css_style_value_absolutized_impl(this: CssStyleValue*, arg0: CssComputationContext) -> CssStyleValue*:
    _ = arg0
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:158 Web::CSS::StyleValue::has_auto.
pub func css_style_value_has_auto(this: CssStyleValue*) -> bool:
    return css_style_value_is_keyword(this) and css_keyword_style_value_keyword(css_style_value_as_keyword(this)) == css_data.Keyword.auto

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:162 Web::CSS::StyleValue::has_color (virtual).
pub func css_style_value_has_color_impl(this: CssStyleValue*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:166 Web::CSS::StyleValue::to_color (virtual).
pub func css_style_value_to_color_impl(this: CssStyleValue*, arg0: CssColorResolutionContext) -> gfx.Color?:
    _ = this
    _ = arg0
    return none

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:170 Web::CSS::StyleValue::serialize (virtual).
pub func css_style_value_serialize_impl(this: CssStyleValue*, arg0: ak.StringBuilder*, arg1: css_syntax.SerializationMode):
    _ = this
    _ = arg0
    _ = arg1
    trap("pure virtual: Web::CSS::StyleValue::serialize")

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:163 Web::CSS::StyleValue::tokenize (virtual).
pub func css_style_value_tokenize_impl(this: CssStyleValue*) -> ak.Vector[css_syntax.ComponentValue]:
    # This is an inefficient way of producing ComponentValues, but it's guaranteed to work for types that round-trip.
    # FIXME: Implement better versions in the subclasses.
    var parsing_params: CssParsingParams = ---
    css_parsing_params_construct_css_parsing_mode(&parsing_params)
    let string = css_style_value_to_string(this, css_syntax.SerializationMode.normal)
    var parser = css_parser_create(parsing_params, ak.string_bytes_as_string_view(&string))
    return css_parser_parse_as_list_of_component_values(&parser)

# https://drafts.css-houdini.org/css-typed-om-1/#reify-as-a-cssstylevalue
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:171 Web::CSS::StyleValue::reify (virtual).
pub func css_style_value_reify_impl(this: CssStyleValue*, realm: Realm*, associated_property: ak.FlyString) -> CssCssStyleValue*:
    # 1. Return a new CSSStyleValue object representing value whose [[associatedProperty]] internal slot is set to property.
    return css_css_style_value_create(realm, associated_property, this)

# https://drafts.css-houdini.org/css-typed-om-1/#subdivide-into-iterations
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:178 Web::CSS::StyleValue::subdivide_into_iterations (virtual).
pub func css_style_value_subdivide_into_iterations_impl(this: CssStyleValue*, arg0: CssPropertyNameAndId) -> ak.Vector[CssStyleValue*]:
    _ = arg0
    # To subdivide into iterations a CSS value whole value for a property property, execute the following steps:
    # 1. If property is a single-valued property, return a list containing whole value.
    # 2. Otherwise, divide whole value into individual iterations, as appropriate for property, and return a list
    #    containing the iterations in order.
    # NB: We do this by type. By default, we assume step 1 applies. For step 2, override this method.
    var values = ak.Vector[CssStyleValue*]()
    ak.vector_append_t(&values, this)
    return values

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:175 Web::CSS::StyleValue::set_style_sheet (virtual).
pub func css_style_value_set_style_sheet_impl(this: CssStyleValue*, arg0: CssCssStyleSheet*?):
    _ = this
    _ = arg0

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:176 Web::CSS::StyleValue::visit_edges (virtual).
pub func css_style_value_visit_edges_impl(this: CssStyleValue*, arg0: gc.CellVisitor*):
    _ = this
    _ = arg0

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:178 Web::CSS::StyleValue::equals (virtual).
pub func css_style_value_equals_impl(this: CssStyleValue*, other: CssStyleValue*) -> bool:
    _ = this
    _ = other
    trap("pure virtual: Web::CSS::StyleValue::equals")

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:180 Web::CSS::StyleValue::operator==.
pub func css_style_value_eq(this: CssStyleValue*, other: CssStyleValue*) -> bool:
    return css_style_value_equals(this, other)

# https://drafts.css-houdini.org/css-properties-values-api/#computationally-independent
# A property value is computationally independent if it can be converted into a computed value using only the value
# of the property on the element, and "global" information that cannot be changed by CSS.
## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:188 Web::CSS::StyleValue::is_computationally_independent (virtual).
pub func css_style_value_is_computationally_independent_impl(this: CssStyleValue*) -> bool:
    _ = this
    trap("pure virtual: Web::CSS::StyleValue::is_computationally_independent")

# mark: Free functions ==========================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:188 Web::CSS::int_from_style_value.
pub func css_int_from_style_value(style_value: CssStyleValue*) -> i32:
    if css_style_value_is_integer(style_value):
        return css_integer_style_value_integer(css_style_value_as_integer(style_value))

    if css_style_value_is_calculated(style_value):
        return css_calculated_style_value_resolve_integer(css_style_value_as_calculated(style_value), CssCalculationResolutionContext()) else trap("VERIFY: a resolved integer")

    trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:199 Web::CSS::number_from_style_value.
pub func css_number_from_style_value(style_value: CssStyleValue*, percentage_basis: f64?) -> f64:
    if css_style_value_is_number(style_value):
        return css_number_style_value_number(css_style_value_as_number(style_value))

    if css_style_value_is_calculated(style_value):
        let calculated_style_value = css_style_value_as_calculated(style_value)

        if css_calculated_style_value_resolves_to_number(calculated_style_value):
            return css_calculated_style_value_resolve_number(calculated_style_value, CssCalculationResolutionContext()) else trap("VERIFY: a resolved number")

        if css_calculated_style_value_resolves_to_percentage(calculated_style_value):
            let basis = percentage_basis else trap("VERIFY: a percentage basis")

            let percentage = css_calculated_style_value_resolve_percentage(calculated_style_value, CssCalculationResolutionContext()) else trap("VERIFY: a resolved percentage")
            return css_percentage_as_fraction(&percentage) * basis

        trap("VERIFY_NOT_REACHED")

    if css_style_value_is_percentage(style_value):
        let basis = percentage_basis else trap("VERIFY: a percentage basis")

        let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(style_value))
        return basis * css_percentage_as_fraction(&percentage)

    trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp:228 Web::CSS::string_from_style_value.
pub func css_string_from_style_value(style_value: CssStyleValue*) -> ak.FlyString:
    if css_style_value_is_string(style_value):
        return css_string_style_value_string_value(css_style_value_as_string(style_value))

    if css_style_value_is_custom_ident(style_value):
        return css_custom_ident_style_value_custom_ident(css_style_value_as_custom_ident(style_value))

    trap("VERIFY_NOT_REACHED")

# mark: Comparison helpers ======================================================================

## Optional<FlyString>'s operator== (both absent, or both present and equal), for the
## `operator==(...) const = default`s of the values' Properties.
pub func css_optional_fly_strings_equal(a: ak.FlyString?, b: ak.FlyString?) -> bool:
    if let left = a:
        if let right = b:
            return ak.fly_string_eq_fly_string(&left, &right)
        return false
    return b == none

## `builder.appendff(fmtstr, arguments...)` for the style values: AK's format string over ak's
## type-erased arguments (DESIGN.md §2.11).
pub func css_appendff(builder: ak.StringBuilder*, fmtstr: str, arguments: const ak.TypeErasedParameter[]):
    ak.vformat_arguments(builder, ak.sv(fmtstr), arguments) else trap("MUST: StringBuilder::appendff")