Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / css.lucb

#==============================================================================================
#
#   css - the CSS namespace of the CSSOM: CSS.escape(), CSS.supports(), CSS.registerProperty()
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSS.cpp (lines 1-156).
#
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/CSS.cpp:24 Web::CSS::escape.
# https://www.w3.org/TR/cssom-1/#dom-css-escape
pub func css_escape(arg0: Vm*, identifier: ak.StringView) -> ak.String!:
    _ = arg0
    # The escape(ident) operation must return the result of invoking serialize an identifier of ident.
    return css_syntax.serialize_an_identifier_string_view(identifier)

## Ported from Libraries/LibWeb/CSS/CSS.cpp:31 Web::CSS::supports.
# https://www.w3.org/TR/css-conditional-3/#dom-css-supports
pub func css_supports_fly_string(arg0: Vm*, property_name: ak.FlyString, value: ak.StringView) -> bool:
    _ = arg0
    # 1. If property is an ASCII case-insensitive match for any defined CSS property that the UA supports, or is a
    #    custom property name string, and value successfully parses according to that property’s grammar, return true.
    if let property = css_property_name_and_id_from_name(property_name):
        var parsing_params: CssParsingParams = ---
        css_parsing_params_construct_css_parsing_mode(&parsing_params)
        if parse_css_value(parsing_params, value, css_property_name_and_id_id(&property)) != none:
            return true

    # 2. Otherwise, return false.
    return false

## Ported from Libraries/LibWeb/CSS/CSS.cpp:45 Web::CSS::supports.
# https://www.w3.org/TR/css-conditional-3/#dom-css-supports
pub func css_supports_string_view(vm: Vm*, condition_text: ak.StringView) -> bool!:
    let realm = vm_current_realm(vm) else trap("null current realm")
    var parsing_params: CssParsingParams = ---
    css_parsing_params_construct_realm_css_parsing_mode(&parsing_params, realm)

    # 1. If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
    if let supports = parse_css_supports(parsing_params, condition_text):
        if css_supports_matches(supports):
            return true

    # 2. Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
    let wrapped_condition_text = css_string_formatted("({})", [ak.arg_string_view(condition_text)])

    if let supports = parse_css_supports(parsing_params, ak.string_bytes_as_string_view(&wrapped_condition_text)):
        if css_supports_matches(supports):
            return true

    # 3. Otherwise, return false.
    return false

## Ported from Libraries/LibWeb/CSS/CSS.cpp:64 Web::CSS::register_property.
# https://www.w3.org/TR/css-properties-values-api-1/#the-registerproperty-function
pub func css_register_property(vm: Vm*, definition: CssPropertyDefinition) -> !:
    # 1. Let property set be the value of the current global object’s associated Document’s [[registeredPropertySet]] slot.
    let realm = vm_current_realm(vm) else trap("null current realm")
    let window = (HtmlWindow*)realm_global_object(realm)
    let document = html_window_associated_document(window)
    let property_set = dom_document_registered_property_set(document)

    # 2. If name is not a custom property name string, throw a SyntaxError and exit this algorithm.
    if not css_data.is_a_custom_property_name_string(ak.string_bytes_as_string_view(&definition.name)):
        try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Invalid property name"))))

    # If property set already contains an entry with name as its property name (compared codepoint-wise),
    # throw an InvalidModificationError and exit this algorithm.
    let name = ak.fly_string_from_string(definition.name)
    if ak.hash_map_contains_k(property_set, name):
        try idl_throw(.idl_dom_exception(idl_invalid_modification_error_create(realm, ak.utf16_string_from_str("Property already registered"))))

    var parsing_params: CssParsingParams = ---
    css_parsing_params_construct_dom_document(&parsing_params, document)

    # 3. Attempt to consume a syntax definition from syntax. If it returns failure, throw a SyntaxError.
    #    Otherwise, let syntax definition be the returned syntax definition.
    let syntax_component_values = parse_component_values_list(parsing_params, ak.string_bytes_as_string_view(&definition.syntax))
    let maybe_syntax = css_parse_as_syntax(syntax_component_values, CssLimitSingleComponentIdentToCustomIdent.yes)
    if maybe_syntax == none:
        try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Invalid syntax definition"))))
    let syntax = maybe_syntax else trap("unreachable")

    var initial_value_maybe: CssStyleValue*? = none

    # 4. If syntax definition is the universal syntax definition, and initialValue is not present,
    if css_syntax_node_type(syntax) == CssSyntaxNodeNodeType.universal:
        if let initial_value = definition.initial_value:
            # Otherwise, if syntax definition is the universal syntax definition,
            # parse initialValue as a <declaration-value>

            # AD-HOC: Parse this as the equivalent descriptor value in order to disallow arbitrary substitution functions.
            initial_value_maybe = parse_css_descriptor(parsing_params, css_data.AtRuleId.property, css_descriptor_name_and_id_from_id(css_data.DescriptorId.initial_value), ak.string_bytes_as_string_view(&initial_value))

            # If this fails, throw a SyntaxError and exit this algorithm.
            # Otherwise, let parsed initial value be the parsed result.
            if initial_value_maybe == none:
                try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Invalid initial value"))))
        else:
            # let parsed initial value be empty.
            # This must be treated identically to the "default" initial value of custom properties, as defined in [css-variables].
            initial_value_maybe = none
    elif definition.initial_value == none:
        # Otherwise, if initialValue is not present, throw a SyntaxError and exit this algorithm.
        try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Initial value must be provided for non-universal syntax"))))
    else:
        # Otherwise, parse initialValue according to syntax definition.
        # NB: We don't need to worry about explicitly rejecting arbitrary substitution functions here since all
        #     supported syntaxes implicitly reject them
        let initial_value = definition.initial_value else trap("unreachable")
        let initial_value_component_values = parse_component_values_list(parsing_params, ak.string_bytes_as_string_view(&initial_value))

        let parsed = css_parse_with_a_syntax(parsing_params, initial_value_component_values, syntax)
        initial_value_maybe = parsed

        # If this fails, throw a SyntaxError and exit this algorithm.
        if css_style_value_is_guaranteed_invalid(parsed):
            try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Invalid initial value"))))
        # Otherwise, let parsed initial value be the parsed result.
        # NB: Already done

        # If parsed initial value is not computationally independent, throw a SyntaxError and exit this algorithm.
        if not css_style_value_is_computationally_independent(parsed):
            try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Initial value must be computationally independent"))))

    # 5. Set inherit flag to the value of inherits.
    # NB: Combined with 6.

    # 6. Let registered property be a struct with a property name of name, a syntax of syntax definition,
    #    an initial value of parsed initial value, and an inherit flag of inherit flag.
    let registered_property = CssCustomPropertyRegistration(property_name = name, syntax = definition.syntax, inherit = definition.inherits, initial_value = initial_value_maybe)
    # Append registered property to property set.
    _ = ak.hash_map_set(property_set, registered_property.property_name, registered_property)
    dom_document_did_change_custom_property_registrations(document)