dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / selector_engine_2.lucb
#==============================================================================================
#
# selector_engine_2 - SelectorEngine: the pseudo-class helpers and matching a pseudo-class
#
# DESCRIPTION:
# Port of LibWeb/CSS/SelectorEngine.cpp (lines 557-1078): the same-type sibling walks,
# should_block_shadow_host_matching, :read-write, :open, :host, the meter's optimal-value
# states, and matches_pseudo_class, the switch over every PseudoClass. The cases from
# :valid on (the form validity, :required, :optional, :default, :autofill, :state() and
# :heading) are selector_engine_3's matches_pseudo_class_forms, which this function's
# switch calls.
#
# Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Siblings, shadow hosts and the state pseudo-classes =====================================
## `a == b` of two Optional<FlyString> (a namespace), or of a FlyString and an Optional<FlyString>.
func selector_engine_optional_fly_strings_equal(a: ak.FlyString?, b: ak.FlyString?) -> bool:
if let a_value = a:
let b_value = b else return false
return ak.fly_string_eq_fly_string(&a_value, &b_value)
return b == none
## `namespace_uri == expected`: an Optional<FlyString> compared with a FlyString.
func selector_engine_namespace_is(namespace_uri: ak.FlyString?, expected: const ak.FlyString*) -> bool:
let value = namespace_uri else return false
return ak.fly_string_eq_fly_string(&value, expected)
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:557 Web::SelectorEngine::previous_sibling_with_same_type.
func selector_engine_previous_sibling_with_same_type(element: DomElement*) -> DomElement*?:
let local_name = dom_element_local_name(element)
var next = dom_non_document_type_child_node_previous_element_sibling(element.dom_node())
while let sibling = next:
let sibling_local_name = dom_element_local_name(sibling)
if (ak.fly_string_eq_fly_string(&sibling_local_name, &local_name) and selector_engine_optional_fly_strings_equal(dom_element_namespace_uri(sibling), dom_element_namespace_uri(element))):
return sibling
next = dom_non_document_type_child_node_previous_element_sibling(sibling.dom_node())
return none
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:566 Web::SelectorEngine::next_sibling_with_same_type.
func selector_engine_next_sibling_with_same_type(element: DomElement*) -> DomElement*?:
let local_name = dom_element_local_name(element)
var next = dom_non_document_type_child_node_next_element_sibling(element.dom_node())
while let sibling = next:
let sibling_local_name = dom_element_local_name(sibling)
if (ak.fly_string_eq_fly_string(&sibling_local_name, &local_name) and selector_engine_optional_fly_strings_equal(dom_element_namespace_uri(sibling), dom_element_namespace_uri(element))):
return sibling
next = dom_non_document_type_child_node_next_element_sibling(sibling.dom_node())
return none
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:578 Web::SelectorEngine::should_block_shadow_host_matching.
# Returns true if this selector should be blocked from matching against the shadow host from within a shadow tree.
# Only :host pseudo-class is allowed to match the shadow host from within shadow tree, all other selectors (including other pseudo-classes) are blocked.
# Compound selectors (:has(), :is(), :where()), nesting, and pseudo-elements are allowed as they may contain or be contained within :host.
func selector_engine_should_block_shadow_host_matching(component: const CssSelectorSimpleSelector*, shadow_host: DomElement*?, element: DomElement*) -> bool:
let host = shadow_host else return false
if element != host:
return false
# From within shadow tree, only :host pseudo-class can match the host element
if component.type_ == CssSelectorSimpleSelectorType.pseudo_class:
let pseudo_class = css_selector_simple_selector_pseudo_class(component)
return (pseudo_class.type_ != css_data.PseudoClass.host and pseudo_class.type_ != css_data.PseudoClass.has and pseudo_class.type_ != css_data.PseudoClass.is and pseudo_class.type_ != css_data.PseudoClass.where)
# Allow nesting and PseudoElement as it may contain :host class
if component.type_ == CssSelectorSimpleSelectorType.nesting or component.type_ == CssSelectorSimpleSelectorType.pseudo_element:
return false
return true
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:599 Web::SelectorEngine::matches_read_write_pseudo_class.
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-read-write
func selector_engine_matches_read_write_pseudo_class(element: DomElement*) -> bool:
# The :read-write pseudo-class must match any element falling into one of the following categories,
# which for the purposes of Selectors are thus considered user-alterable: [SELECTORS]
# - input elements to which the readonly attribute applies, and that are mutable
# (i.e. that do not have the readonly attribute specified and that are not disabled)
if let input_element = as_if_html_input_element(element):
return (html_input_element_is_allowed_to_be_readonly(input_element) and not dom_element_has_attribute(element, html_attribute_names_readonly) and html_form_associated_element_enabled(&input_element.html_element().form_associated_element))
# - textarea elements that do not have a readonly attribute, and that are not disabled
if let text_area_element = as_if_html_text_area_element(element):
return (not dom_element_has_attribute(element, html_attribute_names_readonly) and html_form_associated_element_enabled(&text_area_element.html_element().form_associated_element))
# - elements that are editing hosts or editable and are neither input elements nor textarea elements
return dom_node_is_editable_or_editing_host(element.dom_node())
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:616 Web::SelectorEngine::matches_open_state_pseudo_class.
# https://drafts.csswg.org/selectors-4/#open-state
func selector_engine_matches_open_state_pseudo_class(element: DomElement*, open: bool) -> bool:
# The :open pseudo-class represents an element that has both “open” and “closed” states,
# and which is currently in the “open” state.
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-open
# The :open pseudo-class must match any element falling into one of the following categories:
# - details elements that have an open attribute
# - dialog elements that have an open attribute
if is_html_details_element(element) or is_html_dialog_element(element):
return open == dom_element_has_attribute(element, html_attribute_names_open)
# - select elements that are a drop-down box and whose drop-down boxes are open
if let select = as_if_html_select_element(element):
return open == html_select_element_is_open(select)
# - input elements that support a picker and whose pickers are open
if let input = as_if_html_input_element(element):
return open == (html_input_element_supports_a_picker(input) and html_input_element_is_open(input))
return false
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:638 Web::SelectorEngine::matches_host_pseudo_class.
# https://drafts.csswg.org/css-shadow-1/#host-selector
func selector_engine_matches_host_pseudo_class(element: DomElement*, shadow_host: DomElement*?, context: SelectorEngineMatchContext*, argument_selector_list: const ak.Vector[CssSelector*]*) -> bool:
# When evaluated in the context of a shadow tree, it matches the shadow tree’s shadow host if the shadow host,
# in its normal context, matches the selector argument. In any other context, it matches nothing.
let host = shadow_host else return false
if element != host:
return false
# NOTE: There's either 0 or 1 argument selector, since the syntax is :host or :host(<compound-selector>)
if not ak.vector_is_empty(argument_selector_list):
return selector_engine_matches(*ak.vector_first_const(argument_selector_list), dom_element_abstract_element(element), none, context)
return true
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:652 Web::SelectorEngine::matches_optimal_value_pseudo_class.
func selector_engine_matches_optimal_value_pseudo_class(element: DomElement*, desired_state: HtmlMeterElementValueState) -> bool:
if let meter = as_if_html_meter_element(element):
return html_meter_element_value_state(meter) == desired_state
return false
# mark: matches_pseudo_class ==================================================================
## matches_pseudo_class's `note_structural_pseudo_class_match_attempt` lambda.
func selector_engine_note_structural_pseudo_class_match_attempt(target: const DomAbstractElement*, context: SelectorEngineMatchContext*):
let target_element = dom_abstract_element_element_const(target)
if (DomElement*?)target_element != context.subject:
dom_element_set_affected_by_structural_pseudo_class_in_non_subject_position(target_element)
## matches_pseudo_class's `matches_selector_list` lambda (the :nth-child(An+B of S) list).
func selector_engine_matches_pseudo_class_matches_selector_list(list: const ak.Vector[CssSelector*]*, element: DomElement*, shadow_host: DomElement*?, context: SelectorEngineMatchContext*) -> bool:
if ak.vector_is_empty(list):
return true
# list.contains([&](auto const& selector) { return matches(selector, element, shadow_host, context); })
for selector in ak.vector_span_const(list):
if selector_engine_matches(selector, dom_element_abstract_element(element), shadow_host, context):
return true
return false
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:659 Web::SelectorEngine::matches_pseudo_class.
func selector_engine_matches_pseudo_class(pseudo_class: const CssSelectorSimpleSelectorPseudoClassSelector*, target: DomAbstractElement, shadow_host: DomElement*?, context: SelectorEngineMatchContext*, scope: DomParentNode*?, selector_kind: SelectorEngineSelectorKind) -> bool:
css_pseudo_class_bitmap_set(&context.attempted_pseudo_class_matches, pseudo_class.type_, true)
let has_pseudo_element = dom_abstract_element_pseudo_element(&target) != none # target.pseudo_element().has_value()
let element = dom_abstract_element_element_const(&target) # target.element()
match pseudo_class.type_:
.count:
trap("VERIFY_NOT_REACHED")
.link, .any_link:
if has_pseudo_element:
return false
# NOTE: AnyLink should match whether the link is visited or not, so if we ever start matching
# :visited, we'll need to handle these differently.
return dom_element_matches_link_pseudo_class(element)
.local_link:
if has_pseudo_element:
return false
return dom_element_matches_local_link_pseudo_class(element)
.visited:
# FIXME: Maybe match this selector sometimes?
return false
.active:
# FIXME: Match pseudo-elements
if has_pseudo_element:
return false
return dom_element_is_being_activated(element)
.hover:
# FIXME: Match pseudo-elements
if has_pseudo_element:
return false
return selector_engine_matches_hover_pseudo_class(element)
.focus:
# FIXME: Match pseudo-elements
if has_pseudo_element:
return false
return dom_element_is_focused(element)
.focus_visible:
# FIXME: Match pseudo-elements
if has_pseudo_element:
return false
return dom_element_is_focused(element) and dom_element_should_indicate_focus(element)
.focus_within:
# FIXME: Match pseudo-elements
if has_pseudo_element:
return false
let focused_area = dom_document_focused_area(dom_abstract_element_document(&target)) else return false
return tree_node_is_inclusive_ancestor_of(element.dom_node(), focused_area)
.fullscreen:
if has_pseudo_element:
return false
return dom_element_is_fullscreen_element(element)
.first_child:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_first_child_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return dom_non_document_type_child_node_previous_element_sibling(element.dom_node()) == none
.last_child:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_last_child_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return dom_non_document_type_child_node_next_element_sibling(element.dom_node()) == none
.only_child:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_first_child_pseudo_class(element, true)
dom_element_set_affected_by_last_child_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return not (dom_non_document_type_child_node_previous_element_sibling(element.dom_node()) != none or dom_non_document_type_child_node_next_element_sibling(element.dom_node()) != none)
.empty:
if has_pseudo_element:
return false
if not tree_node_has_children(element.dom_node()):
return true
if tree_node_first_child_of_type(element.dom_node(), &dom_element_class) != none:
return false
# NOTE: CSS Selectors level 4 changed ":empty" to also match whitespace-only text nodes.
# However, none of the major browser supports this yet, so let's just hang back until they do.
var has_nonempty_text_child = false
# 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
.root:
if has_pseudo_element:
return false
return is_html_html_element(element)
.host:
if has_pseudo_element:
return false
return selector_engine_matches_host_pseudo_class(element, shadow_host, context, &pseudo_class.argument_selector_list)
.scope:
if has_pseudo_element:
return false
if let scope_node = scope:
return element.dom_node() == scope_node.dom_node()
return is_html_html_element(element)
.first_of_type:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_forward_positional_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return selector_engine_previous_sibling_with_same_type(element) == none
.last_of_type:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_backward_positional_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return selector_engine_next_sibling_with_same_type(element) == none
.only_of_type:
if has_pseudo_element:
return false
if context.collect_per_element_selector_involvement_metadata:
dom_element_set_affected_by_forward_positional_pseudo_class(element, true)
dom_element_set_affected_by_backward_positional_pseudo_class(element, true)
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
return selector_engine_previous_sibling_with_same_type(element) == none and selector_engine_next_sibling_with_same_type(element) == none
.lang:
if has_pseudo_element:
return false
return selector_engine_matches_lang_pseudo_class(element, &pseudo_class.languages)
.disabled:
if has_pseudo_element:
return false
return dom_element_matches_disabled_pseudo_class(element)
.enabled:
if has_pseudo_element:
return false
return dom_element_matches_enabled_pseudo_class(element)
.checked:
if has_pseudo_element:
return false
return dom_element_matches_checked_pseudo_class(element)
.unchecked:
if has_pseudo_element:
return false
return dom_element_matches_unchecked_pseudo_class(element)
.indeterminate:
if has_pseudo_element:
return false
return selector_engine_matches_indeterminate_pseudo_class(element)
.high_value:
if has_pseudo_element:
return false
if let meter = as_if_html_meter_element(element):
return html_meter_element_value(meter) > html_meter_element_high(meter)
return false
.low_value:
if has_pseudo_element:
return false
if let meter = as_if_html_meter_element(element):
return html_meter_element_value(meter) < html_meter_element_low(meter)
return false
.optimal_value:
if has_pseudo_element:
return false
return selector_engine_matches_optimal_value_pseudo_class(element, HtmlMeterElementValueState.optimal)
.suboptimal_value:
if has_pseudo_element:
return false
return selector_engine_matches_optimal_value_pseudo_class(element, HtmlMeterElementValueState.suboptimal)
.even_less_good_value:
if has_pseudo_element:
return false
return selector_engine_matches_optimal_value_pseudo_class(element, HtmlMeterElementValueState.even_less_good)
.defined:
if has_pseudo_element:
return false
return dom_element_is_defined(element)
.has:
# FIXME: Is ::pseudo:has() allowed?
if has_pseudo_element:
return false
# :has() cannot be nested in a :has()
if selector_kind == SelectorEngineSelectorKind.relative:
return false
if context.collect_per_element_selector_involvement_metadata:
if (DomElement*?)element == context.subject:
dom_element_set_affected_by_has_pseudo_class_in_subject_position(element, true)
else:
dom_element_set_affected_by_has_pseudo_class_in_non_subject_position(element, true)
# These selectors should be relative selectors (https://drafts.csswg.org/selectors-4/#relative-selector)
for selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
if selector_engine_matches_has_pseudo_class(selector, element, shadow_host, context, scope):
return true
return false
.is, .where:
for selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
if selector_engine_matches(selector, target, shadow_host, context, scope):
return true
return false
.not_:
for selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
if selector_engine_matches(selector, target, shadow_host, context, scope):
return false
return true
.nth_child, .nth_last_child, .nth_of_type, .nth_last_of_type:
if has_pseudo_element:
return false
let target_element = element
let parent = tree_node_parent(target_element.dom_node())
if context.collect_per_element_selector_involvement_metadata:
match pseudo_class.type_:
.nth_child, .nth_of_type:
dom_element_set_affected_by_forward_positional_pseudo_class(target_element, true)
.nth_last_child, .nth_last_of_type:
dom_element_set_affected_by_backward_positional_pseudo_class(target_element, true)
_:
trap("VERIFY_NOT_REACHED")
selector_engine_note_structural_pseudo_class_match_attempt(&target, context)
# https://drafts.csswg.org/selectors-4/#child-index
# The pseudo-classes defined in this section select elements based on their index amongst their inclusive siblings.
# NB: An element without a parent has no siblings, so its index is 1.
var index: i32 = 1
match pseudo_class.type_:
.nth_child:
if not selector_engine_matches_pseudo_class_matches_selector_list(&pseudo_class.argument_selector_list, target_element, shadow_host, context):
return false
if let parent_node = parent:
var next_child = (DomElement*?)tree_node_first_child_of_type(parent_node, &dom_element_class)
while let child = next_child:
if child == target_element:
break
if selector_engine_matches_pseudo_class_matches_selector_list(&pseudo_class.argument_selector_list, child, shadow_host, context):
index += 1
next_child = dom_non_document_type_child_node_next_element_sibling(child.dom_node())
.nth_last_child:
if not selector_engine_matches_pseudo_class_matches_selector_list(&pseudo_class.argument_selector_list, target_element, shadow_host, context):
return false
if let parent_node = parent:
var next_child = (DomElement*?)tree_node_last_child_of_type(parent_node, &dom_element_class)
while let child = next_child:
if child == target_element:
break
if selector_engine_matches_pseudo_class_matches_selector_list(&pseudo_class.argument_selector_list, child, shadow_host, context):
index += 1
next_child = dom_non_document_type_child_node_previous_element_sibling(child.dom_node())
.nth_of_type:
var next_child = selector_engine_previous_sibling_with_same_type(target_element)
while let child = next_child:
index += 1
next_child = selector_engine_previous_sibling_with_same_type(child)
.nth_last_of_type:
var next_child = selector_engine_next_sibling_with_same_type(target_element)
while let child = next_child:
index += 1
next_child = selector_engine_next_sibling_with_same_type(child)
_:
trap("VERIFY_NOT_REACHED")
return css_selector_simple_selector_an_plus_b_pattern_matches(&pseudo_class.an_plus_b_pattern, index)
.playing:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return not html_media_element_paused(media_element)
return false
.paused:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return html_media_element_paused(media_element)
return false
.seeking:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return html_media_element_seeking(media_element)
return false
.muted:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return html_media_element_muted(media_element)
return false
.volume_locked:
if has_pseudo_element:
return false
# FIXME: Currently we don't allow the user to specify an override volume, so this is always false.
# Once we do, implement this!
return false
.buffering:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return html_media_element_blocked(media_element)
return false
.stalled:
if has_pseudo_element:
return false
if let media_element = as_if_html_media_element(element):
return html_media_element_stalled(media_element)
return false
.target:
if has_pseudo_element:
return false
return dom_element_is_target(element)
.dir:
# FIXME: Should we support pseudo-elements here?
if has_pseudo_element:
return false
# "Values other than ltr and rtl are not invalid, but do not match anything."
# - https://www.w3.org/TR/selectors-4/#the-dir-pseudo
let ident = pseudo_class.ident else return false
if not (ident.keyword == css_data.Keyword.ltr or ident.keyword == css_data.Keyword.rtl):
return false
match dom_element_directionality(element):
.ltr: return ident.keyword == css_data.Keyword.ltr
.rtl: return ident.keyword == css_data.Keyword.rtl
_: trap("VERIFY_NOT_REACHED")
.read_only:
if has_pseudo_element:
return false
return not selector_engine_matches_read_write_pseudo_class(element)
.read_write:
if has_pseudo_element:
return false
return selector_engine_matches_read_write_pseudo_class(element)
.placeholder_shown:
if has_pseudo_element:
return false
return dom_element_matches_placeholder_shown_pseudo_class(element)
.open:
if has_pseudo_element:
return false
return selector_engine_matches_open_state_pseudo_class(element, pseudo_class.type_ == css_data.PseudoClass.open)
.modal:
if has_pseudo_element:
return false
# https://drafts.csswg.org/selectors/#modal-state
if let dialog_element = as_if_html_dialog_element(element):
return html_dialog_element_is_modal(dialog_element)
# FIXME: fullscreen elements are also modal.
return false
.popover_open:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-popover-open
# The :popover-open pseudo-class is defined to match any HTML element whose popover attribute is not in the
# No Popover state and whose popover visibility state is showing.
if let html_element = as_if_html_element(element):
if dom_element_has_attribute(element, html_attribute_names_popover):
return html_element_popover_visibility_state(html_element) == HtmlElementPopoverVisibilityState.showing
return false
.valid, .invalid, .user_valid, .user_invalid, .required, .optional, .default, .autofill, .state, .heading:
return selector_engine_matches_pseudo_class_forms(pseudo_class, target)
_:
return false