Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / css_function_rule.lucb

#==============================================================================================
#
#   css_function_rule - CSS::CSSFunctionRule: an @function rule (a custom function)
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSSFunctionRule.cpp (lines 1-190) and the inline members of
#       CSSFunctionRule.h (lines 17-55): the function's parameters and return type, their
#       serialization ("serialize a CSS type", "serialize a function parameter") and the rule's.
#
#       Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.h:44 Web::CSS::CSSFunctionRule::name.
pub func css_function_rule_name(this: CssFunctionRule*) -> ak.FlyString:
    return this.m_name

## `MUST(serialize_a_series_of_component_values(value).trim_ascii_whitespace())`.
func css_serialize_default_value(default_value: const ak.Vector[css_syntax.ComponentValue]*) -> ak.String:
    let serialized = css_serialize_a_series_of_component_values(ak.vector_span_const(default_value))
    return ak.string_trim_ascii_whitespace(&serialized) else trap("MUST: trim_ascii_whitespace")

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:17 Web::CSS::FunctionParameter::from_internal_function_parameter.
# https://drafts.csswg.org/css-mixins-1/#dictdef-functionparameter
pub func css_function_parameter_from_internal_function_parameter(internal: CssFunctionParameterInternal) -> CssFunctionParameter:
    var default_value: ak.String? = none
    if let value = internal.default_value:
        default_value = css_serialize_default_value(&value)
    return CssFunctionParameter(name = internal.name, type_ = css_syntax_node_to_string(internal.type_), default_value = default_value)
    # name
    # The name of the function parameter.

    # type
    # The type of the function parameter, represented as a syntax string, or "*" if the parameter has no type.

    # defaultValue
    # The default value of the function parameter, or `null` if the argument does not have a default.

## serialize_a_css_type's is_single_syntax_component lambda.
func css_is_single_syntax_component(type_: CssSyntaxNode*) -> bool:
    let node_type = css_syntax_node_type(type_)
    if node_type == CssSyntaxNodeNodeType.universal:
        return true

    if node_type == CssSyntaxNodeNodeType.ident or node_type == CssSyntaxNodeNodeType.type_:
        return true

    if node_type == CssSyntaxNodeNodeType.multiplier or node_type == CssSyntaxNodeNodeType.comma_separated_multiplier:
        let child_type = css_syntax_node_type(css_multiplier_syntax_node_child((CssMultiplierSyntaxNode*)type_))
        if child_type == CssSyntaxNodeNodeType.ident or child_type == CssSyntaxNodeNodeType.type_:
            return true

    return false

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:35 Web::CSS::serialize_a_css_type.
# https://drafts.csswg.org/css-mixins-1/#serialize-a-css-type
func css_serialize_a_css_type(builder: ak.StringBuilder*, type_: CssSyntaxNode*):
    # To serialize a CSS type, return the concatenation of the following:

    # If the <css-type> consists of a single <syntax-component>, return the corresponding syntax string.
    let is_single_syntax_component = css_is_single_syntax_component(type_)

    let syntax_string = css_syntax_node_to_string(type_)
    if is_single_syntax_component:
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&syntax_string))
        return

    # Otherwise, return the concatenation of the following:
    # The string "type(", i.e. "type" followed by a single LEFT PARENTHESIS (U+0028).
    ak.string_builder_append(builder, "type(")

    # The corresponding syntax string.
    ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&syntax_string))

    # The string ")", i.e. a single RIGHT PARENTHESIS (U+0029).
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:72 Web::CSS::FunctionParameterInternal::serialize.
# https://drafts.csswg.org/css-mixins-1/#serialize-a-function-parameter
pub func css_function_parameter_internal_serialize(this: const CssFunctionParameterInternal*, builder: ak.StringBuilder*):
    # To serialize a function parameter, return the concatenation of the following:

    # The result of performing serialize an identifier on the name of the function parameter.
    css_syntax.serialize_an_identifier_string_builder(builder, ak.fly_string_bytes_as_string_view(&this.name))

    # If the function parameter has a type, and that type is not the universal syntax definition:
    if css_syntax_node_type(this.type_) != CssSyntaxNodeNodeType.universal:
        # - A single SPACE (U+0020), followed by the result of performing serialize a CSS type on that type.
        ak.string_builder_append_u8(builder, ' ')
        css_serialize_a_css_type(builder, this.type_)

    # If the function parameter has a default value:
    if let default_value = this.default_value:
        # - A single COLON (U+003A), followed by a single SPACE (U+0020), followed by the result of performing
        #   serialize a CSS value on that value.
        let text = css_serialize_default_value(&default_value)
        dump_appendff(builder, ": {}", [css_arg_string(&text)])

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:92 Web::CSS::CSSFunctionRule::create.
pub func css_function_rule_create(realm: Realm*, rules: CssRuleList*, name: ak.FlyString, parameters: ak.Vector[CssFunctionParameterInternal], return_type: CssSyntaxNode*) -> CssFunctionRule*:
    return realm_create_css_function_rule(realm, realm, rules, name, parameters, return_type)

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:97 Web::CSS::CSSFunctionRule::CSSFunctionRule.
pub func css_function_rule_construct(this: CssFunctionRule*, realm: Realm*, rules: CssRuleList*, name: ak.FlyString, parameters: ak.Vector[CssFunctionParameterInternal], return_type: CssSyntaxNode*):
    css_grouping_rule_construct(this.css_grouping_rule(), realm, rules, CssRuleType.function)
    this.m_name = name
    this.m_parameters = parameters
    this.m_return_type = return_type

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:108 Web::CSS::CSSFunctionRule::initialize (override).
pub func css_function_rule_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.css_function_rule)
    css_grouping_rule_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:112 Web::CSS::CSSFunctionRule::get_parameters.
