Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / css_rule.lucb

#==============================================================================================
#
#   css_rule - CSS::CSSRule: the base of every CSS rule (type, parent rule and style sheet)
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSSRule.cpp (lines 1-143) and the inline members of CSSRule.h
#       (lines 20-90): the rule's type, cssText, the parent rule and parent style sheet links,
#       the dump, and the cached qualified name of the cascade layer the rule is in.
#
#       Copyright (c) 2021, the SerenityOS developers.
#       Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: CSSRule.h ===============================================================================

## Ported from Libraries/LibWeb/CSS/CSSRule.h:50 Web::CSS::CSSRule::type.
pub func css_rule_type(this: CssRule*) -> CssRuleType:
    return this.m_type

## Ported from Libraries/LibWeb/CSS/CSSRule.h:56 Web::CSS::CSSRule::parent_rule.
pub func css_rule_parent_rule(this: CssRule*) -> CssRule*?:
    return this.m_parent_rule

## Ported from Libraries/LibWeb/CSS/CSSRule.h:57 Web::CSS::CSSRule::parent_rule (const).
pub func css_rule_parent_rule_const(this: CssRule*) -> CssRule*?:
    return this.m_parent_rule

## Ported from Libraries/LibWeb/CSS/CSSRule.h:60 Web::CSS::CSSRule::parent_style_sheet.
pub func css_rule_parent_style_sheet(this: CssRule*) -> CssCssStyleSheet*?:
    return this.m_parent_style_sheet

## Ported from Libraries/LibWeb/CSS/CSSRule.h:67 Web::CSS::CSSRule::serialized (pure virtual).
# https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
pub func css_rule_serialized_impl(this: CssRule*) -> ak.String:
    _ = this
    trap("pure virtual: Web::CSS::CSSRule::serialized")

## Ported from Libraries/LibWeb/CSS/CSSRule.h:78 Web::CSS::CSSRule::parent_layer_internal_qualified_name.
pub func css_rule_parent_layer_internal_qualified_name(this: CssRule*) -> ak.FlyString:
    # m_cached_layer_name.ensure([&] { return parent_layer_internal_qualified_name_slow_case(); })
    if let cached = this.m_cached_layer_name:
        return cached
    let name = css_rule_parent_layer_internal_qualified_name_slow_case(this)
    this.m_cached_layer_name = name
    return name

# mark: CSSRule.cpp =============================================================================

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:17 Web::CSS::CSSRule::CSSRule.
pub func css_rule_construct(this: CssRule*, realm: Realm*, type_: CssRuleType):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)
    this.m_type = type_

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:23 Web::CSS::CSSRule::visit_edges (override).
pub func css_rule_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (CssRule*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_parent_style_sheet)
    gc.cell_visitor_visit_t(visitor, this.m_parent_rule)

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:31 Web::CSS::CSSRule::type_for_bindings.
# https://www.w3.org/TR/cssom/#dom-cssrule-type
pub func css_rule_type_for_bindings(this: CssRule*) -> u16:
    # NOTE: Types that aren't defined in the spec must return 0.
    # To do this, we arbitrarily make non-spec ones start at 100.
    let type_ = (u16)this.m_type
    if type_ >= 100:
        return 0
    return type_

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:42 Web::CSS::CSSRule::css_text.
# https://www.w3.org/TR/cssom/#dom-cssrule-csstext
pub func css_rule_css_text(this: CssRule*) -> ak.String:
    # The cssText attribute must return a serialization of the CSS rule.
    return css_rule_serialized(this)

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:49 Web::CSS::CSSRule::set_css_text.
# https://www.w3.org/TR/cssom/#dom-cssrule-csstext
pub func css_rule_set_css_text(this: CssRule*, arg0: ak.StringView):
    # On setting the cssText attribute must do nothing.
    _ = this
    _ = arg0

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:54 Web::CSS::CSSRule::set_parent_rule.
pub func css_rule_set_parent_rule(this: CssRule*, parent_rule: CssRule*?):
    css_rule_clear_caches(this)
    this.m_parent_rule = parent_rule

    if let parent = parent_rule:
        css_rule_set_parent_style_sheet(this, css_rule_parent_style_sheet(parent))
    else:
        css_rule_set_parent_style_sheet(this, none)
    css_rule_clear_caches(this)

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:66 Web::CSS::CSSRule::set_parent_style_sheet (virtual).
pub func css_rule_set_parent_style_sheet_impl(this: CssRule*, parent_style_sheet: CssCssStyleSheet*?):
    css_rule_clear_caches(this)
    this.m_parent_style_sheet = parent_style_sheet
    css_rule_clear_caches(this)

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:73 Web::CSS::CSSRule::dump (virtual).
pub func css_rule_dump_impl(this: CssRule*, builder: ak.StringBuilder*, indent_levels: i32):
    dump_indent(builder, indent_levels)
    dump_appendff(builder, "{}:\n", [ak.arg_string_view(dump_class_name(this.cell()))])

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:79 Web::CSS::CSSRule::clear_caches (virtual).
pub func css_rule_clear_caches_impl(this: CssRule*):
    this.m_cached_layer_name = none

## Ported from Libraries/LibWeb/CSS/CSSRule.cpp:84 Web::CSS::CSSRule::parent_layer_internal_qualified_name_slow_case.
pub func css_rule_parent_layer_internal_qualified_name_slow_case(this: CssRule*) -> ak.FlyString:
    var layer_names: ak.Vector[ak.FlyString]
    var rule = css_rule_parent_rule(this)
    while let current = rule:
        # switch (rule->type())
        if current.m_type == CssRuleType.import_:
            # @import is only a parent to style sheets, not to rules directly. It's handled below this loop.
            trap("VERIFY_NOT_REACHED")
        elif current.m_type == CssRuleType.layer_block:
            let layer_block = as_css_layer_block_rule(current)
            ak.vector_append_t(&layer_names, css_layer_block_rule_internal_name(layer_block))
        # Ignore everything else
        # Note that LayerStatement cannot have child rules so we still ignore it here.
        rule = css_rule_parent_rule(current)

    # If this style sheet is owned by a rule, include its qualified layer name.
    if let parent_style_sheet = this.m_parent_style_sheet:
        if let owner_rule = css_css_style_sheet_owner_rule(parent_style_sheet):
            if let import_ = as_if_css_import_rule(owner_rule):
                # https://drafts.csswg.org/css-cascade-5/#at-import
                # The layer is added to the layer order even if the import fails to load the stylesheet, but is subject to
                # any import conditions (just as if declared by an @layer rule wrapped in the appropriate conditional
                # group rules).
                if let layer_name = css_import_rule_internal_layer_name(import_):
                    if css_import_rule_matches(import_):
                        ak.vector_append_t(&layer_names, layer_name)
                        let parent_qualified_layer_name = css_rule_parent_layer_internal_qualified_name(owner_rule)
                        if not ak.fly_string_is_empty(&parent_qualified_layer_name):
                            ak.vector_append_t(&layer_names, parent_qualified_layer_name)

    # MUST(String::join('.', layer_names.in_reverse()))
    ak.vector_reverse(&layer_names)
    var builder = ak.string_builder_new()
    ak.string_builder_join_fly_string(&builder, ak.sv("."), ak.vector_span_const(&layer_names))
    return ak.string_builder_to_fly_string_without_validation(&builder)