Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / css_style_sheet_2.lucb

#==============================================================================================
#
#   css_style_sheet_2 - CSS::CSSStyleSheet: effective rules, owners, namespaces, loading state
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSSStyleSheet.cpp (lines 324-665) and the inline members of
#       CSSStyleSheet.h (lines 38-168): the traversal of the effective rules, the owning
#       documents and shadow roots, enabling and disabling, owner invalidation, media query
#       evaluation, the namespace and @import caches, critical subresources (Subresource) and
#       the parsing parameters of the sheet.
#
#       Copyright (c) 2019-2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2022-2026, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2024-2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: CSSStyleSheet.h =========================================================================

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:55 Web::CSS::CSSStyleSheet::Subresource::parent_style_sheet_for_subresource (pure virtual).
pub func css_style_sheet_subresource_parent_style_sheet_for_subresource_impl(this: CssStyleSheetSubresource*) -> CssCssStyleSheet*?:
    _ = this
    trap("pure virtual: Web::CSS::CSSStyleSheet::Subresource::parent_style_sheet_for_subresource")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:56 Web::CSS::CSSStyleSheet::Subresource::loading_state.
pub func css_style_sheet_subresource_loading_state(this: CssStyleSheetSubresource*) -> CssStyleSheetLoadingState:
    return this.m_loading_state

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:57 Web::CSS::CSSStyleSheet::Subresource::visit_edges (pure virtual).
pub func css_style_sheet_subresource_visit_edges_impl(this: CssStyleSheetSubresource*, arg0: gc.CellVisitor*):
    _ = this
    _ = arg0
    trap("pure virtual: Web::CSS::CSSStyleSheet::Subresource::visit_edges")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:70 Web::CSS::CSSStyleSheet::owner_rule (const).
pub func css_css_style_sheet_owner_rule_const(this: CssCssStyleSheet*) -> CssRule*?:
    return this.m_owner_css_rule

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:71 Web::CSS::CSSStyleSheet::owner_rule.
pub func css_css_style_sheet_owner_rule(this: CssCssStyleSheet*) -> CssRule*?:
    return this.m_owner_css_rule

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:72 Web::CSS::CSSStyleSheet::set_owner_css_rule.
pub func css_css_style_sheet_set_owner_css_rule(this: CssCssStyleSheet*, rule: CssRule*?):
    this.m_owner_css_rule = rule

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:74 Web::CSS::CSSStyleSheet::type (override).
pub func css_css_style_sheet_type_impl(base: CssStyleSheet*) -> ak.String:
    _ = base
    return ak.string_from_str("text/css")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:76 Web::CSS::CSSStyleSheet::rules (const).
pub func css_css_style_sheet_rules_const(this: CssCssStyleSheet*) -> CssRuleList*:
    return this.m_rules else trap("null m_rules")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:77 Web::CSS::CSSStyleSheet::rules.
pub func css_css_style_sheet_rules(this: CssCssStyleSheet*) -> CssRuleList*:
    return this.m_rules else trap("null m_rules")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:79 Web::CSS::CSSStyleSheet::css_rules.