# https://drafts.csswg.org/css-mixins-1/#dom-cssfunctionrule-getparameters
pub func css_function_rule_get_parameters(this: CssFunctionRule*) -> ak.Vector[CssFunctionParameter]:
    var parameters: ak.Vector[CssFunctionParameter]
    ak.vector_ensure_capacity(&parameters, ak.vector_size(&this.m_parameters))

    for parameter in ak.vector_span(&this.m_parameters):
        ak.vector_append_t(&parameters, css_function_parameter_from_internal_function_parameter(parameter))

    return parameters

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:124 Web::CSS::CSSFunctionRule::return_type.
# https://drafts.csswg.org/css-mixins-1/#dom-cssfunctionrule-returntype
pub func css_function_rule_return_type(this: CssFunctionRule*) -> ak.String:
    # The return type of the custom function, represented as a syntax string. If the custom function has no return
    # type, returns "*".
    # NB: We always store a return type (defaulting to "*")
    return css_syntax_node_to_string(this.m_return_type)

## Ported from Libraries/LibWeb/CSS/CSSFunctionRule.cpp:133 Web::CSS::CSSFunctionRule::serialized (override).
# https://drafts.csswg.org/css-mixins-1/#serialize-a-cssfunctionrule
pub func css_function_rule_serialized_impl(base: CssRule*) -> ak.String:
    let this = (CssFunctionRule*)base
    # To serialize a CSSFunctionRule, return the concatenation of the following:
    var builder = ak.string_builder_new()

    # 1. The string "@function" followed by a single SPACE (U+0020).
    ak.string_builder_append(&builder, "@function ")

    # 2. The result of performing serialize an identifier on the name of the custom function, followed by a single LEFT
    #    PARENTHESIS (U+0028).
    css_syntax.serialize_an_identifier_string_builder(&builder, ak.fly_string_bytes_as_string_view(&this.m_name))
    ak.string_builder_append_u8(&builder, '(')

    # 3. The result of serialize a function parameter on each of the custom function’s parameters, all joined by ", "
    #    (COMMA U+002C, followed by a single SPACE U+0020).
    for i in 0..<ak.vector_size(&this.m_parameters):
        if i > 0:
            ak.string_builder_append(&builder, ", ")
        css_function_parameter_internal_serialize(ak.vector_at(&this.m_parameters, i), &builder)

    # 4. A single RIGHT PARENTHESIS (U+0029).
    ak.string_builder_append_u8(&builder, ')')

    # 5. If the custom function has return type, and that return type is not the universal syntax definition ("*"):
    if css_syntax_node_type(this.m_return_type) != CssSyntaxNodeNodeType.universal:
        # - A single SPACE (U+0020), followed by the string "returns", followed by a single SPACE (U+0020).
        ak.string_builder_append(&builder, " returns ")

        # - The result of performing serialize a CSS type on that type.
        css_serialize_a_css_type(&builder, this.m_return_type)

    # 6. A single SPACE (U+0020), followed by a LEFT CURLY BRACKET (U+007B).
    ak.string_builder_append(&builder, " {")

    # 7. The result of performing serialize a CSS rule on each rule in cssRules, filtering out empty strings, each
    #    preceded by a single SPACE (U+0020).
    let rules = css_grouping_rule_css_rules(this.css_grouping_rule())

    for i in 0..<css_rule_list_length(rules):
        let rule = css_rule_list_item(rules, i) else trap("null rule")

        let serialized_rule = css_rule_serialized(rule)

        if not ak.string_is_empty(&serialized_rule):
            ak.string_builder_append_u8(&builder, ' ')
            ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&serialized_rule))

    # 8. A single SPACE (U+0020), followed by a single RIGHT CURLY BRACKET (U+007D).
    ak.string_builder_append(&builder, " }")

    return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")