dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / css_style_rule.lucb
#==============================================================================================
#
# css_style_rule - CSS::CSSStyleRule: a style rule (selectors, declarations, nested rules)
#
# DESCRIPTION:
# Port of LibWeb/CSS/CSSStyleRule.cpp (lines 1-258) and the inline members of
# CSSStyleRule.h (lines 19-56): serialization, selectorText, the absolutized selectors
# of nested rules, and the propagation of the parent style sheet to the style values.
#
# Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: CSSStyleRule.h ==========================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.h:27 Web::CSS::CSSStyleRule::selectors.
pub func css_style_rule_selectors(this: CssStyleRule*) -> ak.Vector[CssSelector*]:
return this.m_selectors
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.h:28 Web::CSS::CSSStyleRule::declaration.
pub func css_style_rule_declaration(this: CssStyleRule*) -> CssStyleProperties*:
return this.m_declaration
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.h:36 Web::CSS::CSSStyleRule::qualified_layer_name.
pub func css_style_rule_qualified_layer_name(this: CssStyleRule*) -> ak.FlyString:
return css_rule_parent_layer_internal_qualified_name(this.css_rule())
# mark: CSSStyleRule.cpp ========================================================================
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:22 Web::CSS::CSSStyleRule::create.
pub func css_style_rule_create(realm: Realm*, selectors: ak.Vector[CssSelector*], declaration: CssStyleProperties*, nested_rules: CssRuleList*) -> CssStyleRule*:
return realm_create_css_style_rule(realm, realm, selectors, declaration, nested_rules)
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:27 Web::CSS::CSSStyleRule::CSSStyleRule.
pub func css_style_rule_construct(this: CssStyleRule*, realm: Realm*, selectors: ak.Vector[CssSelector*], declaration: CssStyleProperties*, nested_rules: CssRuleList*):
css_grouping_rule_construct(this.css_grouping_rule(), realm, nested_rules, CssRuleType.style)
this.m_selectors = selectors
this.m_declaration = declaration
css_style_declaration_set_parent_rule(this.m_declaration.css_style_declaration(), this.css_rule())
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:35 Web::CSS::CSSStyleRule::initialize (override).
pub func css_style_rule_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.css_style_rule)
css_grouping_rule_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:41 Web::CSS::CSSStyleRule::visit_edges (override).
pub func css_style_rule_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssStyleRule*)base
css_grouping_rule_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_declaration)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_style_map) # CSS::StylePropertyMap is opaque in phase 1 (typed OM, P3)
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:47 Web::CSS::CSSStyleRule::style.
# https://drafts.csswg.org/cssom-1/#dom-cssstylerule-style
pub func css_style_rule_style(this: CssStyleRule*) -> CssStyleProperties*:
return this.m_declaration
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:55 Web::CSS::CSSStyleRule::style_map.
# https://drafts.css-houdini.org/css-typed-om-1/#dom-cssstylerule-stylemap
pub func css_style_rule_style_map(this: CssStyleRule*) -> CssStylePropertyMap*:
if this.m_style_map == none:
this.m_style_map = css_style_property_map_create(bind_platform_object_realm(this.bind_platform_object()), this.m_declaration.css_style_declaration())
return this.m_style_map else trap("unreachable")
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:63 Web::CSS::CSSStyleRule::serialized (override).
# https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
pub func css_style_rule_serialized_impl(base: CssRule*) -> ak.String:
let this = (CssStyleRule*)base
var builder = ak.string_builder_new()
# 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
# followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
let selectors_text = css_serialize_a_group_of_selectors(css_style_rule_selectors(this))
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&selectors_text))
ak.string_builder_append(&builder, " {")
# 2. Let decls be the result of performing serialize a CSS declaration block on the rule’s associated declarations,
# or null if there are no such declarations.
let declaration = this.m_declaration.css_style_declaration()
var decls: ak.String? = none
if css_style_declaration_length(declaration) > 0:
decls = css_style_declaration_serialized(declaration)
# 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’s cssRules list,
# or null if there are no such rules.
var rules: ak.Vector[ak.String]
for rule in ak.vector_span(&css_grouping_rule_css_rules(this.css_grouping_rule()).m_rules):
ak.vector_append_t(&rules, css_rule_serialized(rule))
# 4. If decls and rules are both null, append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)) and return s.
if decls == none and ak.vector_is_empty(&rules):
ak.string_builder_append(&builder, " }")
return ak.string_builder_to_string_without_validation(&builder)
# 5. If rules is null:
if ak.vector_is_empty(&rules):
let decls_text = decls else trap("unreachable")
# 1. Append a single SPACE (U+0020) to s
ak.string_builder_append_u8(&builder, ' ')
# 2. Append decls to s
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&decls_text))
# 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
ak.string_builder_append(&builder, " }")
# 4. Return s.
return ak.string_builder_to_string_without_validation(&builder)
# 6. Otherwise:
# 1. If decls is not null, prepend it to rules.
if let decls_text = decls:
ak.vector_prepend_u(&rules, decls_text)
# 2. For each rule in rules:
for rule in ak.vector_span(&rules):
# * If rule is the empty string, do nothing.
if ak.string_is_empty(&rule):
continue
# * Otherwise:
# 1. Append a newline followed by two spaces to s.
# 2. Append rule to s.
dump_appendff(&builder, "\n {}", [css_arg_string(&rule)])
# 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
ak.string_builder_append(&builder, "\n}")
# 4. Return s.
return ak.string_builder_to_string_without_validation(&builder)
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:128 Web::CSS::CSSStyleRule::selector_text.
# https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
pub func css_style_rule_selector_text(this: CssStyleRule*) -> ak.String:
# The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
return css_serialize_a_group_of_selectors(css_style_rule_selectors(this))
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:135 Web::CSS::CSSStyleRule::set_selector_text.
# https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
pub func css_style_rule_set_selector_text(this: CssStyleRule*, selector_text: ak.StringView):
css_rule_clear_caches(this.css_rule())
# 1. Run the parse a group of selectors algorithm on the given value.
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_realm_css_parsing_mode(&parsing_params, bind_platform_object_realm(this.bind_platform_object()))
if let parent_style_sheet = this.css_grouping_rule_base.css_rule_base.m_parent_style_sheet:
parsing_params.declared_namespaces = css_css_style_sheet_declared_namespaces(parent_style_sheet)
var parsed_selectors: ak.Vector[CssSelector*]? = none
if css_style_rule_parent_style_rule(this) != none:
# AD-HOC: If we're a nested style rule, then we need to parse the selector as relative and then adapt it with implicit &s.
parsed_selectors = parse_selector_for_nested_style_rule(parsing_params, selector_text)
else:
parsed_selectors = parse_selector(parsing_params, selector_text)
# 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
if let selectors = parsed_selectors:
# NOTE: If we have a parent style rule, we need to update the selectors to add any implicit `&`s
this.m_selectors = selectors
if let sheet = css_rule_parent_style_sheet(this.css_rule()):
css_css_style_sheet_invalidate_owners(sheet, DomStyleInvalidationReason.set_selector_text)
# 3. Otherwise, if the algorithm returns a null value, do nothing.
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:166 Web::CSS::CSSStyleRule::absolutized_selectors.
pub func css_style_rule_absolutized_selectors(this: CssStyleRule*) -> ak.Vector[CssSelector*]:
if let cached = this.m_cached_absolutized_selectors:
return cached
# Replace all occurrences of `&` with the nearest ancestor style rule's selector list wrapped in `:is(...)`,
# or if we have no such ancestor, with `:scope`.
# If we don't have any nesting selectors, we can just use our selectors as they are.
var has_any_nesting = false
for selector in ak.vector_span(&this.m_selectors):
if css_selector_contains_the_nesting_selector(selector):
has_any_nesting = true
break
if not has_any_nesting:
this.m_cached_absolutized_selectors = this.m_selectors
return this.m_selectors
# Otherwise, build up a new list of selectors with the `&` replaced.
# First, figure out what we should replace `&` with.
# "When used in the selector of a nested style rule, the nesting selector represents the elements matched by the parent rule.
# When used in any other context, it represents the same elements as :scope in that context (unless otherwise defined)."
# https://drafts.csswg.org/css-nesting-1/#nest-selector
if let parent_style_rule = css_style_rule_parent_style_rule(this):
# TODO: If there's only 1, we don't have to use `:is()` for it
let parent_selector = CssSelectorSimpleSelector(type_ = CssSelectorSimpleSelectorType.pseudo_class,
value = .css_selector_simple_selector_pseudo_class_selector(CssSelectorSimpleSelectorPseudoClassSelector(type_ = css_data.PseudoClass.is,
argument_selector_list = css_style_rule_absolutized_selectors(parent_style_rule))))
var absolutized_selectors: ak.Vector[CssSelector*]
for selector in ak.vector_span(&this.m_selectors):
if let absolutized = css_selector_absolutized(selector, parent_selector):
ak.vector_append_t(&absolutized_selectors, absolutized)
this.m_cached_absolutized_selectors = absolutized_selectors
else:
# NOTE: We can't actually replace & with :scope, because & has to have 0 specificity.
# So we leave it, and treat & like :scope during matching.
this.m_cached_absolutized_selectors = this.m_selectors
return this.m_cached_absolutized_selectors else trap("unreachable")
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:218 Web::CSS::CSSStyleRule::clear_caches (override).
pub func css_style_rule_clear_caches_impl(base: CssRule*):
let this = (CssStyleRule*)base
css_grouping_rule_clear_caches_impl(base)
this.m_cached_absolutized_selectors = none
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:224 Web::CSS::CSSStyleRule::set_parent_style_sheet (override).
pub func css_style_rule_set_parent_style_sheet_impl(base: CssRule*, parent_style_sheet: CssCssStyleSheet*?):
let this = (CssStyleRule*)base
css_grouping_rule_set_parent_style_sheet_impl(base, parent_style_sheet)
# This is annoying: Style values that request resources need to know their CSSStyleSheet in order to fetch them.
for property in ak.vector_span(&this.m_declaration.m_properties):
css_style_value_set_style_sheet(property.value, parent_style_sheet)
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:234 Web::CSS::CSSStyleRule::parent_style_rule.
func css_style_rule_parent_style_rule(this: CssStyleRule*) -> CssStyleRule*?:
var parent = css_rule_parent_rule(this.css_rule())
while let current = parent:
if css_rule_type(current) == CssRuleType.style:
return (CssStyleRule*)current
parent = css_rule_parent_rule(current)
return none
## Ported from Libraries/LibWeb/CSS/CSSStyleRule.cpp:243 Web::CSS::CSSStyleRule::dump (override).
pub func css_style_rule_dump_impl(base: CssRule*, builder: ak.StringBuilder*, indent_levels: i32):
let this = (CssStyleRule*)base
let rules = css_grouping_rule_css_rules(this.css_grouping_rule())
css_rule_dump_impl(base, builder, indent_levels)
for selector in ak.vector_span(&this.m_selectors):
dump_selector_string_builder(builder, selector, indent_levels + 1)
dump_style_properties(builder, this.m_declaration, indent_levels + 1)
dump_indent(builder, indent_levels + 1)
dump_appendff(builder, "Child rules ({}):\n", [ak.arg_u64((u64)css_rule_list_length(rules))])
for child_rule in ak.vector_span(&rules.m_rules):
dump_rule_string_builder(builder, child_rule, indent_levels + 2)