dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / invalidation / attribute_invalidator.lucb
#==============================================================================================
#
# attribute_invalidator - style invalidation after an element's attribute changes
#
# DESCRIPTION:
# Port of LibWeb/CSS/Invalidation/AttributeInvalidator.cpp (lines 1-85).
# The changed features (old and new classes, ids, the pseudo-classes an attribute feeds)
# go to Node::invalidate_style; a shadow host whose shadow sheets match :host also dirties
# its shadow tree.
#
# Copyright (c) 2026-present, the Ladybird developers
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## The changed property `{ .type = type, .value = name }`.
func css_invalidation_name_property(type_: CssInvalidationSetPropertyType, name: ak.FlyString) -> CssInvalidationSetProperty:
return CssInvalidationSetProperty(type_ = type_, value = FlyStringOrPseudoClassOrEmpty.fly_string(name))
## The changed property `{ .type = PseudoClass, .value = pseudo_class }`.
pub func css_invalidation_pseudo_class_property(pseudo_class: css_data.PseudoClass) -> CssInvalidationSetProperty:
return CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.pseudo_class, value = FlyStringOrPseudoClassOrEmpty.pseudo_class(pseudo_class))
## `Vector<StringView>::contains_slow(view)`.
func css_invalidation_views_contain(views: const ak.Vector[ak.StringView]*, view: ak.StringView) -> bool:
for existing in ak.vector_span_const(views):
if ak.string_view_eq_string_view(&existing, view):
return true
return false
## Ported from Libraries/LibWeb/CSS/Invalidation/AttributeInvalidator.cpp:24 Web::CSS::Invalidation::invalidate_style_after_attribute_change.
pub func css_invalidation_invalidate_style_after_attribute_change(element: DomElement*, attribute_name: ak.FlyString, old_value: ak.String?, new_value: ak.String?):
var changed_properties: ak.Vector[CssInvalidationSetProperty]
var style_invalidation_options: DomStyleInvalidationOptions
if dom_element_is_presentational_hint(element, attribute_name) or dom_element_style_uses_attr_css_function(element):
style_invalidation_options.invalidate_self = true
if ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_style):
style_invalidation_options.invalidate_self = true
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_class):
let is_ascii_whitespace = DomElementIsAsciiWhitespace(unused = 0)
var old_classes: ak.Vector[ak.StringView]
var new_classes: ak.Vector[ak.StringView]
if let old_string = old_value:
let view = ak.string_bytes_as_string_view(&old_string)
old_classes = ak.string_view_split_view_if(&view, &is_ascii_whitespace)
if let new_string = new_value:
let view = ak.string_bytes_as_string_view(&new_string)
new_classes = ak.string_view_split_view_if(&view, &is_ascii_whitespace)
for old_class in ak.vector_span(&old_classes):
if not css_invalidation_views_contain(&new_classes, old_class):
ak.vector_append_t(&changed_properties, css_invalidation_name_property(CssInvalidationSetPropertyType.class_, ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&old_class))))
for new_class in ak.vector_span(&new_classes):
if not css_invalidation_views_contain(&old_classes, new_class):
ak.vector_append_t(&changed_properties, css_invalidation_name_property(CssInvalidationSetPropertyType.class_, ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&new_class))))
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_id):
if let old_string = old_value:
ak.vector_append_t(&changed_properties, css_invalidation_name_property(CssInvalidationSetPropertyType.id, ak.fly_string_from_string(old_string)))
if let new_string = new_value:
ak.vector_append_t(&changed_properties, css_invalidation_name_property(CssInvalidationSetPropertyType.id, ak.fly_string_from_string(new_string)))
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_disabled):
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.disabled))
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.enabled))
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_placeholder):
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.placeholder_shown))
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_value):
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.checked))
elif ak.fly_string_eq_fly_string(&attribute_name, &html_attribute_names_required):
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.required))
ak.vector_append_t(&changed_properties, css_invalidation_pseudo_class_property(css_data.PseudoClass.optional))
if new_value == none:
dom_element_remember_removed_attribute_for_style_invalidation(element, attribute_name)
ak.vector_append_t(&changed_properties, css_invalidation_name_property(CssInvalidationSetPropertyType.attribute, attribute_name))
dom_node_invalidate_style_vector(element.dom_node(), DomStyleInvalidationReason.element_attribute_change, changed_properties, style_invalidation_options)
# If this element hosts a shadow root whose stylesheets have :host()-matching rules, the shadow tree's computed
# styles can depend on this host's attributes. Mark the shadow subtree dirty so those rules re-evaluate.
if let shadow_root = dom_element_shadow_root(element):
if css_determine_shadow_root_stylesheet_effects_dom_shadow_root(shadow_root).may_match_shadow_host:
dom_node_set_entire_subtree_needs_style_update(shadow_root.dom_node(), true)
dom_node_set_needs_style_update(shadow_root.dom_node(), true)