Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / invalidation / invalidation_set_matcher.lucb

#==============================================================================================
#
#   invalidation_set_matcher - whether an element has any of the features of an invalidation
#                              set
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Invalidation/InvalidationSetMatcher.cpp (lines 1-101).
#
#       Copyright (c) 2026-present, the Ladybird developers
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## element_matches_any_invalidation_set_property's `includes_property` lambda.
func css_invalidation_element_includes_property(element: DomElement*, property: const CssInvalidationSetProperty*) -> bool:
    match property.type_:
        .class_:
            let class_names = dom_element_class_names(element)
            let name = css_invalidation_set_property_name(property)
            for class_name in ak.vector_span_const(&class_names):
                if ak.fly_string_eq_fly_string(&class_name, &name):
                    return true
            return false
        .id:
            let id = dom_element_id(element) else return false
            let name = css_invalidation_set_property_name(property)
            return ak.fly_string_eq_fly_string(&id, &name)
        .tag_name:
            let local_name = dom_element_local_name(element)
            let name = css_invalidation_set_property_name(property)
            return ak.fly_string_eq_fly_string(&local_name, &name)
        .attribute:
            let name = css_invalidation_set_property_name(property)
            return dom_element_has_attribute(element, name) or dom_element_has_removed_attribute_for_style_invalidation(element, name)
        .pseudo_class:
            let pseudo_class = match property.value:
                .pseudo_class(value) => value
                _ => trap("VERIFY: Variant::get<PseudoClass>")
            match pseudo_class:
                .has:
                    return dom_element_affected_by_has_pseudo_class_in_subject_position(element) or dom_element_affected_by_has_pseudo_class_in_non_subject_position(element)
                .enabled:
                    return dom_element_matches_enabled_pseudo_class(element)
                .disabled:
                    return dom_element_matches_disabled_pseudo_class(element)
                .defined:
                    return dom_element_is_defined(element)
                .checked:
                    return dom_element_matches_checked_pseudo_class(element)
                .placeholder_shown:
                    return dom_element_matches_placeholder_shown_pseudo_class(element)
                .empty:
                    if not tree_node_has_children(element.dom_node()):
                        return true
                    # element.first_child_of_type<DOM::Element>()
                    for child in dom_node_children(element.dom_node()):
                        if is_dom_element(child):
                            return false
                    var has_nonempty_text_child = false
                    # element.for_each_child_of_type<DOM::Text>(...)
                    for child in dom_node_children(element.dom_node()):
                        let text_child = as_if_dom_text(child) else continue
                        let data = dom_character_data_data(text_child.dom_character_data())
                        if not ak.utf16_string_base_is_empty(&data.utf16_string_base):
                            has_nonempty_text_child = true
                            break
                    return not has_nonempty_text_child
                .any_link, .link:
                    return dom_element_matches_link_pseudo_class(element)
                .local_link:
                    return dom_element_matches_local_link_pseudo_class(element)
                .root:
                    return is_html_html_element(element)
                .host:
                    return dom_element_is_shadow_host(element)
                .required, .optional:
                    return is_html_input_element(element) or is_html_select_element(element) or is_html_text_area_element(element)
                _:
                    trap("VERIFY_NOT_REACHED")
        .invalidate_self:
            return false
        .invalidate_whole_subtree:
            return true
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/Invalidation/InvalidationSetMatcher.cpp:21 Web::CSS::Invalidation::element_matches_any_invalidation_set_property.
pub func css_invalidation_element_matches_any_invalidation_set_property(element: DomElement*, set: const CssInvalidationSet*) -> bool:
    var includes_any = false
    for property in css_invalidation_set_properties(set):
        if css_invalidation_element_includes_property(element, &property):
            includes_any = true
            break
    return includes_any