dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / css_rule_list.lucb
#==============================================================================================
#
# css_rule_list - CSS::CSSRuleList: an ordered list of CSS rules
#
# DESCRIPTION:
# Port of LibWeb/CSS/CSSRuleList.cpp (lines 1-332) and the inline members of
# CSSRuleList.h (lines 23-82): creation, item access, "insert a CSS rule" and "remove a
# CSS rule" with their placement constraints, the traversal of the effective rules, and
# media query evaluation.
#
# Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: CSSRuleList.h ===========================================================================
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:32 Web::CSS::CSSRuleList::item (const).
pub func css_rule_list_item_const(this: CssRuleList*, index: usize) -> CssRule*?:
if index >= css_rule_list_length(this):
return none
return *ak.vector_at(&this.m_rules, index)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:39 Web::CSS::CSSRuleList::item.
pub func css_rule_list_item(this: CssRuleList*, index: usize) -> CssRule*?:
if index >= css_rule_list_length(this):
return none
return *ak.vector_at(&this.m_rules, index)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:46 Web::CSS::CSSRuleList::length.
pub func css_rule_list_length(this: CssRuleList*) -> usize:
return ak.vector_size(&this.m_rules)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:48 Web::CSS::CSSRuleList::begin (const).
pub func css_rule_list_begin_const(this: CssRuleList*) -> ak.SimpleIterator[ak.Vector[CssRule*], CssRule*]:
return ak.vector_begin(&this.m_rules)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:49 Web::CSS::CSSRuleList::begin.
pub func css_rule_list_begin(this: CssRuleList*) -> ak.SimpleIterator[ak.Vector[CssRule*], CssRule*]:
return ak.vector_begin(&this.m_rules)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:51 Web::CSS::CSSRuleList::end (const).
pub func css_rule_list_end_const(this: CssRuleList*) -> ak.SimpleIterator[ak.Vector[CssRule*], CssRule*]:
return ak.vector_end(&this.m_rules)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:52 Web::CSS::CSSRuleList::end.
pub func css_rule_list_end(this: CssRuleList*) -> ak.SimpleIterator[ak.Vector[CssRule*], CssRule*]:
return ak.vector_end(&this.m_rules)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:67 Web::CSS::CSSRuleList::set_owner_rule.
pub func css_rule_list_set_owner_rule(this: CssRuleList*, owner_rule: CssRule*):
this.m_owner_rule = owner_rule
## Ported from Libraries/LibWeb/CSS/CSSRuleList.h:68 Web::CSS::CSSRuleList::set_rules.
pub func css_rule_list_set_rules(this: CssRuleList*, rules: ak.Vector[CssRule*]):
this.m_rules = rules
# mark: Construction ============================================================================
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:28 Web::CSS::CSSRuleList::create.
pub func css_rule_list_create(realm: Realm*, rules: const (CssRule*)[]) -> CssRuleList*:
let rule_list = realm_create_css_rule_list(realm, realm)
for rule in rules:
ak.vector_append_t(&rule_list.m_rules, rule)
return rule_list
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:36 Web::CSS::CSSRuleList::CSSRuleList.
pub func css_rule_list_construct(this: CssRuleList*, realm: Realm*):
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
this.platform_object.m_legacy_platform_object_flags = BindPlatformObjectLegacyPlatformObjectFlags(supports_indexed_properties = 1)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:42 Web::CSS::CSSRuleList::initialize (override).
pub func css_rule_list_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.css_rule_list)
js_cell_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:48 Web::CSS::CSSRuleList::visit_edges (override).
pub func css_rule_list_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssRuleList*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_vector(visitor, &this.m_rules)
gc.cell_visitor_visit_t(visitor, this.m_owner_rule)
# mark: Inserting and removing rules ============================================================
## insert_a_css_rule's has_rule_of_type_other_than_specified_before_index lambda.
func css_rule_list_has_rule_of_type_other_than_specified_before_index(this: CssRuleList*, types: const CssRuleType[], index: usize) -> bool:
for i in 0..<index:
let rule_type = css_rule_type(*ak.vector_at(&this.m_rules, i))
var any_matches = false
for type_ in types:
if rule_type == type_:
any_matches = true
if not any_matches:
return true
return false
## insert_a_css_rule's has_rule_of_type_at_or_after_index lambda.
func css_rule_list_has_rule_of_type_at_or_after_index(this: CssRuleList*, type_: CssRuleType, index: usize) -> bool:
for i in index..<ak.vector_size(&this.m_rules):
if css_rule_type(*ak.vector_at(&this.m_rules, i)) == type_:
return true
return false
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:58 Web::CSS::CSSRuleList::insert_a_css_rule.
# AD-HOC: The spec doesn't include a declared_namespaces parameter, but we need it to handle parsing of namespaced selectors.
# https://drafts.csswg.org/cssom/#insert-a-css-rule
pub func css_rule_list_insert_a_css_rule(this: CssRuleList*, rule: StringViewOrCssRule, index: u32, nested: CssRuleListNested, declared_namespaces: ak.HashTable[ak.FlyString, ak.FlyStringTraits]) -> u32!:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. Set length to the number of items in list.
let length = ak.vector_size(&this.m_rules)
# 2. If index is greater than length, then throw an IndexSizeError exception.
if (usize)index > length:
try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("CSS rule index out of bounds."))))
# 3. Set new rule to the results of performing parse a CSS rule on argument rule.
# NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
# spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
# if that variant holds a CSSRule already.
var new_rule: CssRule*? = none
var rule_text = ak.sv("")
var rule_is_string = false
match rule:
.string_view(text):
rule_text = text
rule_is_string = true
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_realm_css_parsing_mode(&parsing_params, realm)
parsing_params.declared_namespaces = declared_namespaces
new_rule = parse_css_rule(parsing_params, text)
.css_rule(parsed_rule):
new_rule = parsed_rule
# 4. If new rule is a syntax error, and nested is set, perform the following substeps:
if new_rule == none and nested == CssRuleListNested.yes:
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_realm_css_parsing_mode(&parsing_params, realm)
parsing_params.rule_context = css_rule_list_rule_context(this)
parsing_params.declared_namespaces = declared_namespaces
# - Set declarations to the results of performing parse a CSS declaration block, on argument rule.
if not rule_is_string:
trap("VERIFY: Variant::get<StringView>")
let declarations = parse_css_property_declaration_block(parsing_params, rule_text)
# - If declarations is empty, throw a SyntaxError exception.
if ak.ordered_hash_map_is_empty(&declarations.custom_properties) and ak.vector_is_empty(&declarations.properties):
try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Unable to parse CSS declarations block."))))
# - Otherwise, set new rule to a new nested declarations rule with declarations as it contents.
new_rule = css_nested_declarations_create_css_style_properties(realm, css_style_properties_create(realm, declarations.properties, declarations.custom_properties)).css_rule()
# 5. If new rule is a syntax error, throw a SyntaxError exception.
if new_rule == none:
try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Unable to parse CSS rule."))))
let inserted_rule = new_rule else trap("unreachable")
# 6. If new rule cannot be inserted into list at the zero-indexed position index due to constraints specified by CSS, then throw a HierarchyRequestError exception. [CSS21]
# "Any @import rules must precede all other valid at-rules and style rules in a style sheet
# (ignoring @charset and @layer statement rules) and must not have any other valid at-rules
# or style rules between it and previous @import rules, or else the @import rule is invalid."
# https://drafts.csswg.org/css-cascade-5/#at-import
#
# "Any @namespace rules must follow all @charset and @import rules and precede all other
# non-ignored at-rules and style rules in a style sheet.
var rule_is_disallowed = false
match css_rule_type(inserted_rule):
.layer_statement:
rule_is_disallowed = false
.import_:
rule_is_disallowed = css_rule_list_has_rule_of_type_other_than_specified_before_index(this, [CssRuleType.import_, CssRuleType.layer_statement], (usize)index)
.namespace:
rule_is_disallowed = css_rule_list_has_rule_of_type_at_or_after_index(this, CssRuleType.import_, (usize)index) or css_rule_list_has_rule_of_type_other_than_specified_before_index(this, [CssRuleType.import_, CssRuleType.namespace, CssRuleType.layer_statement], (usize)index)
_:
rule_is_disallowed = css_rule_list_has_rule_of_type_at_or_after_index(this, CssRuleType.import_, (usize)index) or css_rule_list_has_rule_of_type_at_or_after_index(this, CssRuleType.namespace, (usize)index)
# FIXME: There are more constraints that we should check here - Parser::is_valid_in_the_current_context is probably a good reference for that.
let inserted_type = css_rule_type(inserted_rule)
if rule_is_disallowed or (nested == CssRuleListNested.yes and (inserted_type == CssRuleType.import_ or inserted_type == CssRuleType.namespace)):
try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(realm, ak.utf16_string_from_str("Cannot insert rule at specified index."))))
# 7. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
if inserted_type == CssRuleType.namespace:
for existing_rule in ak.vector_span(&this.m_rules):
let existing_type = css_rule_type(existing_rule)
if existing_type != CssRuleType.import_ and existing_type != CssRuleType.namespace:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(realm, ak.utf16_string_from_str("Cannot insert @namespace rule into a stylesheet with non-namespace/import rules"))))
# 8. Insert new rule into list at the zero-indexed position index.
ak.vector_insert(&this.m_rules, (usize)index, inserted_rule)
# FIXME: Load font faces for inserted @font-face rules
# 9. Return index.
if ak.function0_is_set(&this.on_change):
ak.function0_call(&this.on_change)
return index
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:163 Web::CSS::CSSRuleList::remove_a_css_rule.
# https://www.w3.org/TR/cssom/#remove-a-css-rule
pub func css_rule_list_remove_a_css_rule(this: CssRuleList*, index: u32) -> !:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. Set length to the number of items in list.
let length = ak.vector_size(&this.m_rules)
# 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
if (usize)index >= length:
try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("CSS rule index out of bounds."))))
# 3. Set old rule to the indexth item in list.
let old_rule = *ak.vector_at(&this.m_rules, (usize)index)
# 4. If old rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
if css_rule_type(old_rule) == CssRuleType.namespace:
for rule in ak.vector_span(&this.m_rules):
let rule_type = css_rule_type(rule)
if rule_type != CssRuleType.import_ and rule_type != CssRuleType.namespace:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(realm, ak.utf16_string_from_str("Cannot remove @namespace rule from a stylesheet with non-namespace/import rules."))))
# https://drafts.csswg.org/css-font-loading/#font-face-css-connection
# If a @font-face rule is removed from the document, its connected FontFace object is no longer CSS-connected.
if let font_face_rule = as_if_css_font_face_rule(old_rule):
css_font_face_rule_disconnect_font_face(font_face_rule)
# 5. Remove rule old rule from list at the zero-indexed position index.
ak.vector_remove(&this.m_rules, (usize)index)
# 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
# NOTE: We set the parent stylesheet to null within set_parent_rule.
css_rule_set_parent_rule(old_rule, none)
if ak.function0_is_set(&this.on_change):
ak.function0_call(&this.on_change)
# mark: Traversal and media queries =============================================================
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:200 Web::CSS::CSSRuleList::for_each_effective_rule.
pub func css_rule_list_for_each_effective_rule(this: CssRuleList*, order: TraversalOrder, callback: ak.Function1[CssRule*, unit]):
for rule in ak.vector_span(&this.m_rules):
if order == TraversalOrder.preorder:
ak.function1_call(&callback, rule)
match css_rule_type(rule):
.import_:
let import_rule = (CssImportRule*)rule
if let loaded_style_sheet = css_import_rule_loaded_style_sheet(import_rule):
css_css_style_sheet_for_each_effective_rule(loaded_style_sheet, order, callback)
.container, .layer_block, .media, .page, .style, .supports:
css_grouping_rule_for_each_effective_rule((CssGroupingRule*)rule, order, callback)
# CounterStyle, FontFace, FontFeatureValues, Function, FunctionDeclarations, Keyframe, Keyframes,
# LayerStatement, Margin, Namespace, NestedDeclarations, Property: nothing to traverse.
_:
_ = rule
if order == TraversalOrder.postorder:
ak.function1_call(&callback, rule)
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:243 Web::CSS::CSSRuleList::evaluate_media_queries.
pub func css_rule_list_evaluate_media_queries(this: CssRuleList*, document: DomDocument*) -> bool:
var any_media_queries_changed_match_state = false
for rule in ak.vector_span(&this.m_rules):
match css_rule_type(rule):
.container:
let container_rule = as_css_container_rule(rule)
if css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(container_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
.function:
let function_rule = as_css_function_rule(rule)
if css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(function_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
.import_:
let import_rule = as_css_import_rule(rule)
if let loaded_style_sheet = css_import_rule_loaded_style_sheet(import_rule):
if css_css_style_sheet_evaluate_media_queries(loaded_style_sheet, document):
any_media_queries_changed_match_state = true
.layer_block:
let layer_rule = as_css_layer_block_rule(rule)
if css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(layer_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
.media:
let media_rule = as_css_media_rule(rule)
let was_first_evaluation = not css_media_rule_did_evaluate(media_rule)
let did_match = css_condition_rule_condition_matches(media_rule.css_condition_rule())
let now_matches = css_media_rule_evaluate(media_rule, document)
# The first evaluation establishes the baseline. did_match defaults to false because each MediaQuery
# starts with m_matches=false, so a brand-new rule would otherwise look like a false->true flip the
# first time it gets evaluated against a matching state.
if not was_first_evaluation and did_match != now_matches:
any_media_queries_changed_match_state = true
if now_matches and css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(media_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
.supports:
let supports_rule = as_css_supports_rule(rule)
if css_condition_rule_condition_matches(supports_rule.css_condition_rule()) and css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(supports_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
.style:
let style_rule = as_css_style_rule(rule)
if css_rule_list_evaluate_media_queries(css_grouping_rule_css_rules(style_rule.css_grouping_rule()), document):
any_media_queries_changed_match_state = true
# CounterStyle, FontFace, FontFeatureValues, FunctionDeclarations, Keyframe, Keyframes, LayerStatement,
# Margin, Namespace, NestedDeclarations, Property, Page: nothing to evaluate.
_:
_ = rule
return any_media_queries_changed_match_state
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:316 Web::CSS::CSSRuleList::item_value (override).
pub func css_rule_list_item_value_impl(base: BindPlatformObject*, index: usize) -> JsValue?:
let this = (CssRuleList*)base
if let value = css_rule_list_item_const(this, index):
return bind_platform_object_wrapper(value.bind_platform_object())
return none
## Ported from Libraries/LibWeb/CSS/CSSRuleList.cpp:323 Web::CSS::CSSRuleList::rule_context.
func css_rule_list_rule_context(this: CssRuleList*) -> ak.Vector[CssRuleContext]:
var context: ak.Vector[CssRuleContext]
var rule = this.m_owner_rule
while let current = rule:
ak.vector_append_t(&context, css_rule_context_type_for_rule(css_rule_type(current)))
rule = css_rule_parent_rule(current)
ak.vector_reverse(&context)
return context