Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / style_values / unresolved_style_value.lucb

#==============================================================================================
#
#   unresolved_style_value - UnresolvedStyleValue: a value with arbitrary substitution functions, kept as component values
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp (lines 1-167) and the inline
#       members of UnresolvedStyleValue.h.
#
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: UnresolvedStyleValue ====================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:22 Web::CSS::UnresolvedStyleValue::create.
pub func css_unresolved_style_value_create(values: ak.Vector[css_syntax.ComponentValue], substitution_presence: css_syntax.SubstitutionFunctionsPresence, original_source_text: ak.String? = none) -> CssUnresolvedStyleValue*:
    let this = &(new CssUnresolvedStyleValue[1] --- else trap("out of memory"))[0]
    css_unresolved_style_value_construct(this, values, substitution_presence, original_source_text)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:27 Web::CSS::UnresolvedStyleValue::UnresolvedStyleValue.
pub func css_unresolved_style_value_construct(this: CssUnresolvedStyleValue*, values: ak.Vector[css_syntax.ComponentValue], substitution_presence: css_syntax.SubstitutionFunctionsPresence, original_source_text: ak.String?):
    css_style_value_construct(this.css_style_value(), CssStyleValueType.unresolved)
    css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_unresolved_style_value_vtable, &css_unresolved_style_value_class)
    this.m_values = values
    this.m_substitution_functions_presence = substitution_presence
    this.m_original_source_text = original_source_text

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:35 Web::CSS::UnresolvedStyleValue::serialize (override).
pub func css_unresolved_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, arg1: css_syntax.SerializationMode):
    _ = arg1
    let this = (CssUnresolvedStyleValue*)base
    if let original_source_text = this.m_original_source_text:
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&original_source_text))
        return

    let serialized = css_serialize_a_series_of_component_values(ak.vector_span_const(&this.m_values))
    let trimmed = ak.string_trim_ascii_whitespace(&serialized) else trap("MUST: String::trim_ascii_whitespace")
    ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&trimmed))

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:24 Web::CSS::UnresolvedStyleValue::tokenize (override).
pub func css_unresolved_style_value_tokenize_impl(base: CssStyleValue*) -> ak.Vector[css_syntax.ComponentValue]:
    let this = (CssUnresolvedStyleValue*)base
    return ak.vector_clone(&this.m_values)

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:26 Web::CSS::UnresolvedStyleValue::values.
pub func css_unresolved_style_value_values(this: CssUnresolvedStyleValue*) -> ak.Vector[css_syntax.ComponentValue]:
    return this.m_values

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:27 Web::CSS::UnresolvedStyleValue::contains_arbitrary_substitution_function.
pub func css_unresolved_style_value_contains_arbitrary_substitution_function(this: CssUnresolvedStyleValue*) -> bool:
    return css_syntax.substitution_functions_presence_has_any(&this.m_substitution_functions_presence)

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:28 Web::CSS::UnresolvedStyleValue::includes_attr_function.
pub func css_unresolved_style_value_includes_attr_function(this: CssUnresolvedStyleValue*) -> bool:
    return this.m_substitution_functions_presence.attr

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:29 Web::CSS::UnresolvedStyleValue::includes_inherit_function.
pub func css_unresolved_style_value_includes_inherit_function(this: CssUnresolvedStyleValue*) -> bool:
    return this.m_substitution_functions_presence.inherit

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:30 Web::CSS::UnresolvedStyleValue::includes_if_function.
pub func css_unresolved_style_value_includes_if_function(this: CssUnresolvedStyleValue*) -> bool:
    return this.m_substitution_functions_presence.if_

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:31 Web::CSS::UnresolvedStyleValue::includes_var_function.
pub func css_unresolved_style_value_includes_var_function(this: CssUnresolvedStyleValue*) -> bool:
    return this.m_substitution_functions_presence.var_

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:45 Web::CSS::UnresolvedStyleValue::equals (override).
pub func css_unresolved_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
    let this = (CssUnresolvedStyleValue*)base
    if css_style_value_type(base) != css_style_value_type(other):
        return false
    let other_values = &css_style_value_as_unresolved(other).m_values
    if ak.vector_size(&this.m_values) != ak.vector_size(other_values):
        return false
    for i in 0..<ak.vector_size(&this.m_values):
        if not css_syntax.component_value_eq(ak.vector_at_const(&this.m_values, i), ak.vector_at_const(other_values, i)):
            return false
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h:37 Web::CSS::UnresolvedStyleValue::is_computationally_independent (override).
pub func css_unresolved_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
    _ = base
    trap("VERIFY_NOT_REACHED")

# mark: Reification =============================================================================

