Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / invalidation / has_mutation_feature_collector.lucb

#==============================================================================================
#
#   has_mutation_feature_collector - whether a mutated subtree has any feature that a :has()
#                                    argument of a style scope uses
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp (lines 1-103).
#
#       Copyright (c) 2026-present, the Ladybird developers
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp:18 Web::CSS::Invalidation::HasMutationFeatureCollector::HasMutationFeatureCollector.
pub func css_has_mutation_feature_collector_construct(this: CssHasMutationFeatureCollector*, data: const CssStyleInvalidationData*):
    this.m_data = data

## Ported from Libraries/LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp:23 Web::CSS::Invalidation::HasMutationFeatureCollector::has_any_metadata.
pub func css_has_mutation_feature_collector_has_any_metadata(this: const CssHasMutationFeatureCollector*) -> bool:
    let data = this.m_data
    return not ak.hash_map_is_empty(&data.ids_used_in_has_selectors) or not ak.hash_map_is_empty(&data.class_names_used_in_has_selectors) or not ak.hash_map_is_empty(&data.attribute_names_used_in_has_selectors) or not ak.hash_map_is_empty(&data.tag_names_used_in_has_selectors) or not ak.hash_map_is_empty(&data.pseudo_classes_used_in_has_selectors) or data.has_selectors_sensitive_to_featureless_subtree_changes

## Ported from Libraries/LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp:33 Web::CSS::Invalidation::HasMutationFeatureCollector::element_has_feature_used_in_has_selector.
func css_has_mutation_feature_collector_element_has_feature_used_in_has_selector(this: const CssHasMutationFeatureCollector*, element: DomElement*) -> bool:
    let data = this.m_data
    if ak.hash_map_contains_k(&data.tag_names_used_in_has_selectors, dom_element_local_name(element)):
        return true
    # Selector metadata stores tag and attribute names lowercased. For non-HTML elements this is only a conservative
    # scheduling hint; the actual :has() match still uses selector matching with the element's namespace semantics.
    let is_html = css_optional_fly_strings_equal(dom_element_namespace_uri(element), namespace_html)
    if not is_html and ak.hash_map_contains_k(&data.tag_names_used_in_has_selectors, dom_element_lowercased_local_name(element)):
        return true
    if let id = dom_element_id(element):
        if ak.hash_map_contains_k(&data.ids_used_in_has_selectors, id):
            return true
    let class_names = dom_element_class_names(element)
    for class_name in ak.vector_span_const(&class_names):
        if ak.hash_map_contains_k(&data.class_names_used_in_has_selectors, class_name):
            return true
    var attribute_used_in_has = false
    # element.for_each_attribute([&](FlyString const& name, String const&) { ... })
    if let attributes = element.m_attributes:
        for i in 0..<dom_named_node_map_length(attributes):
            let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
            let name = dom_attr_name(attribute)
            if ak.hash_map_contains_k(&data.attribute_names_used_in_has_selectors, name):
                attribute_used_in_has = true
            if not is_html and ak.hash_map_contains_k(&data.attribute_names_used_in_has_selectors, ak.fly_string_to_ascii_lowercase(&name)):
                attribute_used_in_has = true
    if attribute_used_in_has:
        return true
    for pseudo_class_entry in ak.hash_map_begin_const(&data.pseudo_classes_used_in_has_selectors):
        var pseudo_class_set: CssInvalidationSet
        css_invalidation_set_set_needs_invalidate_pseudo_class(&pseudo_class_set, pseudo_class_entry.key)
        if css_invalidation_element_matches_any_invalidation_set_property(element, &pseudo_class_set):
            return true
    return false

## Ported from Libraries/LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp:67 Web::CSS::Invalidation::HasMutationFeatureCollector::subtree_has_feature_used_in_has_selector.
pub func css_has_mutation_feature_collector_subtree_has_feature_used_in_has_selector(this: const CssHasMutationFeatureCollector*, node: DomNode*) -> bool:
    # Some :has() arguments can match because a node exists, stops existing, or changes position, even when that
    # node has no tag/class/id/attribute/pseudo-class metadata we can compare against. Examples include :has(*),
    # :has(:not(.x)), :has(:empty), and child-index pseudo-classes. Keep the old conservative walk for those.
    if this.m_data.has_selectors_sensitive_to_featureless_subtree_changes:
        return true

    var found = false
    for descendant in dom_node_inclusive_subtree(node):
        if dom_node_is_character_data(descendant):
            found = true
            break
        if let element = as_if_dom_element(descendant):
            if css_has_mutation_feature_collector_element_has_feature_used_in_has_selector(this, element):
                found = true
                break
    return found

## Ported from Libraries/LibWeb/CSS/Invalidation/HasMutationFeatureCollector.cpp:90 Web::CSS::Invalidation::subtree_has_feature_used_in_has_selector.
pub func css_invalidation_subtree_has_feature_used_in_has_selector(node: DomNode*, style_scope: const CssStyleScope*) -> bool:
    let rule_cache = style_scope.m_rule_cache else return true
    let data = &rule_cache.style_invalidation_data

    let collector = CssHasMutationFeatureCollector(m_data = data)
    if not css_has_mutation_feature_collector_has_any_metadata(&collector):
        return true

    return css_has_mutation_feature_collector_subtree_has_feature_used_in_has_selector(&collector, node)