pub func css_css_style_sheet_css_rules(this: CssCssStyleSheet*) -> CssRuleList*?:
    return this.m_rules

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:80 Web::CSS::CSSStyleSheet::css_rules (const).
pub func css_css_style_sheet_css_rules_const(this: CssCssStyleSheet*) -> CssRuleList*?:
    return this.m_rules

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:97 Web::CSS::CSSStyleSheet::owning_documents_or_shadow_roots.
pub func css_css_style_sheet_owning_documents_or_shadow_roots(this: CssCssStyleSheet*) -> ak.HashTable[DomNode*?, ak.PtrTraits[DomNode]]:
    return this.m_owning_documents_or_shadow_roots

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:107 Web::CSS::CSSStyleSheet::default_namespace_rule.
pub func css_css_style_sheet_default_namespace_rule(this: CssCssStyleSheet*) -> CssNamespaceRule*?:
    return this.m_default_namespace_rule

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:112 Web::CSS::CSSStyleSheet::import_rules.
pub func css_css_style_sheet_import_rules(this: CssCssStyleSheet*) -> ak.Vector[CssImportRule*]:
    return this.m_import_rules

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:115 Web::CSS::CSSStyleSheet::base_url.
pub func css_css_style_sheet_base_url(this: CssCssStyleSheet*) -> web_url.Url?:
    return this.m_base_url

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:115 Web::CSS::CSSStyleSheet::set_base_url.
pub func css_css_style_sheet_set_base_url(this: CssCssStyleSheet*, base_url: web_url.Url?):
    this.m_base_url = base_url

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:117 Web::CSS::CSSStyleSheet::register_pending_image_value.
pub func css_css_style_sheet_register_pending_image_value(this: CssCssStyleSheet*, value: CssImageStyleValue*):
    ak.vector_append_t(&this.m_pending_image_values, css_weak_image_style_value(value))

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:120 Web::CSS::CSSStyleSheet::constructed.
pub func css_css_style_sheet_constructed(this: CssCssStyleSheet*) -> bool:
    return this.m_constructed

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:123 Web::CSS::CSSStyleSheet::constructor_document.
pub func css_css_style_sheet_constructor_document(this: CssCssStyleSheet*) -> DomDocument*?:
    return this.m_constructor_document

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:123 Web::CSS::CSSStyleSheet::set_constructor_document.
pub func css_css_style_sheet_set_constructor_document(this: CssCssStyleSheet*, constructor_document: DomDocument*?):
    this.m_constructor_document = constructor_document

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:125 Web::CSS::CSSStyleSheet::disallow_modification.
pub func css_css_style_sheet_disallow_modification(this: CssCssStyleSheet*) -> bool:
    return this.m_disallow_modification

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:144 Web::CSS::CSSStyleSheet::set_constructed.
func css_css_style_sheet_set_constructed(this: CssCssStyleSheet*, constructed: bool):
    this.m_constructed = constructed

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.h:145 Web::CSS::CSSStyleSheet::set_disallow_modification.
func css_css_style_sheet_set_disallow_modification(this: CssCssStyleSheet*, disallow_modification: bool):
    this.m_disallow_modification = disallow_modification

# mark: Weak references to image style values ===================================================

# m_pending_image_values holds WeakPtr<ImageStyleValue>s (IGNORE_GC). A style value is a blob of
# the heap (DESIGN.md ยง3.3): a Cell header followed by the value, so its Weak is made to the header
# and read back past it.

## `WeakPtr<ImageStyleValue>(value)`.
func css_weak_image_style_value(value: CssImageStyleValue*) -> gc.Weak[CssImageStyleValue]:
    let header = (gc.Cell*)((u8*)value - gc.blob_header_size)
    let weak = gc.make_weak(header)
    return gc.Weak[CssImageStyleValue](m_impl = weak.m_impl, m_generation = weak.m_generation)

## `weak_image_value.ptr()`.
func css_weak_image_style_value_ptr(weak: const gc.Weak[CssImageStyleValue]*) -> CssImageStyleValue*?:
    let header = gc.weak_ptr(weak) else return none
    return (CssImageStyleValue*)((u8*)header + gc.blob_header_size)

# mark: Effective rules =========================================================================

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:325 Web::CSS::CSSStyleSheet::for_each_effective_rule.
pub func css_css_style_sheet_for_each_effective_rule(this: CssCssStyleSheet*, order: TraversalOrder, callback: ak.Function1[CssRule*, unit]):
    if css_media_list_matches(this.style_sheet.m_media):
        css_rule_list_for_each_effective_rule(this.m_rules else trap("null m_rules"), order, callback)