# https://drafts.css-houdini.org/css-typed-om-1/#reify-var
## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:56 Web::CSS::reify_a_var_reference.
func css_reify_a_var_reference(realm: Realm*, function: css_syntax.Function) -> CssVariableReferenceValue*?:
    # NB: A var() might not be representable as a CSSVariableReferenceValue, for example if it has invalid syntax or
    #    it contains an ASF in its variable-name slot. In those cases, we return null here, so it's treated like a
    #    regular function.
    let maybe_var_arguments = css_parse_according_to_argument_grammar(CssArbitrarySubstitutionFunction.var_, function.value) else return none
    var var_arguments: ak.Vector[ak.Vector[css_syntax.ComponentValue]] = ---
    match maybe_var_arguments:
        .vector_vector_component_value(list):
            var_arguments = list
        _:
            trap("VERIFY: a DeclarationValueList")

    # NB: Try to parse the variable name. If we can't, return null as above.
    var tokens = css_syntax.token_stream_create(*ak.vector_first_const(&var_arguments))
    css_syntax.token_stream_discard_whitespace(&tokens)
    let maybe_variable = css_syntax.token_stream_consume_a_token(&tokens)
    css_syntax.token_stream_discard_whitespace(&tokens)
    if css_syntax.token_stream_has_next_token(&tokens) or not css_syntax.component_value_is(&maybe_variable, css_syntax.TokenType.ident):
        return none
    let variable_token = css_syntax.component_value_token(&maybe_variable)
    let ident = css_syntax.token_ident(&variable_token)
    if not css_data.is_a_custom_property_name_string(ak.fly_string_bytes_as_string_view(&ident)):
        return none

    # To reify a var() reference var:
    # 1. Let object be a new CSSVariableReferenceValue.
    # 2. Set object’s variable internal slot to the serialization of the <custom-ident> providing the variable name.
    let variable = ident

    # 3. If var has a fallback value, set object’s fallback internal slot to the result of reifying the fallback’s
    #    component values. Otherwise, set it to null.
    var fallback: CssUnparsedValue*? = none
    if ak.vector_size(&var_arguments) > 1:
        fallback = css_reify_a_list_of_component_values(realm, *ak.vector_at_const(&var_arguments, 1))

    # 4. Return object.
    return css_variable_reference_value_create(realm, variable, fallback)

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:93 Web::CSS::Reifier::reify.
pub func css_reifier_reify(realm: Realm*, source_values: ak.Vector[css_syntax.ComponentValue]) -> ak.Vector[CssGcRootCssUnparsedSegment]:
    var reifier = CssReifier(m_reified_values = ak.Vector[CssGcRootCssUnparsedSegment](), m_unserialized_values = ak.Vector[css_syntax.ComponentValue]())
    css_reifier_process_values(&reifier, realm, source_values)
    if not ak.vector_is_empty(&reifier.m_unserialized_values):
        css_reifier_serialize_unserialized_values(&reifier)
    return reifier.m_reified_values

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:103 Web::CSS::Reifier::process_values.
pub func css_reifier_process_values(this: CssReifier*, realm: Realm*, source_values: ak.Vector[css_syntax.ComponentValue]):
    # NB: var() could be arbitrarily nested within other functions and blocks, so we have to walk the tree.
    #     Also, a var() might not be representable, if it has an ASF in place of its name, so those will be part
    #     of a string instead.
    for component_value in ak.vector_span_const(&source_values):
        if css_syntax.component_value_is_function_string_view(&component_value, ak.sv("var")):
            # First parse the var() to see if it is representable as a CSSVariableReferenceValue. It might not be,
            # for example if it has an ASF in the place of its variable name. In that case we fall back to
            # serializing it like a regular function.
            if let var_reference = css_reify_a_var_reference(realm, css_syntax.component_value_function(&component_value)):
                css_reifier_serialize_unserialized_values(this)
                ak.vector_append_t(&this.m_reified_values, CssGcRootCssUnparsedSegment.css_variable_reference_value(var_reference))
                continue

        if css_syntax.component_value_is_function(&component_value):
            let function = css_syntax.component_value_function(&component_value)
            ak.vector_append_t(&this.m_unserialized_values, css_syntax.component_value_from_token(function.name_token))
            css_reifier_process_values(this, realm, function.value)
            ak.vector_append_t(&this.m_unserialized_values, css_syntax.component_value_from_token(function.end_token))
            continue

        if css_syntax.component_value_is_block(&component_value):
            let block = css_syntax.component_value_block(&component_value)
            ak.vector_append_t(&this.m_unserialized_values, css_syntax.component_value_from_token(block.token))
            css_reifier_process_values(this, realm, block.value)
            ak.vector_append_t(&this.m_unserialized_values, css_syntax.component_value_from_token(block.end_token))
            continue

        ak.vector_append_t(&this.m_unserialized_values, component_value)

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:140 Web::CSS::Reifier::serialize_unserialized_values.
pub func css_reifier_serialize_unserialized_values(this: CssReifier*):
    ak.vector_append_t(&this.m_reified_values, CssGcRootCssUnparsedSegment.string(css_serialize_a_series_of_component_values(ak.vector_span_const(&this.m_unserialized_values))))
    ak.vector_clear_with_capacity(&this.m_unserialized_values)

## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:150 Web::CSS::reify_a_list_of_component_values.
func css_reify_a_list_of_component_values(realm: Realm*, component_values: ak.Vector[css_syntax.ComponentValue]) -> CssUnparsedValue*:
    # To reify a list of component values from a list:
    # 1. Replace all var() references in list with CSSVariableReferenceValue objects, as described in §5.4 var() References.
    # 2. Replace each remaining maximal subsequence of component values in list with a single string of their concatenated serializations.
    let reified_values = css_reifier_reify(realm, component_values)

    # 3. Return a new CSSUnparsedValue whose [[tokens]] slot is set to list.
    return css_unparsed_value_create(realm, reified_values)

# https://drafts.css-houdini.org/css-typed-om-1/#reify-a-list-of-component-values
## Ported from Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp:162 Web::CSS::UnresolvedStyleValue::reify (override).
pub func css_unresolved_style_value_reify_impl(base: CssStyleValue*, realm: Realm*, associated_property: ak.FlyString) -> CssCssStyleValue*:
    _ = associated_property
    let this = (CssUnresolvedStyleValue*)base
    return (CssCssStyleValue*)css_reify_a_list_of_component_values(realm, this.m_values)