dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / selector_engine_3.lucb
#==============================================================================================
#
# selector_engine_3 - SelectorEngine: the form and element-state pseudo-classes, namespaces
# and simple selectors
#
# DESCRIPTION:
# Port of LibWeb/CSS/SelectorEngine.cpp (lines 1079-1476): the last cases of
# matches_pseudo_class's switch (:valid, :invalid, :user-valid, :user-invalid, :required,
# :optional, :default, :autofill, :state() and :heading, as
# matches_pseudo_class_forms), matches_namespace and matches_simple_selector.
#
# 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: Forms and element state =================================================================
## The `for_each_in_subtree` of :valid / :invalid over a form or fieldset: whether an element of
## `root`'s subtree is a candidate for constraint validation that does not satisfy its
## constraints (with `form` given: one whose form owner is `form`).
func selector_engine_has_invalid_form_associated_descendant(root: DomElement*, form: HtmlFormElement*?) -> bool:
for node in dom_node_subtree(root.dom_node()):
if let form_associated_element = as_if_form_associated_element(node):
if let form_element = form:
if html_form_associated_element_form(form_associated_element) != form_element:
continue
if (html_form_associated_element_is_candidate_for_constraint_validation(form_associated_element) and not html_form_associated_element_satisfies_its_constraints(form_associated_element)):
return true
return false
## The user validity of an input, select or textarea element (false for any other element).
func selector_engine_user_validity(element: DomElement*) -> bool:
if let input_element = as_if_html_input_element(element):
return html_input_element_user_validity(input_element)
elif let select_element = as_if_html_select_element(element):
return html_select_element_user_validity(select_element)
elif let text_area_element = as_if_html_text_area_element(element):
return html_text_area_element_user_validity(text_area_element)
return false
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:1079 Web::SelectorEngine::matches_pseudo_class (the cases
## PseudoClass::Valid to PseudoClass::Heading).
func selector_engine_matches_pseudo_class_forms(pseudo_class: const CssSelectorSimpleSelectorPseudoClassSelector*, target: DomAbstractElement) -> bool:
let has_pseudo_element = dom_abstract_element_pseudo_element(&target) != none # target.pseudo_element().has_value()
let target_element = dom_abstract_element_element_const(&target)
match pseudo_class.type_:
.valid:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-valid
# The :valid pseudo-class must match any element falling into one of the following categories:
# - elements that are candidates for constraint validation and that satisfy their constraints
if let form_associated_element = as_if_form_associated_element(target_element):
if (html_form_associated_element_is_candidate_for_constraint_validation(form_associated_element) and html_form_associated_element_satisfies_its_constraints(form_associated_element)):
return true
# - form elements that are not the form owner of any elements that themselves are candidates for constraint validation but do not satisfy their constraints
if let form_element = as_if_html_form_element(target_element):
let has_invalid_elements = selector_engine_has_invalid_form_associated_descendant(target_element, form_element)
if not has_invalid_elements:
return true
# - fieldset elements that have no descendant elements that themselves are candidates for constraint validation but do not satisfy their constraints
if is_html_field_set_element(target_element):
let has_invalid_children = selector_engine_has_invalid_form_associated_descendant(target_element, none)
if not has_invalid_children:
return true
return false
.invalid:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-invalid
# The :invalid pseudo-class must match any element falling into one of the following categories:
# - elements that are candidates for constraint validation but that do not satisfy their constraints
if let form_associated_element = as_if_form_associated_element(target_element):
if (html_form_associated_element_is_candidate_for_constraint_validation(form_associated_element) and not html_form_associated_element_satisfies_its_constraints(form_associated_element)):
return true
# - form elements that are the form owner of one or more elements that themselves are candidates for constraint validation but do not satisfy their constraints
if let form_element = as_if_html_form_element(target_element):
let has_invalid_elements = selector_engine_has_invalid_form_associated_descendant(target_element, form_element)
if has_invalid_elements:
return true
# - fieldset elements that have of one or more descendant elements that themselves are candidates for constraint validation but do not satisfy their constraints
if is_html_field_set_element(target_element):
let has_invalid_children = selector_engine_has_invalid_form_associated_descendant(target_element, none)
if has_invalid_children:
return true
return false
.user_valid:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-user-valid
# The :user-valid pseudo-class must match input, textarea, and select elements whose user validity is true,
let user_validity = selector_engine_user_validity(target_element)
if not user_validity:
return false
# are candidates for constraint validation, and that satisfy their constraints.
let form_associated_element = as_if_form_associated_element(target_element) else trap("as<FormAssociatedElement> failed")
if (html_form_associated_element_is_candidate_for_constraint_validation(form_associated_element) and html_form_associated_element_satisfies_its_constraints(form_associated_element)):
return true
return false
.user_invalid:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-user-invalid
# The :user-invalid pseudo-class must match input, textarea, and select elements whose user validity is true,
let user_validity = selector_engine_user_validity(target_element)
if not user_validity:
return false
# are candidates for constraint validation but do not satisfy their constraints.
let form_associated_element = as_if_form_associated_element(target_element) else trap("as<FormAssociatedElement> failed")
if (html_form_associated_element_is_candidate_for_constraint_validation(form_associated_element) and not html_form_associated_element_satisfies_its_constraints(form_associated_element)):
return true
return false
.required:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-required
# The :required pseudo-class must match any element falling into one of the following categories:
# - input elements that are required
if let input_element = as_if_html_input_element(target_element):
if html_input_element_required_applies(input_element) and dom_element_has_attribute(target_element, html_attribute_names_required):
return true
# - select elements that have a required attribute
elif is_html_select_element(target_element):
if dom_element_has_attribute(target_element, html_attribute_names_required):
return true
# - textarea elements that have a required attribute
elif is_html_text_area_element(target_element):
if dom_element_has_attribute(target_element, html_attribute_names_required):
return true
return false
.optional:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-optional
# The :optional pseudo-class must match any element falling into one of the following categories:
# - input elements to which the required attribute applies that are not required
if let input_element = as_if_html_input_element(target_element):
if html_input_element_required_applies(input_element) and not dom_element_has_attribute(target_element, html_attribute_names_required):
return true
# AD-HOC: Chromium and Webkit also match for hidden inputs (and WPT expects this)
# See: https://github.com/whatwg/html/issues/11273
if html_input_element_type_state(input_element) == HtmlInputElementTypeAttributeState.hidden:
return true
# - select elements that do not have a required attribute
elif is_html_select_element(target_element):
if not dom_element_has_attribute(target_element, html_attribute_names_required):
return true
# - textarea elements that do not have a required attribute
elif is_html_text_area_element(target_element):
if not dom_element_has_attribute(target_element, html_attribute_names_required):
return true
return false
.default:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-default
# The :default pseudo-class must match any element falling into one of the following categories:
if let form_associated_element = as_if_form_associated_element(target_element):
# - Submit buttons that are default buttons of their form owner.
if html_form_associated_element_is_submit_button(form_associated_element):
if let form = html_form_associated_element_form(form_associated_element):
if html_form_element_default_button(form) == form_associated_element:
return true
let html_element = html_form_associated_element_form_associated_element_to_html_element(form_associated_element)
# - input elements to which the checked attribute applies and that have a checked attribute
if let input_element = as_if_html_input_element(html_element):
if html_input_element_checked_applies(input_element) and dom_element_has_attribute(html_element.dom_element(), html_attribute_names_checked):
return true
# - option elements that have a selected attribute
if is_html_option_element(html_element):
if dom_element_has_attribute(html_element.dom_element(), html_attribute_names_selected):
return true
return false
.autofill:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-autofill
# FIXME: The :autofill and :-webkit-autofill pseudo-classes must match input elements which have been autofilled by
# user agent. These pseudo-classes must stop matching if the user edits the autofilled field.
# NB: We don't support autofilling inputs yet, so this is always false.
return false
.state:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-custom
# The :state(identifier) pseudo-class must match all custom elements whose states set's set entries contains identifier.
if not dom_element_is_custom(target_element):
return false
if let custom_state_set = dom_element_custom_state_set(target_element):
let ident = pseudo_class.ident else trap("VERIFY: :state() without its ident")
return html_custom_state_set_has_state(custom_state_set, ident.string_value)
return false
.heading:
if has_pseudo_element:
return false
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-heading
# The :heading pseudo-class must match all h1, h2, h3, h4, h5, and h6 elements.
# https://html.spec.whatwg.org/multipage/semantics-other.html#selector-heading-functional
# The :heading(integer#) pseudo-class must match all h1, h2, h3, h4, h5, and h6 elements that have a heading level of integer. [CSSSYNTAX] [CSSVALUES]
if let heading_element = as_if_html_heading_element(target_element):
if ak.vector_is_empty(&pseudo_class.levels):
return true
# levels.contains_slow(heading_element->heading_level())
let heading_level = (i64)html_heading_element_heading_level(heading_element)
for level in ak.vector_span_const(&pseudo_class.levels):
if level == heading_level:
return true
return false
return false
_:
trap("VERIFY_NOT_REACHED")
# mark: Namespaces ==============================================================================
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:1341 Web::SelectorEngine::matches_namespace.
func selector_engine_matches_namespace(qualified_name: const CssSelectorSimpleSelectorQualifiedName*, element: DomElement*, style_sheet_for_rule: CssCssStyleSheet*?) -> bool:
match qualified_name.namespace_type:
.default_:
# "if no default namespace has been declared for selectors, this is equivalent to *|E."
let style_sheet = style_sheet_for_rule else return true
let default_namespace_rule = css_css_style_sheet_default_namespace_rule(style_sheet) else return true
# "Otherwise it is equivalent to ns|E where ns is the default namespace."
return selector_engine_optional_fly_strings_equal(dom_element_namespace_uri(element), css_namespace_rule_namespace_uri(default_namespace_rule))
.none_:
# "elements with name E without a namespace"
return dom_element_namespace_uri(element) == none
.any:
# "elements with name E in any namespace, including those without a namespace"
return true
.named:
# "elements with name E in namespace ns"
# Unrecognized namespace prefixes are invalid, so don't match.
# (We can't detect this at parse time, since a namespace rule may be inserted later.)
# So, if we don't have a context to look up namespaces from, we fail to match.
let style_sheet = style_sheet_for_rule else return false
let selector_namespace = css_css_style_sheet_namespace_uri(style_sheet, ak.fly_string_bytes_as_string_view(&qualified_name.namespace_))
# https://www.w3.org/TR/css-namespaces-3/#terminology
# In CSS Namespaces a namespace name consisting of the empty string is taken to represent the null namespace
# or lack of a namespace.
if let namespace_value = selector_namespace:
if ak.fly_string_is_empty(&namespace_value):
return dom_element_namespace_uri(element) == none
return selector_namespace != none and selector_engine_optional_fly_strings_equal(selector_namespace, dom_element_namespace_uri(element))
_:
trap("VERIFY_NOT_REACHED")
# mark: Simple selectors ========================================================================
## Ported from Libraries/LibWeb/CSS/SelectorEngine.cpp:1380 Web::SelectorEngine::matches_simple_selector.
func selector_engine_matches_simple_selector(component: const CssSelectorSimpleSelector*, target: DomAbstractElement, shadow_host: DomElement*?, context: SelectorEngineMatchContext*, scope: DomParentNode*?, selector_kind: SelectorEngineSelectorKind, anchor: DomElement*?) -> bool:
let has_pseudo_element = dom_abstract_element_pseudo_element(&target) != none # target.pseudo_element().has_value()
if has_pseudo_element or selector_engine_should_block_shadow_host_matching(component, shadow_host, dom_abstract_element_element_const(&target)):
return false
match component.type_:
.universal, .tag_name:
if has_pseudo_element:
return false
let target_element = dom_abstract_element_element_const(&target)
let qualified_name = css_selector_simple_selector_qualified_name(component)
# Reject if the tag name doesn't match
if component.type_ == CssSelectorSimpleSelectorType.tag_name:
let local_name = dom_element_local_name(target_element)
# See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
if (dom_document_document_type(dom_node_document(target_element.dom_node())) == DocumentDomDocumentType.html and selector_engine_namespace_is(dom_element_namespace_uri(target_element), &namespace_html)):
if not ak.fly_string_eq_fly_string(&qualified_name.name.lowercase_name, &local_name):
return false
elif not ak.fly_string_equals_ignoring_ascii_case_fly_string(&qualified_name.name.name, &local_name):
return false
return selector_engine_matches_namespace(&qualified_name, target_element, context.style_sheet_for_rule)
.id:
if has_pseudo_element:
return false
return selector_engine_optional_fly_strings_equal(css_selector_simple_selector_name(component), dom_element_id(dom_abstract_element_element_const(&target)))
.class_:
if has_pseudo_element:
return false
# Class selectors are matched case insensitively in quirks mode.
# See: https://drafts.csswg.org/selectors-4/#class-html
let case_sensitivity = ak.CaseSensitivity.case_insensitive if dom_document_in_quirks_mode(dom_abstract_element_document(&target)) else ak.CaseSensitivity.case_sensitive
return dom_element_has_class(dom_abstract_element_element_const(&target), css_selector_simple_selector_name(component), case_sensitivity)
.attribute:
if has_pseudo_element:
return false
let attribute = css_selector_simple_selector_attribute(component)
return selector_engine_matches_attribute(&attribute, context.style_sheet_for_rule, dom_abstract_element_element_const(&target))
.pseudo_class:
let pseudo_class = css_selector_simple_selector_pseudo_class(component)
return selector_engine_matches_pseudo_class(&pseudo_class, target, shadow_host, context, scope, selector_kind)
.pseudo_element:
let pseudo_element = css_selector_simple_selector_pseudo_element(component)
if css_selector_pseudo_element_selector_type(&pseudo_element) == css_data.PseudoElement.slotted:
let slotted_element = context.slotted_element else trap("VERIFY: matching ::slotted() without the slotted element")
return selector_engine_matches(css_selector_pseudo_element_selector_compound_selector(&pseudo_element), dom_element_abstract_element(slotted_element), shadow_host, context)
if css_selector_pseudo_element_selector_type(&pseudo_element) == css_data.PseudoElement.part:
# All part names need to match the [pseudo-]element.
# FIXME: Support matching pseudo-elements.
# https://drafts.csswg.org/css-shadow-1/#part
# "The ::part() pseudo-element only matches anything when the originating element is a shadow host."
# FIXME: How does this interact with :host ?
var next_shadow_root = dom_node_containing_shadow_root(dom_abstract_element_element_const(&target).dom_node())
while let ancestor_shadow_root = next_shadow_root:
next_shadow_root = dom_node_containing_shadow_root(ancestor_shadow_root.dom_node())
let host = dom_document_fragment_host(ancestor_shadow_root.dom_document_fragment()) else trap("a shadow root without its host")
# https://drafts.csswg.org/css-shadow-1/#part-element-map
# "The descendants of an element [...] does not include the shadow trees of the element."
let is_direct_child_scope = dom_node_containing_shadow_root(host.dom_node()) == context.rule_shadow_root
let is_host_part_own_scope = (DomShadowRoot*?)ancestor_shadow_root == context.rule_shadow_root and context.for_host_part_matching
if not is_direct_child_scope and not is_host_part_own_scope:
continue
let part_element_map = dom_shadow_root_part_element_map(ancestor_shadow_root)
var all_part_names_match = true
let part_names = css_selector_pseudo_element_selector_ident_list(&pseudo_element)
for part_name in ak.vector_span_const(&part_names):
let matching_parts = ak.hash_map_get_k_const(part_element_map, part_name)
if matching_parts == none or not ak.ordered_hash_table_contains_t(matching_parts else trap("unreachable"), target):
all_part_names_match = false
break
if all_part_names_match:
context.part_owning_parent = host
return true
return false
# Other pseudo-element matching/not-matching is handled in the top level matches().
return true
.nesting:
# Nesting either behaves like :is(), or like :scope.
# :is() is handled already, by us replacing it with :is() directly, so if we
# got here, it's :scope.
let scope_pseudo_class = CssSelectorSimpleSelectorPseudoClassSelector(type_ = css_data.PseudoClass.scope)
return selector_engine_matches_pseudo_class(&scope_pseudo_class, target, shadow_host, context, scope, selector_kind)
.invalid:
# Invalid selectors never match
return false
_:
trap("VERIFY_NOT_REACHED")