## The filtering lambda of for_each_effective_style_producing_rule (captures: the callback).
func css_css_style_sheet_style_producing_rule_filter(context: void*?, rule: CssRule*):
    let callback = (ak.Function1[CssRule*, unit]*)(context else trap("no callback"))
    if css_rule_type(rule) == CssRuleType.style or css_rule_type(rule) == CssRuleType.nested_declarations:
        ak.function1_call(callback, rule)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:331 Web::CSS::CSSStyleSheet::for_each_effective_style_producing_rule.
pub func css_css_style_sheet_for_each_effective_style_producing_rule(this: CssCssStyleSheet*, callback: ak.Function1[CssRule*, unit]):
    var inner = callback
    css_css_style_sheet_for_each_effective_rule(this, TraversalOrder.preorder, ak.Function1[CssRule*, unit](call = css_css_style_sheet_style_producing_rule_filter, context = &inner))

## The filtering lambda of for_each_effective_keyframes_at_rule (captures: the callback).
func css_css_style_sheet_keyframes_rule_filter(context: void*?, rule: CssRule*):
    let callback = (ak.Function1[CssKeyframesRule*, unit]*)(context else trap("no callback"))
    if css_rule_type(rule) == CssRuleType.keyframes:
        ak.function1_call(callback, (CssKeyframesRule*)rule)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:339 Web::CSS::CSSStyleSheet::for_each_effective_keyframes_at_rule.
pub func css_css_style_sheet_for_each_effective_keyframes_at_rule(this: CssCssStyleSheet*, callback: ak.Function1[CssKeyframesRule*, unit]):
    var inner = callback
    css_css_style_sheet_for_each_effective_rule(this, TraversalOrder.preorder, ak.Function1[CssRule*, unit](call = css_css_style_sheet_keyframes_rule_filter, context = &inner))

## The filtering lambda of for_each_effective_counter_style_at_rule (captures: the callback).
func css_css_style_sheet_counter_style_rule_filter(context: void*?, rule: CssRule*):
    let callback = (ak.Function1[CssCounterStyleRule*, unit]*)(context else trap("no callback"))
    if css_rule_type(rule) == CssRuleType.counter_style:
        ak.function1_call(callback, (CssCounterStyleRule*)rule)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:347 Web::CSS::CSSStyleSheet::for_each_effective_counter_style_at_rule.
pub func css_css_style_sheet_for_each_effective_counter_style_at_rule(this: CssCssStyleSheet*, callback: ak.Function1[CssCounterStyleRule*, unit]):
    var inner = callback
    css_css_style_sheet_for_each_effective_rule(this, TraversalOrder.preorder, ak.Function1[CssRule*, unit](call = css_css_style_sheet_counter_style_rule_filter, context = &inner))

# mark: Owners ==================================================================================

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:355 Web::CSS::CSSStyleSheet::add_owning_document_or_shadow_root.
pub func css_css_style_sheet_add_owning_document_or_shadow_root(this: CssCssStyleSheet*, document_or_shadow_root: DomNode*):
    assert(dom_node_is_document(document_or_shadow_root) or dom_node_is_shadow_root(document_or_shadow_root))
    _ = ak.hash_table_set(&this.m_owning_documents_or_shadow_roots, document_or_shadow_root)

    # CSSOM's "add a CSS style sheet" steps bail out once the disabled flag is set, so ownership alone should not
    # make a disabled sheet observable in the destination document. Delay CSS-connected font activation until the
    # sheet actually becomes enabled.
    if not css_style_sheet_disabled(this.css_style_sheet()) and ak.hash_table_size(&this.m_owning_documents_or_shadow_roots) == 1:
        css_font_computer_load_fonts_from_sheet(dom_document_font_computer(dom_node_document(document_or_shadow_root)), this)

    for import_rule in ak.vector_span(&this.m_import_rules):
        if let loaded_style_sheet = css_import_rule_loaded_style_sheet(import_rule):
            css_css_style_sheet_add_owning_document_or_shadow_root(loaded_style_sheet, document_or_shadow_root)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:372 Web::CSS::CSSStyleSheet::remove_owning_document_or_shadow_root.
