dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_sheet_list.lucb
#==============================================================================================
#
# style_sheet_list - CSS::StyleSheetList: the CSS style sheets of a document or shadow root
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleSheetList.cpp (lines 1-277) and the inline members of
# StyleSheetList.h (lines 17-71): "create a CSS style sheet", "add a CSS style sheet",
# "remove a CSS style sheet", keeping the list in tree order, and the style invalidation
# that adding or removing a sheet causes.
#
# Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: StyleSheetList.h ========================================================================
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:37 Web::CSS::StyleSheetList::sheets (const).
pub func css_style_sheet_list_sheets_const(this: CssStyleSheetList*) -> ak.Vector[CssCssStyleSheet*]:
return this.m_sheets
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:37 Web::CSS::StyleSheetList::sheets.
pub func css_style_sheet_list_sheets(this: CssStyleSheetList*) -> ak.Vector[CssCssStyleSheet*]*:
return &this.m_sheets
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:39 Web::CSS::StyleSheetList::item.
pub func css_style_sheet_list_item(this: CssStyleSheetList*, index: usize) -> CssCssStyleSheet*?:
if index >= ak.vector_size(&this.m_sheets):
return none
return *ak.vector_at(&this.m_sheets, index)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:46 Web::CSS::StyleSheetList::length.
pub func css_style_sheet_list_length(this: CssStyleSheetList*) -> usize:
return ak.vector_size(&this.m_sheets)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:54 Web::CSS::StyleSheetList::document_or_shadow_root.
pub func css_style_sheet_list_document_or_shadow_root(this: CssStyleSheetList*) -> DomNode*:
return this.m_document_or_shadow_root
## Ported from Libraries/LibWeb/CSS/StyleSheetList.h:54 Web::CSS::StyleSheetList::document_or_shadow_root (const).
pub func css_style_sheet_list_document_or_shadow_root_const(this: CssStyleSheetList*) -> DomNode*:
return this.m_document_or_shadow_root
# mark: Adding and removing sheets ==============================================================
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:23 Web::CSS::StyleSheetList::remove_a_css_style_sheet.
# https://www.w3.org/TR/cssom/#remove-a-css-style-sheet
pub func css_style_sheet_list_remove_a_css_style_sheet(this: CssStyleSheetList*, sheet: CssCssStyleSheet*):
# 1. Remove the CSS style sheet from the list of document or shadow root CSS style sheets.
css_style_sheet_list_remove_sheet(this, sheet)
# 2. Set the CSS style sheet’s parent CSS style sheet, owner node and owner CSS rule to null.
css_style_sheet_set_parent_css_style_sheet(sheet.css_style_sheet(), none)
css_style_sheet_set_owner_node(sheet.css_style_sheet(), none)
css_css_style_sheet_set_owner_css_rule(sheet, none)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:35 Web::CSS::StyleSheetList::add_a_css_style_sheet.
# https://www.w3.org/TR/cssom/#add-a-css-style-sheet
pub func css_style_sheet_list_add_a_css_style_sheet(this: CssStyleSheetList*, sheet: CssCssStyleSheet*):
let style_sheet = sheet.css_style_sheet()
# 1. Add the CSS style sheet to the list of document or shadow root CSS style sheets at the appropriate location. The remainder of these steps deal with the disabled flag.
css_style_sheet_list_add_sheet(this, sheet)
# 2. If the disabled flag is set, then return.
if css_style_sheet_disabled(style_sheet):
return
# 3. If the title is not the empty string, the alternate flag is unset, and preferred CSS style sheet set name is the empty string change the preferred CSS style sheet set name to the title.
let title = css_style_sheet_title(style_sheet)
if not ak.string_is_empty(&title) and not css_style_sheet_is_alternate(style_sheet) and ak.string_is_empty(&this.m_preferred_css_style_sheet_set_name):
this.m_preferred_css_style_sheet_set_name = title
# 4. If any of the following is true, then unset the disabled flag and return:
# - The title is the empty string.
# - The last CSS style sheet set name is null and the title is a case-sensitive match for the preferred CSS style sheet set name.
# - The title is a case-sensitive match for the last CSS style sheet set name.
# NOTE: We don't enable alternate sheets with an empty title. This isn't directly mentioned in the algorithm steps, but the
# HTML specification says that the title element must be specified with a non-empty value for alternative style sheets.
# See: https://html.spec.whatwg.org/multipage/links.html#the-link-is-an-alternative-stylesheet
var unset_disabled = ak.string_is_empty(&title) and not css_style_sheet_is_alternate(style_sheet)
if let last_name = this.m_last_css_style_sheet_set_name:
if web_unicode.string_equals_ignoring_case(&title, last_name):
unset_disabled = true
elif web_unicode.string_equals_ignoring_case(&title, this.m_preferred_css_style_sheet_set_name):
unset_disabled = true
if unset_disabled:
css_style_sheet_set_disabled(style_sheet, false)
return
# 5. Set the disabled flag.
css_style_sheet_set_disabled(style_sheet, true)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:68 Web::CSS::StyleSheetList::create_a_css_style_sheet.
# https://www.w3.org/TR/cssom/#create-a-css-style-sheet
pub func css_style_sheet_list_create_a_css_style_sheet(this: CssStyleSheetList*, css_text: ak.String, type_: ak.String, owner_node: DomElement*?, media: ak.String, title: ak.String, alternate: CssStyleSheetListAlternate, origin_clean: CssStyleSheetListOriginClean, location: web_url.Url?, parent_style_sheet: CssCssStyleSheet*?, owner_rule: CssRule*?) -> CssCssStyleSheet*:
# 1. Create a new CSS style sheet object and set its properties as specified.
# AD-HOC: The spec never tells us when to parse this style sheet, but the most logical place is here.
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_dom_document(&parsing_params, css_style_sheet_list_document(this))
let sheet = parse_css_stylesheet(parsing_params, ak.string_bytes_as_string_view(&css_text), location)
let style_sheet = sheet.css_style_sheet()
css_style_sheet_set_parent_css_style_sheet(style_sheet, parent_style_sheet)
css_css_style_sheet_set_owner_css_rule(sheet, owner_rule)
css_style_sheet_set_owner_node(style_sheet, owner_node)
css_style_sheet_set_type(style_sheet, type_)
css_style_sheet_set_media(style_sheet, media)
css_style_sheet_set_title(style_sheet, title)
css_style_sheet_set_alternate(style_sheet, alternate == CssStyleSheetListAlternate.yes)
css_style_sheet_set_origin_clean(style_sheet, origin_clean == CssStyleSheetListOriginClean.yes)
# 2. Then run the add a CSS style sheet steps for the newly created CSS style sheet.
css_style_sheet_list_add_a_css_style_sheet(this, sheet)
return sheet
# mark: Invalidation ============================================================================
## The lambda of build_invalidation_set_for_stylesheet (captures: the result).
func css_build_invalidation_set_for_style_rule(context: void*?, rule: CssRule*):
let result = (CssStyleSheetInvalidationSet*)(context else trap("no result"))
if css_invalidation_set_needs_invalidate_whole_subtree(&result.invalidation_set):
return
if let style_rule = as_if_css_style_rule(rule):
css_extend_style_sheet_invalidation_set_with_style_rule(result, style_rule)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:90 Web::CSS::build_invalidation_set_for_stylesheet.
func css_build_invalidation_set_for_stylesheet(sheet: CssCssStyleSheet*) -> CssStyleSheetInvalidationSet:
var result: CssStyleSheetInvalidationSet
css_css_style_sheet_for_each_effective_style_producing_rule(sheet, ak.Function1[CssRule*, unit](call = css_build_invalidation_set_for_style_rule, context = &result))
return result
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:104 Web::CSS::rule_requires_broad_add_or_remove_invalidation.
func css_rule_requires_broad_add_or_remove_invalidation(rule: CssRule*) -> bool:
match css_rule_type(rule):
.property, .font_face, .font_feature_values, .counter_style, .keyframes, .layer_block, .layer_statement:
return true
_:
return false
## The lambda of stylesheet_requires_broad_add_or_remove_invalidation (captures: the flag).
func css_stylesheet_requires_broad_invalidation_visit(context: void*?, rule: CssRule*):
let requires_broad_invalidation = (bool*)(context else trap("no flag"))
if *requires_broad_invalidation:
return
# Add/remove invalidation still has to look at effective non-style rules such as @keyframes or @layer, but
# inactive top-level media sheets should contribute nothing at all here.
*requires_broad_invalidation = css_rule_requires_broad_add_or_remove_invalidation(rule)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:122 Web::CSS::stylesheet_requires_broad_add_or_remove_invalidation.
func css_stylesheet_requires_broad_add_or_remove_invalidation(sheet: CssCssStyleSheet*) -> bool:
var requires_broad_invalidation = false
css_css_style_sheet_for_each_effective_rule(sheet, TraversalOrder.preorder, ak.Function1[CssRule*, unit](call = css_stylesheet_requires_broad_invalidation_visit, context = &requires_broad_invalidation))
return requires_broad_invalidation
## Invalidate the rule cache of the document or shadow root's style scope.
func css_style_sheet_list_invalidate_rule_cache(this: CssStyleSheetList*):
if let shadow_root = as_if_dom_shadow_root(this.m_document_or_shadow_root):
css_style_scope_invalidate_rule_cache(dom_shadow_root_style_scope(shadow_root))
else:
css_style_scope_invalidate_rule_cache(dom_document_style_scope(dom_node_document(this.m_document_or_shadow_root)))
## Fold the shadow root's stylesheet effects into a broad invalidation (add_sheet and remove_sheet).
func css_style_sheet_list_add_shadow_root_effects(this: CssStyleSheetList*, invalidation_set_result: CssStyleSheetInvalidationSet*):
if let shadow_root = as_if_dom_shadow_root(this.m_document_or_shadow_root):
let effects = css_determine_shadow_root_stylesheet_effects_dom_shadow_root(shadow_root)
invalidation_set_result.may_match_shadow_host = invalidation_set_result.may_match_shadow_host or effects.may_match_shadow_host
invalidation_set_result.may_match_light_dom_under_shadow_host = invalidation_set_result.may_match_light_dom_under_shadow_host or effects.may_match_light_dom_under_shadow_host
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:135 Web::CSS::StyleSheetList::add_sheet.
func css_style_sheet_list_add_sheet(this: CssStyleSheetList*, sheet: CssCssStyleSheet*):
css_css_style_sheet_add_owning_document_or_shadow_root(sheet, css_style_sheet_list_document_or_shadow_root(this))
css_css_style_sheet_load_pending_image_resources(sheet, css_style_sheet_list_document(this))
if ak.vector_is_empty(&this.m_sheets):
# This is the first sheet, append it to the list.
ak.vector_append_t(&this.m_sheets, sheet)
else:
# We have sheets from before. Insert the new sheet in the correct position (DOM tree order).
var did_insert = false
var i = (isize)ak.vector_size(&this.m_sheets) - 1
while i >= 0:
let existing_sheet = *ak.vector_at(&this.m_sheets, (usize)i)
let existing_owner = css_style_sheet_owner_node(existing_sheet.css_style_sheet()) else trap("null owner node")
var other: DomNode*? = none
if let owner = css_style_sheet_owner_node(sheet.css_style_sheet()):
other = owner.dom_node()
let position = dom_node_compare_document_position(existing_owner.dom_node(), other)
if (position & (u16)DomNodeDocumentPosition.document_position_following) != 0:
ak.vector_insert(&this.m_sheets, (usize)(i + 1), sheet)
did_insert = true
break
i -= 1
if not did_insert:
ak.vector_prepend_u(&this.m_sheets, sheet)
# NOTE: We evaluate media queries immediately when adding a new sheet.
# This coalesces the full document style invalidations.
# If we don't do this, we invalidate now, and then again when Document updates media rules.
_ = css_css_style_sheet_evaluate_media_queries(sheet, css_style_sheet_list_document(this))
if css_rule_list_length(css_css_style_sheet_rules(sheet)) == 0:
# NOTE: If the added sheet has no rules, we don't have to invalidate anything.
return
css_style_sheet_list_invalidate_rule_cache(this)
if not dom_node_is_shadow_root(this.m_document_or_shadow_root) and dom_node_entire_subtree_needs_style_update(this.m_document_or_shadow_root):
# NOTE: If the entire subtree is already marked for style update,
# there's no point spending time building invalidation sets.
# Shadow roots are special: :host(...) and ::slotted(...) can
# still require follow-up invalidation outside that subtree.
return
var invalidation_set_result = css_build_invalidation_set_for_stylesheet(sheet)
let requires_broad_invalidation = css_invalidation_set_needs_invalidate_whole_subtree(&invalidation_set_result.invalidation_set) or css_stylesheet_requires_broad_add_or_remove_invalidation(sheet)
if requires_broad_invalidation:
css_style_sheet_list_add_shadow_root_effects(this, &invalidation_set_result)
css_invalidate_root_for_style_sheet_change(this.m_document_or_shadow_root, invalidation_set_result, DomStyleInvalidationReason.style_sheet_list_add_sheet, requires_broad_invalidation)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:198 Web::CSS::StyleSheetList::remove_sheet.
func css_style_sheet_list_remove_sheet(this: CssStyleSheetList*, sheet: CssCssStyleSheet*):
var invalidation_set_result = css_build_invalidation_set_for_stylesheet(sheet)
let requires_broad_invalidation = css_invalidation_set_needs_invalidate_whole_subtree(&invalidation_set_result.invalidation_set) or css_stylesheet_requires_broad_add_or_remove_invalidation(sheet)
css_css_style_sheet_remove_owning_document_or_shadow_root(sheet, css_style_sheet_list_document_or_shadow_root(this))
let did_remove = ak.vector_remove_first_matching(&this.m_sheets, ak.Function1[const (CssCssStyleSheet*)*, bool](call = css_style_sheet_list_is_sheet, context = sheet))
assert(did_remove)
if css_rule_list_length(css_css_style_sheet_rules(sheet)) == 0:
# NOTE: If the removed sheet had no rules, we don't have to invalidate anything.
return
css_style_sheet_list_invalidate_rule_cache(this)
if not dom_node_is_shadow_root(this.m_document_or_shadow_root) and dom_node_entire_subtree_needs_style_update(this.m_document_or_shadow_root):
return
if requires_broad_invalidation:
css_style_sheet_list_add_shadow_root_effects(this, &invalidation_set_result)
css_invalidate_root_for_style_sheet_change(this.m_document_or_shadow_root, invalidation_set_result, DomStyleInvalidationReason.style_sheet_list_remove_sheet, requires_broad_invalidation)
## The predicate of remove_sheet's remove_first_matching: `entry.ptr() == &sheet`.
func css_style_sheet_list_is_sheet(context: void*?, entry: const (CssCssStyleSheet*)*) -> bool:
return (void*?)*entry == context
# mark: Construction ============================================================================
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:233 Web::CSS::StyleSheetList::create.
pub func css_style_sheet_list_create(document_or_shadow_root: DomNode*) -> CssStyleSheetList*:
let realm = bind_platform_object_realm(document_or_shadow_root.bind_platform_object())
return realm_create_css_style_sheet_list(realm, document_or_shadow_root)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:239 Web::CSS::StyleSheetList::StyleSheetList.
pub func css_style_sheet_list_construct(this: CssStyleSheetList*, document_or_shadow_root: DomNode*):
bind_platform_object_construct_realm(this.bind_platform_object(), bind_platform_object_realm(document_or_shadow_root.bind_platform_object()))
this.m_document_or_shadow_root = document_or_shadow_root
this.platform_object.m_legacy_platform_object_flags = BindPlatformObjectLegacyPlatformObjectFlags(supports_indexed_properties = 1)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:246 Web::CSS::StyleSheetList::initialize (override).
pub func css_style_sheet_list_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.style_sheet_list)
js_cell_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:252 Web::CSS::StyleSheetList::visit_edges (override).
pub func css_style_sheet_list_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssStyleSheetList*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_document_or_shadow_root)
gc.cell_visitor_visit_vector(visitor, &this.m_sheets)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:259 Web::CSS::StyleSheetList::item_value (override).
pub func css_style_sheet_list_item_value_impl(base: BindPlatformObject*, index: usize) -> JsValue?:
let this = (CssStyleSheetList*)base
if index >= ak.vector_size(&this.m_sheets):
return none
return bind_platform_object_wrapper((*ak.vector_at(&this.m_sheets, index)).bind_platform_object())
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:267 Web::CSS::StyleSheetList::document.
pub func css_style_sheet_list_document(this: CssStyleSheetList*) -> DomDocument*:
return dom_node_document(this.m_document_or_shadow_root)
## Ported from Libraries/LibWeb/CSS/StyleSheetList.cpp:267 Web::CSS::StyleSheetList::document (const).
pub func css_style_sheet_list_document_const(this: CssStyleSheetList*) -> DomDocument*:
return dom_node_document(this.m_document_or_shadow_root)