dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / document_12.lucb
#==============================================================================================
#
# document_12 - DOM::Document: resize observations, style sheets, the top layer, find in page
#
# DESCRIPTION:
# Port of LibWeb/DOM/Document.cpp (lines 6606-7083): gathering and broadcasting resize
# observations, adopted style sheets and the active CSS style sheets, the random base
# value, finding a style sheet by URL and its source, registering shadow roots, the top
# layer and the topmost popover, refreshing scroll state, finding matching text, and
# declarative shadow roots and render blocking.
#
#==============================================================================================
# mark: Resize observations =====================================================================
## Ported from Document.cpp:6606 Web::DOM::calculate_depth_for_node.
# https://drafts.csswg.org/resize-observer-1/#calculate-depth-for-node
pub func dom_calculate_depth_for_node(node: DomNode*) -> usize:
# 1. Let p be the parent-traversal path from node to a root Element of this element’s flattened DOM tree.
# 2. Return number of nodes in p.
var depth: usize = 0
var current: DomNode*? = node
while let current_node = current:
depth += 1
current = tree_node_parent(current_node)
return depth
## Ported from Document.cpp:6618 Web::DOM::Document::gather_active_observations_at_depth.
# https://drafts.csswg.org/resize-observer-1/#gather-active-observations-h
pub func dom_document_gather_active_observations_at_depth(this: DomDocument*, depth: usize):
# 1. Let depth be the depth passed in.
# 2. For each observer in [[resizeObservers]] run these steps: clear its active and skipped targets, then sort
# each active observation into them by the depth of its target. ResizeObserver is outside the phase-1
# closure, so the list is always empty until it is ported.
if not ak.intrusive_list_is_empty(&this.m_resize_observers):
trap("unported (P3): Web::DOM::Document::gather_active_observations_at_depth (ResizeObserver)")
## Ported from Document.cpp:6648 Web::DOM::Document::broadcast_active_resize_observations.
# https://drafts.csswg.org/resize-observer-1/#broadcast-active-resize-observations
pub func dom_document_broadcast_active_resize_observations(this: DomDocument*) -> usize:
# 1. Let shallowestTargetDepth be ∞
let shallowest_target_depth = ak.usize_max
# 2. For each observer in document.[[resizeObservers]] run these steps: create a ResizeObserverEntry for each
# active target, record the reported sizes and the shallowest depth, invoke the observer's callback and
# clear its active targets. ResizeObserver is outside the phase-1 closure (see above).
if not ak.intrusive_list_is_empty(&this.m_resize_observers):
trap("unported (P3): Web::DOM::Document::broadcast_active_resize_observations (ResizeObserver)")
return shallowest_target_depth
## Ported from Document.cpp:6714 Web::DOM::Document::has_active_resize_observations.
# https://drafts.csswg.org/resize-observer-1/#has-active-observations-h
pub func dom_document_has_active_resize_observations(this: DomDocument*) -> bool:
# 1. For each observer in [[resizeObservers]] run this step:
# 1. If observer.[[activeTargets]] is not empty, return true.
if not ak.intrusive_list_is_empty(&this.m_resize_observers):
trap("unported (P3): Web::DOM::Document::has_active_resize_observations (ResizeObserver)")
# 2. Return false.
return false
## Ported from Document.cpp:6728 Web::DOM::Document::has_skipped_resize_observations.
# https://drafts.csswg.org/resize-observer-1/#has-skipped-observations-h
pub func dom_document_has_skipped_resize_observations(this: DomDocument*) -> bool:
# 1. For each observer in [[resizeObservers]] run this step:
# 1. If observer.[[skippedTargets]] is not empty, return true.
if not ak.intrusive_list_is_empty(&this.m_resize_observers):
trap("unported (P3): Web::DOM::Document::has_skipped_resize_observations (ResizeObserver)")
# 2. Return false.
return false
# mark: Style sheets ============================================================================
## Ported from Document.cpp:6741 Web::DOM::Document::adopted_style_sheets.
pub func dom_document_adopted_style_sheets(this: DomDocument*) -> IdlObservableArray*:
if this.m_adopted_style_sheets == none:
this.m_adopted_style_sheets = dom_create_adopted_style_sheets_list(this.dom_node())
return this.m_adopted_style_sheets else trap("unreachable: set above")
## Ported from Document.cpp:6748 Web::DOM::Document::set_adopted_style_sheets.
pub func dom_document_set_adopted_style_sheets(this: DomDocument*, new_value: JsValue) -> !:
if this.m_adopted_style_sheets == none:
this.m_adopted_style_sheets = dom_create_adopted_style_sheets_list(this.dom_node())
let adopted_style_sheets = this.m_adopted_style_sheets else trap("unreachable: set above")
idl_observable_array_clear(adopted_style_sheets)
# auto iterator_record = TRY(get_iterator(vm(), new_value, JS::IteratorHint::Sync));
# while (true) {
# auto next = TRY(iterator_step_value(vm(), iterator_record));
# if (!next.has_value())
# break;
# TRY(m_adopted_style_sheets->append(*next));
# }
trap("unported (P3): JS::get_iterator / iterator_step_value over the adopted style sheets")
## for_each_active_css_style_sheet's adopted-sheet lambda: the callback for sheets that are not disabled.
func dom_document_for_each_active_css_style_sheet_adopted(context: void*?, style_sheet: CssCssStyleSheet*):
let callback = (ak.Function1[CssCssStyleSheet*, unit]*)(context else trap("no callback"))
if not css_style_sheet_disabled(style_sheet.css_style_sheet()):
ak.function1_call(callback, style_sheet)
## Ported from Document.cpp:6765 Web::DOM::Document::for_each_active_css_style_sheet.
pub func dom_document_for_each_active_css_style_sheet(this: DomDocument*, callback: ak.Function1[CssCssStyleSheet*, unit]):
if let style_sheets = this.m_style_sheets:
for style_sheet in ak.vector_span(css_style_sheet_list_sheets(style_sheets)):
if not (css_style_sheet_is_alternate(style_sheet.css_style_sheet()) and css_style_sheet_disabled(style_sheet.css_style_sheet())):
ak.function1_call(&callback, style_sheet)
if let adopted_style_sheets = this.m_adopted_style_sheets:
var captured_callback = callback
let adopted = ak.Function1[CssCssStyleSheet*, unit](call = dom_document_for_each_active_css_style_sheet_adopted, context = &captured_callback)
idl_observable_array_for_each[CssCssStyleSheet, ak.Function1[CssCssStyleSheet*, unit]](adopted_style_sheets, adopted)
if let dynamic_view_transition_style_sheet = this.m_dynamic_view_transition_style_sheet:
ak.function1_call(&callback, dynamic_view_transition_style_sheet)
## The `static XorShift128PlusRNG random_number_generator` of ensure_element_shared_css_random_base_value.
var dom_document_random_number_generator: ak.XorShift128PlusRng?
## The initialization callback of ensure_element_shared_css_random_base_value's `ensure`.
func dom_document_next_random_base_value(context: void*?) -> f64:
if dom_document_random_number_generator == none:
var generator: ak.XorShift128PlusRng = ---
ak.xor_shift128_plus_rng_construct(&generator)
dom_document_random_number_generator = generator
var generator = dom_document_random_number_generator else trap("unreachable: set above")
let value = ak.xor_shift128_plus_rng_get(&generator)
dom_document_random_number_generator = generator
return value
## Ported from Document.cpp:6786 Web::DOM::Document::ensure_element_shared_css_random_base_value.
pub func dom_document_ensure_element_shared_css_random_base_value(this: DomDocument*, random_caching_key: CssRandomCachingKey) -> f64:
return *ak.hash_map_ensure_t_callback(&this.m_element_shared_css_random_base_value_cache, random_caching_key, ak.Function0[f64](call = dom_document_next_random_base_value, context = none))
## Ported from Document.cpp:6794 Web::DOM::find_style_sheet_with_url.
func dom_find_style_sheet_with_url(url: ak.String, style_sheet: CssCssStyleSheet*) -> CssCssStyleSheet*?:
if let href = css_style_sheet_href(style_sheet.css_style_sheet()):
if ak.string_equals(&href, &url):
return style_sheet
var import_rules = css_css_style_sheet_import_rules(style_sheet)
for import_rule in ak.vector_span(&import_rules):
if let loaded_style_sheet = css_import_rule_loaded_style_sheet(import_rule):
if let match_ = dom_find_style_sheet_with_url(url, loaded_style_sheet):
return match_
return none
## The captures of get_style_sheet_source's adopted-sheet lambda: `[&]` (the url and the result).
struct DomDocumentGetStyleSheetSourceAdopted:
var url: ak.String
var result: ak.String?
## get_style_sheet_source's adopted-sheet lambda: the first matching sheet's source text.
func dom_document_get_style_sheet_source_adopted(context: void*?, style_sheet: CssCssStyleSheet*):
let captures = (DomDocumentGetStyleSheetSourceAdopted*)(context else trap("no captures"))
if captures.result != none:
return
if let match_ = dom_find_style_sheet_with_url(captures.url, style_sheet):
captures.result = css_css_style_sheet_source_text(match_)
## Ported from Document.cpp:6809 Web::DOM::Document::get_style_sheet_source.
pub func dom_document_get_style_sheet_source(this: DomDocument*, identifier: CssStyleSheetIdentifier) -> ak.String?:
match identifier.type_:
.style_element:
if let unique_id = identifier.dom_element_unique_id:
if let node = dom_node_from_unique_id(unique_id):
if dom_node_is_html_style_element(node):
if let sheet = dom_style_element_base_sheet(&as_html_style_element(node).style_element_base):
return css_css_style_sheet_source_text(sheet)
if dom_node_is_svg_style_element(node):
if let sheet = dom_style_element_base_sheet(&as_svg_style_element(node).style_element_base):
return css_css_style_sheet_source_text(sheet)
return none
.link_element, .import_rule:
if identifier.url == none:
ak.dbgln("Attempting to get link or imported style-sheet with no url; giving up")
return none
let url = identifier.url else trap("unreachable: checked above")
if let style_sheets = this.m_style_sheets:
for style_sheet in ak.vector_span(css_style_sheet_list_sheets(style_sheets)):
if let match_ = dom_find_style_sheet_with_url(url, style_sheet):
return css_css_style_sheet_source_text(match_)
if let adopted_style_sheets = this.m_adopted_style_sheets:
var captures = DomDocumentGetStyleSheetSourceAdopted(url = url, result = none)
idl_observable_array_for_each[CssCssStyleSheet, ak.Function1[CssCssStyleSheet*, unit]](adopted_style_sheets, ak.Function1[CssCssStyleSheet*, unit](call = dom_document_get_style_sheet_source_adopted, context = &captures))
return captures.result
return none
.user_agent:
let url = identifier.url else trap("Optional::value() of an empty user-agent style sheet name")
return css_style_computer_user_agent_style_sheet_source(ak.string_bytes_as_string_view(&url))
.user_style:
return page_user_style(dom_document_page(this))
_:
return none
## Ported from Document.cpp:6863 Web::DOM::Document::register_shadow_root.
pub func dom_document_register_shadow_root(this: DomDocument*, shadow_root: DomShadowRoot*):
ak.intrusive_list_append(&this.m_shadow_roots, shadow_root, memory.offset_of(DomShadowRoot, m_list_node))
## Ported from Document.cpp:6868 Web::DOM::Document::unregister_shadow_root.
pub func dom_document_unregister_shadow_root(this: DomDocument*, shadow_root: DomShadowRoot*):
ak.intrusive_list_remove(&this.m_shadow_roots, shadow_root, memory.offset_of(DomShadowRoot, m_list_node))
# mark: The top layer ===========================================================================
## Ported from Document.cpp:6874 Web::DOM::Document::add_an_element_to_the_top_layer.
# https://drafts.csswg.org/css-position-4/#add-an-element-to-the-top-layer
pub func dom_document_add_an_element_to_the_top_layer(this: DomDocument*, element: DomElement*):
# 1. Let doc be el’s node document.
# 2. If el is already contained in doc’s top layer:
if ak.ordered_hash_table_contains_t(&this.m_top_layer_elements, element):
# Assert: el is also in doc’s pending top layer removals. (Otherwise, this is a spec error.)
assert(ak.ordered_hash_table_contains_t(&this.m_top_layer_pending_removals, element))
# Remove el from both doc’s top layer and pending top layer removals.
_ = ak.ordered_hash_table_remove_t(&this.m_top_layer_elements, element)
_ = ak.ordered_hash_table_remove_t(&this.m_top_layer_pending_removals, element)
# 3. Append el to doc’s top layer.
_ = ak.ordered_hash_table_set(&this.m_top_layer_elements, element)
dom_element_set_in_top_layer(element, true)
# FIXME: 4. At the UA !important cascade origin, add a rule targeting el containing an overlay: auto declaration.
dom_element_set_rendered_in_top_layer(element, true)
dom_node_set_needs_style_update(element.dom_node(), true)
dom_document_invalidate_layout_tree(this, DomInvalidateLayoutTreeReason.document_add_an_element_to_the_top_layer)
## Ported from Document.cpp:6899 Web::DOM::Document::request_an_element_to_be_remove_from_the_top_layer.
# https://drafts.csswg.org/css-position-4/#request-an-element-to-be-removed-from-the-top-layer
pub func dom_document_request_an_element_to_be_remove_from_the_top_layer(this: DomDocument*, element: DomElement*):
# 1. Let doc be el’s node document.
# 2. If el is not contained doc’s top layer, or el is already contained in doc’s pending top layer removals, return.
if not ak.ordered_hash_table_contains_t(&this.m_top_layer_elements, element) or ak.ordered_hash_table_contains_t(&this.m_top_layer_pending_removals, element):
return
# FIXME: 3. Remove the UA !important overlay: auto rule targeting el.
dom_element_set_rendered_in_top_layer(element, false)
dom_node_set_needs_style_update(element.dom_node(), true)
dom_document_invalidate_layout_tree(this, DomInvalidateLayoutTreeReason.document_request_an_element_to_be_removed_from_the_top_layer)
# 4. Append el to doc’s pending top layer removals.
_ = ak.ordered_hash_table_set(&this.m_top_layer_pending_removals, element)
dom_element_set_in_top_layer(element, false)
## Ported from Document.cpp:6918 Web::DOM::Document::remove_an_element_from_the_top_layer_immediately.
# https://drafts.csswg.org/css-position-4/#remove-an-element-from-the-top-layer-immediately
pub func dom_document_remove_an_element_from_the_top_layer_immediately(this: DomDocument*, element: DomElement*):
# 1. Let doc be el’s node document.
# 2. Remove el from doc’s top layer and pending top layer removals.
_ = ak.ordered_hash_table_remove_t(&this.m_top_layer_elements, element)
dom_element_set_in_top_layer(element, false)
# FIXME: 3. Remove the UA !important overlay: auto rule targeting el, if it exists.
dom_element_set_rendered_in_top_layer(element, false)
dom_node_set_needs_style_update(element.dom_node(), true)
dom_document_invalidate_layout_tree(this, DomInvalidateLayoutTreeReason.document_immediately_remove_element_from_the_top_layer)
## Ported from Document.cpp:6934 Web::DOM::Document::process_top_layer_removals.
# https://drafts.csswg.org/css-position-4/#process-top-layer-removals
pub func dom_document_process_top_layer_removals(this: DomDocument*):
# 1. For each element el in doc’s pending top layer removals: if el’s computed value of overlay is none, or el is
# not rendered, remove el from doc’s top layer and pending top layer removals.
var elements_to_remove: ak.Vector[DomElement*]
# NB: Called during top layer processing.
for element in ak.ordered_hash_table_begin(&this.m_top_layer_pending_removals):
# FIXME: Implement overlay property
if dom_node_unsafe_paintable((*element).dom_node()) == none:
ak.vector_append_t(&elements_to_remove, *element)
for element in ak.vector_span(&elements_to_remove):
_ = ak.ordered_hash_table_remove_t(&this.m_top_layer_elements, element)
_ = ak.ordered_hash_table_remove_t(&this.m_top_layer_pending_removals, element)
if not ak.vector_is_empty(&elements_to_remove):
dom_document_invalidate_layout_tree(this, DomInvalidateLayoutTreeReason.document_pending_top_layer_removals_processed)
## Ported from Document.cpp:6957 Web::DOM::Document::topmost_auto_or_hint_popover.
# https://html.spec.whatwg.org/multipage/popover.html#topmost-auto-popover
pub func dom_document_topmost_auto_or_hint_popover(this: DomDocument*) -> HtmlElement*?:
# To find the topmost auto or hint popover given a Document document, perform the following steps. They return an HTML element or null.
# 1. If document's showing hint popover list is not empty, then return document's showing hint popover list's last element.
if not ak.vector_is_empty(&this.m_showing_hint_popover_list):
return *ak.vector_last(&this.m_showing_hint_popover_list)
# 2. If document's showing auto popover list is not empty, then return document's showing auto popover list's last element.
if not ak.vector_is_empty(&this.m_showing_auto_popover_list):
return *ak.vector_last(&this.m_showing_auto_popover_list)
# 3. Return null.
return none
## Ported from Document.cpp:6973 Web::DOM::Document::set_needs_to_refresh_scroll_state.
pub func dom_document_set_needs_to_refresh_scroll_state(this: DomDocument*, b: bool):
# NB: Propagating scroll state invalidation.
if let paintable = dom_document_unsafe_paintable(this):
paint_viewport_paintable_set_needs_to_refresh_scroll_state(paintable, b)
# mark: Find in page ============================================================================
## Ported from Document.cpp:6980 Web::DOM::Document::find_matching_text.
pub func dom_document_find_matching_text(this: DomDocument*, query: ak.String, case_sensitivity: ak.CaseSensitivity) -> ak.Vector[DomRange*?]:
# Ensure the layout tree exists before searching for text matches.
dom_document_update_layout(this, DomUpdateLayoutReason.document_find_matching_text)
let layout_node = dom_document_layout_node(this) else return ak.Vector[DomRange*?]()
var text_blocks = layout_viewport_text_blocks(layout_node)
if ak.vector_is_empty(&text_blocks):
return ak.Vector[DomRange*?]()
let utf16_query = ak.utf16_string_from_utf8_string(&query)
let query_view = ak.utf16_string_base_utf16_view(&utf16_query.utf16_string_base)
let query_length = ak.utf16_view_length_in_code_units(&query_view)
var matches: ak.Vector[DomRange*?]
for text_block in ak.vector_span(&text_blocks):
var positions = text_block.positions
var offset: usize = 0
var i: usize = 0
let text_view = ak.utf16_string_base_utf16_view(&text_block.text.utf16_string_base)
var match_start_position = ak.vector_at(&positions, 0)
while true:
let match_index_value = (ak.utf16_view_find_code_unit_offset_ignoring_case(&text_view, query_view, offset) if case_sensitivity == ak.CaseSensitivity.case_insensitive else ak.utf16_view_find_code_unit_offset_utf16_view(&text_view, query_view, offset))
let match_index = match_index_value else break
while i < ak.vector_size(&positions) -% 1 and match_index > ak.vector_at(&positions, i + 1).start_offset:
match_start_position = ak.vector_at(&positions, i + 1)
i += 1
let start_position = match_index - match_start_position.start_offset
let start_dom_node = match_start_position.dom_node
var match_end_position = match_start_position
while i < ak.vector_size(&positions) -% 1 and (match_index + query_length > ak.vector_at(&positions, i + 1).start_offset):
match_end_position = ak.vector_at(&positions, i + 1)
i += 1
let end_dom_node = match_end_position.dom_node
let end_position = match_index + query_length - match_end_position.start_offset
ak.vector_append_t(&matches, dom_range_create_dom_node(start_dom_node.dom_node(), u32(start_position), end_dom_node.dom_node(), u32(end_position)))
match_start_position = match_end_position
offset = match_index + query_length + 1
if offset >= ak.utf16_view_length_in_code_units(&text_view):
break
return matches
# mark: Declarative shadow roots and render blocking ============================================
## Ported from Document.cpp:7032 Web::DOM::Document::allow_declarative_shadow_roots.
# https://dom.spec.whatwg.org/#document-allow-declarative-shadow-roots
pub func dom_document_allow_declarative_shadow_roots(this: DomDocument*) -> bool:
return this.m_allow_declarative_shadow_roots
## Ported from Document.cpp:7037 Web::DOM::Document::is_render_blocking_element.
pub func dom_document_is_render_blocking_element(this: DomDocument*, element: DomElement*) -> bool:
return ak.hash_table_contains_t(&this.m_render_blocking_elements, element)
## Ported from Document.cpp:7043 Web::DOM::Document::is_render_blocked.
# https://html.spec.whatwg.org/multipage/dom.html#render-blocked
pub func dom_document_is_render_blocked(this: DomDocument*) -> bool:
# A Document document is render-blocked if both of the following are true:
# - document's render-blocking element set is non-empty, or document allows adding render-blocking elements.
# - The current high resolution time given document's relevant global object has not exceeded an implementation-defined timeout value.
# NOTE: This timeout is implementation-defined.
# Other browsers are willing to wait longer, but let's start with 30 seconds.
let max_time_to_block_rendering_in_ms = 30000.0
let now = high_resolution_time_current_high_resolution_time(html_relevant_global_object((JsObject*)this))
if now > max_time_to_block_rendering_in_ms:
return false
# AD-HOC: Consider pending CSS @import rules as render-blocking
if not ak.hash_table_is_empty(&this.m_pending_css_import_rules):
return true
return not ak.hash_table_is_empty(&this.m_render_blocking_elements) or dom_document_allows_adding_render_blocking_elements(this)
## Ported from Document.cpp:7065 Web::DOM::Document::allows_adding_render_blocking_elements.
# https://html.spec.whatwg.org/multipage/dom.html#allows-adding-render-blocking-elements
pub func dom_document_allows_adding_render_blocking_elements(this: DomDocument*) -> bool:
# A document allows adding render-blocking elements if document's content type is "text/html" and the body element of document is null.
let content_type = dom_document_content_type(this)
return ak.string_eq_string_view(&content_type, ak.sv("text/html")) and dom_document_body(this) == none
## Ported from Document.cpp:7071 Web::DOM::Document::add_render_blocking_element.
pub func dom_document_add_render_blocking_element(this: DomDocument*, element: DomElement*):
_ = ak.hash_table_set(&this.m_render_blocking_elements, element)
## Ported from Document.cpp:7076 Web::DOM::Document::remove_render_blocking_element.
pub func dom_document_remove_render_blocking_element(this: DomDocument*, element: DomElement*):
_ = ak.hash_table_remove_t(&this.m_render_blocking_elements, element)
if ak.hash_table_is_empty(&this.m_render_blocking_elements):
page_client_request_frame(page_client(dom_document_page(this)))