pub func css_css_style_sheet_remove_owning_document_or_shadow_root(this: CssCssStyleSheet*, document_or_shadow_root: DomNode*):
    _ = ak.hash_table_remove_t(&this.m_owning_documents_or_shadow_roots, document_or_shadow_root)

    # All owning documents or shadow roots must be part of the same document so we only need to unload this style
    # sheet's fonts once we have none remaining.
    if ak.hash_table_size(&this.m_owning_documents_or_shadow_roots) == 0:
        css_font_computer_unload_fonts_from_sheet(dom_document_font_computer(dom_node_document(document_or_shadow_root)), this)

    for import_rule in ak.vector_span(&this.m_import_rules):
        if let loaded_style_sheet = css_import_rule_loaded_style_sheet(import_rule):
            css_css_style_sheet_remove_owning_document_or_shadow_root(loaded_style_sheet, document_or_shadow_root)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:387 Web::CSS::CSSStyleSheet::set_disabled (override).
pub func css_css_style_sheet_set_disabled_impl(base: CssStyleSheet*, disabled: bool):
    let this = (CssCssStyleSheet*)base
    if css_style_sheet_disabled(base) == disabled:
        return

    let previous_sheet_effects = css_determine_shadow_root_stylesheet_effects_css_css_style_sheet(this)
    let document = css_css_style_sheet_owning_document(this)
    # When a stylesheet is disabled we stop evaluating its media queries, so both the cached top-level match bit
    # and the MediaList's internal state can go stale across viewport changes. Clear the cache for both
    # directions, and eagerly refresh on re-enable so subsequent rule-cache rebuilds see the current media state
    # instead of the pre-disable one.
    this.m_did_match = none
    css_style_sheet_set_disabled_impl(base, disabled)

    if not disabled:
        if let owning_document = document:
            _ = css_css_style_sheet_evaluate_media_queries(this, owning_document)
            css_font_computer_load_fonts_from_sheet(dom_document_font_computer(owning_document), this)
            css_css_style_sheet_load_pending_image_resources(this, owning_document)
    elif let owning_document = document:
        css_font_computer_unload_fonts_from_sheet(dom_document_font_computer(owning_document), this)

    css_css_style_sheet_invalidate_owners(this, DomStyleInvalidationReason.style_sheet_disabled_state_change, &previous_sheet_effects)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:414 Web::CSS::CSSStyleSheet::for_each_owning_style_scope.
pub func css_css_style_sheet_for_each_owning_style_scope(this: CssCssStyleSheet*, callback: ak.Function1[CssStyleScope*, unit]):
    for entry in ak.hash_table_begin_const(&this.m_owning_documents_or_shadow_roots):
        let document_or_shadow_root = *entry else trap("null owner")
        var style_scope: CssStyleScope* = ---
        if dom_node_is_shadow_root(document_or_shadow_root):
            style_scope = dom_shadow_root_style_scope(as_dom_shadow_root(document_or_shadow_root))
        else:
            style_scope = dom_document_style_scope(dom_node_document(document_or_shadow_root))

        ak.function1_call(&callback, style_scope)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:425 Web::CSS::CSSStyleSheet::shared_single_constructed_sheet_style_cache.
pub func css_css_style_sheet_shared_single_constructed_sheet_style_cache(this: CssCssStyleSheet*, style_scope: CssStyleScope*) -> CssStyleCache*:
    assert(css_css_style_sheet_constructed(this))
    if this.m_shared_single_constructed_sheet_style_cache == none:
        this.m_shared_single_constructed_sheet_style_cache = css_style_cache_create_for_style_scope(style_scope)
    return this.m_shared_single_constructed_sheet_style_cache else trap("unreachable")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:433 Web::CSS::CSSStyleSheet::invalidate_shared_style_cache.
func css_css_style_sheet_invalidate_shared_style_cache(this: CssCssStyleSheet*):
    this.m_shared_single_constructed_sheet_style_cache = none

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:438 Web::CSS::CSSStyleSheet::invalidate_owners.
pub func css_css_style_sheet_invalidate_owners(this: CssCssStyleSheet*, reason: DomStyleInvalidationReason, previous_sheet_effects: const CssShadowRootStylesheetEffects*? = none):
    this.m_did_match = none
    css_css_style_sheet_invalidate_shared_style_cache(this)

    # The MediaList may have been mutated (e.g. via MediaList::set_media_text), and owner invalidation computes
    # shadow-root effects from effective rules. Refresh the media state first so host-side shadow invalidation
    # sees the updated definitions.
    if let document = css_css_style_sheet_owning_document(this):
        _ = css_css_style_sheet_evaluate_media_queries(this, document)

    css_invalidate_style_for_style_sheet_owners(this, reason, CssShouldInvalidateRuleCache.yes, previous_sheet_effects)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:452 Web::CSS::CSSStyleSheet::owning_document.
pub func css_css_style_sheet_owning_document(this: CssCssStyleSheet*) -> DomDocument*?:
    if not ak.hash_table_is_empty(&this.m_owning_documents_or_shadow_roots):
        var it = ak.hash_table_begin_const(&this.m_owning_documents_or_shadow_roots)
        let first = it.next() else trap("unreachable")
        return dom_node_document(*first else trap("null owner"))

    if let element = css_style_sheet_owner_node(this.css_style_sheet()):
        return dom_node_document(element.dom_node())

    return none

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:463 Web::CSS::CSSStyleSheet::load_pending_image_resources.
pub func css_css_style_sheet_load_pending_image_resources(this: CssCssStyleSheet*, document: DomDocument*):
    if css_style_sheet_disabled(this.css_style_sheet()):
        return

    let pending = this.m_pending_image_values
    this.m_pending_image_values = ak.Vector[gc.Weak[CssImageStyleValue]]()
    for weak_image_value in ak.vector_span_const(&pending):
        if let image_value = css_weak_image_style_value_ptr(&weak_image_value):
            css_abstract_image_style_value_load_any_resources_dom_document(image_value.css_abstract_image_style_value(), document)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:475 Web::CSS::CSSStyleSheet::evaluate_media_queries.
pub func css_css_style_sheet_evaluate_media_queries(this: CssCssStyleSheet*, document: DomDocument*) -> bool:
    var any_media_queries_changed_match_state = false

    let now_matches = css_media_list_evaluate(this.style_sheet.m_media, document)
    # The first evaluation establishes the baseline. The sheet's rules already entered the cascade through
    # StyleSheetListAddSheet, AdoptedStyleSheetsList, or invalidate_owners (each of which performs its own
    # invalidation), so we don't need to also report a match-state change just because no prior result was
    # recorded.
    if let did_match = this.m_did_match:
        if did_match != now_matches:
            any_media_queries_changed_match_state = true
    if now_matches and css_rule_list_evaluate_media_queries(this.m_rules else trap("null m_rules"), document):
        any_media_queries_changed_match_state = true

    this.m_did_match = now_matches
    if any_media_queries_changed_match_state:
        css_css_style_sheet_invalidate_shared_style_cache(this)

    return any_media_queries_changed_match_state

# mark: Namespaces and the rule caches ==========================================================

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:495 Web::CSS::CSSStyleSheet::default_namespace.
pub func css_css_style_sheet_default_namespace(this: CssCssStyleSheet*) -> ak.FlyString?:
    if let rule = this.m_default_namespace_rule:
        return css_namespace_rule_namespace_uri(rule)

    return none

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:503 Web::CSS::CSSStyleSheet::declared_namespaces.
pub func css_css_style_sheet_declared_namespaces(this: CssCssStyleSheet*) -> ak.HashTable[ak.FlyString, ak.FlyStringTraits]:
    var declared_namespaces: ak.HashTable[ak.FlyString, ak.FlyStringTraits]

    for entry in ak.hash_map_begin_const(&this.m_namespace_rules):
        _ = ak.hash_table_set(&declared_namespaces, entry.key)

    return declared_namespaces

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:514 Web::CSS::CSSStyleSheet::namespace_uri.
pub func css_css_style_sheet_namespace_uri(this: CssCssStyleSheet*, namespace_prefix: ak.StringView) -> ak.FlyString?:
    # m_namespace_rules.get(namespace_prefix): the FlyString keys hash and compare as their bytes.
    let key = ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&namespace_prefix))
    let namespace_ = ak.hash_map_get_k_const(&this.m_namespace_rules, key) else return none
    return css_namespace_rule_namespace_uri(*namespace_ else trap("null namespace rule"))

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:522 Web::CSS::CSSStyleSheet::recalculate_rule_caches.
func css_css_style_sheet_recalculate_rule_caches(this: CssCssStyleSheet*):
    css_css_style_sheet_invalidate_shared_style_cache(this)

    this.m_default_namespace_rule = none
    ak.hash_map_clear(&this.m_namespace_rules)
    ak.vector_clear(&this.m_import_rules)

    let rules = this.m_rules else trap("null m_rules")
    for rule in ak.vector_span(&rules.m_rules):
        # "Any @import rules must precede all other valid at-rules and style rules in a style sheet
        # (ignoring @charset and @layer statement rules) and must not have any other valid at-rules
        # or style rules between it and previous @import rules, or else the @import rule is invalid."
        # https://drafts.csswg.org/css-cascade-5/#at-import
        #
        # "Any @namespace rules must follow all @charset and @import rules and precede all other
        # non-ignored at-rules and style rules in a style sheet.
        # ...
        # A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored."
        # https://drafts.csswg.org/css-namespaces/#syntax
        match css_rule_type(rule):
            .import_:
                # @import rules must appear before @namespace rules, so skip this if we've seen @namespace.
                if not ak.hash_map_is_empty(&this.m_namespace_rules):
                    continue
                ak.vector_append_t(&this.m_import_rules, as_css_import_rule(rule))
            .namespace:
                let namespace_rule = as_css_namespace_rule(rule)
                let namespace_uri = css_namespace_rule_namespace_uri(namespace_rule)
                let prefix = css_namespace_rule_prefix(namespace_rule)
                if not ak.fly_string_is_empty(&namespace_uri) and ak.fly_string_is_empty(&prefix):
                    this.m_default_namespace_rule = namespace_rule

                _ = ak.hash_map_set(&this.m_namespace_rules, prefix, namespace_rule)
            _:
                # Any other types mean that further @namespace rules are invalid, so we can stop here.
                return

# mark: Source text and loading state ===========================================================

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:565 Web::CSS::CSSStyleSheet::set_source_text.
pub func css_css_style_sheet_set_source_text(this: CssCssStyleSheet*, source: ak.String):
    this.m_source_text = source

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:570 Web::CSS::CSSStyleSheet::source_text.
pub func css_css_style_sheet_source_text(this: CssCssStyleSheet*) -> ak.String?:
    return this.m_source_text

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:575 Web::CSS::CSSStyleSheet::add_critical_subresource.
pub func css_css_style_sheet_add_critical_subresource(this: CssCssStyleSheet*, subresource: CssStyleSheetSubresource*):
    ak.vector_append_t(&this.m_critical_subresources, subresource)

## The predicate of remove_critical_subresource: `&it == &subresource`.
func css_css_style_sheet_is_subresource(context: void*?, it: const (CssStyleSheetSubresource*)*) -> bool:
    return (void*?)*it == context

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:580 Web::CSS::CSSStyleSheet::remove_critical_subresource.
pub func css_css_style_sheet_remove_critical_subresource(this: CssCssStyleSheet*, subresource: CssStyleSheetSubresource*):
    _ = ak.vector_remove_first_matching(&this.m_critical_subresources, ak.Function1[const (CssStyleSheetSubresource*)*, bool](call = css_css_style_sheet_is_subresource, context = subresource))
    css_css_style_sheet_check_if_loading_completed(this)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:586 Web::CSS::CSSStyleSheet::loading_state.
pub func css_css_style_sheet_loading_state(this: CssCssStyleSheet*) -> CssStyleSheetLoadingState:
    var any_loading = false
    var any_errors = false

    for subresource in ak.vector_span(&this.m_critical_subresources):
        match css_style_sheet_subresource_loading_state(subresource):
            .unloaded, .loading:
                any_loading = true
            .loaded:
                _ = subresource
            .error_:
                any_errors = true
            _:
                trap("VERIFY_NOT_REACHED")

    if any_loading:
        return CssStyleSheetLoadingState.loading

    if any_errors:
        return CssStyleSheetLoadingState.error_

    return CssStyleSheetLoadingState.loaded

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:613 Web::CSS::CSSStyleSheet::check_if_loading_completed.
pub func css_css_style_sheet_check_if_loading_completed(this: CssCssStyleSheet*):
    let state = css_css_style_sheet_loading_state(this)
    if state == CssStyleSheetLoadingState.loaded or state == CssStyleSheetLoadingState.error_:
        # We're finished loading, so propagate that to our owner.
        let owner_node = css_style_sheet_owner_node(this.css_style_sheet())
        var style_element: DomStyleElementBase*? = none
        var link_element: HtmlLinkElement*? = none
        if let node = owner_node:
            style_element = as_if_style_element_base(node)
            link_element = as_if_html_link_element(node)
        if let element = style_element:
            dom_style_element_base_finished_loading_critical_subresources(element, DomStyleElementBaseAnyFailed.yes if state == CssStyleSheetLoadingState.error_ else DomStyleElementBaseAnyFailed.no)
        elif let element = link_element:
            html_link_element_finished_loading_critical_style_subresources(element, HtmlLinkElementAnyFailed.yes if state == CssStyleSheetLoadingState.error_ else HtmlLinkElementAnyFailed.no)
        elif let owner_rule = css_css_style_sheet_owner_rule(this):
            if let import_rule = as_if_css_import_rule(owner_rule):
                css_style_sheet_subresource_set_loading_state(&import_rule.subresource, state)

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:628 Web::CSS::CSSStyleSheet::make_parsing_params.
func css_css_style_sheet_make_parsing_params(this: CssCssStyleSheet*) -> CssParsingParams:
    var parsing_params: CssParsingParams = ---
    css_parsing_params_construct_css_parsing_mode(&parsing_params)
    if let document = css_css_style_sheet_owning_document(this):
        css_parsing_params_construct_dom_document(&parsing_params, document)
    else:
        css_parsing_params_construct_realm_css_parsing_mode(&parsing_params, bind_platform_object_realm(this.bind_platform_object()))

    parsing_params.declared_namespaces = css_css_style_sheet_declared_namespaces(this)
    return parsing_params

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:640 Web::CSS::CSSStyleSheet::loading_state_name.
pub func css_css_style_sheet_loading_state_name(loading_state: CssStyleSheetLoadingState) -> ak.StringView:
    match loading_state:
        .unloaded: return ak.sv("Unloaded")
        .loading: return ak.sv("Loading")
        .loaded: return ak.sv("Loaded")
        .error_: return ak.sv("Error")
        _: trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/CSSStyleSheet.cpp:655 Web::CSS::CSSStyleSheet::Subresource::set_loading_state.
pub func css_style_sheet_subresource_set_loading_state(this: CssStyleSheetSubresource*, loading_state: CssStyleSheetLoadingState):
    this.m_loading_state = loading_state
    if loading_state == CssStyleSheetLoadingState.loaded or loading_state == CssStyleSheetLoadingState.error_:
        if let style_sheet = css_style_sheet_subresource_parent_style_sheet_for_subresource(this):
            css_css_style_sheet_check_if_loading_completed(style_sheet)