dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / style_element_base.lucb
#==============================================================================================
#
# style_element_base - DOM::StyleElementBase: the style block of <style> (HTML and SVG)
#
# DESCRIPTION:
# Port of LibWeb/DOM/StyleElementBase.cpp (lines 1-175) and the inline functions of
# StyleElementBase.h. StyleElementBase is a stateful mixin (DESIGN.md ยง2.5) whose one
# virtual, as_element, leads back to its element.
#
# The C++ `Optional<DocumentLoadEventDelayer>` releases its hold when it is emplaced over
# or cleared (the delayer's destructor); the port calls
# dom_document_load_event_delayer_destruct at those two points.
#
#==============================================================================================
# mark: Updating the style block ================================================================
# The user agent must run the "update a style block" algorithm whenever one of the following conditions occur:
# FIXME: The element is popped off the stack of open elements of an HTML parser or XML parser.
#
# NOTE: This is basically done by children_changed() today:
# The element's children changed steps run.
#
# NOTE: This is basically done by inserted() and removed_from() today:
# The element is not on the stack of open elements of an HTML parser or XML parser, and it becomes connected or disconnected.
## `m_document_load_event_delayer.clear()`: destroys a held delayer.
func dom_style_element_base_clear_document_load_event_delayer(this: DomStyleElementBase*):
if let delayer = this.m_document_load_event_delayer:
var released = delayer
dom_document_load_event_delayer_destruct(&released)
this.m_document_load_event_delayer = none
## Ported from StyleElementBase.cpp:32 Web::DOM::StyleElementBase::update_a_style_block.
# https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block
pub func dom_style_element_base_update_a_style_block(this: DomStyleElementBase*):
let style_element = dom_style_element_base_as_element(this)
let document = dom_node_document(style_element.dom_node())
# OPTIMIZATION: Skip parsing CSS if we're in the middle of parsing a HTML fragment.
# The style block will be parsed upon insertion into a proper document.
if dom_document_is_temporary_document_for_fragment_parsing(document):
return
# 1. Let element be the style element.
# 2. If element has an associated CSS style sheet, remove the CSS style sheet in question.
if let associated_css_style_sheet = this.m_associated_css_style_sheet:
let style_sheet_list = this.m_style_sheet_list else trap("VERIFY: style sheet list")
css_style_sheet_list_remove_a_css_style_sheet(style_sheet_list, associated_css_style_sheet)
this.m_style_sheet_list = none
# FIXME: This should probably be handled by StyleSheet::set_owner_node().
this.m_associated_css_style_sheet = none
# 3. If element is not connected, then return.
if not dom_node_is_connected(style_element.dom_node()):
return
# 4. If element's type attribute is present and its value is neither the empty string nor an ASCII case-insensitive match for "text/css", then return.
if let type_attribute = dom_element_attribute(style_element, html_attribute_names_type):
let type_view = ak.string_bytes_as_string_view(&type_attribute)
if not ak.string_is_empty(&type_attribute) and not ak.string_view_equals_ignoring_ascii_case(&type_view, ak.sv("text/css")):
return
# 5. If the Should element's inline behavior be blocked by Content Security Policy? algorithm returns "Blocked" when executed upon the style element, "style", and the style element's child text content, then return. [CSP]
let child_text_content = dom_node_child_text_content(style_element.dom_node())
let realm = bind_platform_object_realm(style_element.bind_platform_object())
if content_security_policy_should_elements_inline_type_behavior_be_blocked_by_content_security_policy(realm, style_element, ContentSecurityPolicyInlineType.style, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&child_text_content.utf16_string_base)) == ContentSecurityPolicyResult.blocked:
return
# 6. Create a CSS style sheet with the following properties:
# type
# text/css
# owner node
# element
# media
# The media attribute of element.
# title
# The title attribute of element, if element is in a document tree, or the empty string otherwise.
# alternate flag
# Unset.
# origin-clean flag
# Set.
# location
# parent CSS style sheet
# owner CSS rule
# null
# disabled flag
# Left at its default value.
# CSS rules
# Left uninitialized.
let style_sheet_list = dom_element_document_or_shadow_root_style_sheets(style_element)
this.m_style_sheet_list = style_sheet_list
let text_content = dom_node_text_content(style_element.dom_node()) else ak.utf16_string_new()
var title = ak.string_new()
if dom_node_in_a_document_tree(style_element.dom_node()):
title = dom_element_attribute(style_element, html_attribute_names_title) else ak.string_new()
let associated_css_style_sheet = css_style_sheet_list_create_a_css_style_sheet(style_sheet_list, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&text_content.utf16_string_base), ak.string_from_str("text/css"), style_element, dom_element_attribute(style_element, html_attribute_names_media) else ak.string_new(), title, CssStyleSheetListAlternate.no, CssStyleSheetListOriginClean.yes, none, none, none)
this.m_associated_css_style_sheet = associated_css_style_sheet
# 7. If element contributes a script-blocking style sheet, append element to its node document's script-blocking style sheet set.
if dom_element_contributes_a_script_blocking_style_sheet(style_element):
# m_document_load_event_delayer.emplace(document): the previous delayer, if any, is destroyed first.
dom_style_element_base_clear_document_load_event_delayer(this)
var delayer: DomDocumentLoadEventDelayer = ---
dom_document_load_event_delayer_construct(&delayer, document)
this.m_document_load_event_delayer = delayer
_ = ak.hash_table_set(dom_document_script_blocking_style_sheet_set(document), style_element)
# FIXME: 8. If element's media attribute's value matches the environment and element is potentially render-blocking, then block rendering on element.
# AD-HOC: Check if we have already loaded the sheet's resources.
let loading_state = css_css_style_sheet_loading_state(associated_css_style_sheet)
if loading_state == CssStyleSheetLoadingState.loaded or loading_state == CssStyleSheetLoadingState.error_:
dom_style_element_base_finished_loading_critical_subresources(this, DomStyleElementBaseAnyFailed.yes if loading_state == CssStyleSheetLoadingState.error_ else DomStyleElementBaseAnyFailed.no)
## The captures of finished_loading_critical_subresources's element task.
struct DomStyleElementBaseFinishedLoadingCriticalSubresourcesTask:
var element: DomElement*
var success: bool
## finished_loading_critical_subresources's element task.
func dom_style_element_base_finished_loading_critical_subresources_task(context: void*?):
let captures = (DomStyleElementBaseFinishedLoadingCriticalSubresourcesTask*)(context else trap("no captures"))
let element = captures.element
let realm = bind_platform_object_realm(element.bind_platform_object())
# 1. If success is true, fire an event named load at element.
# AD-HOC: these should call fire an event this is not implemented anywhere so we dispatch it ourselves
if captures.success:
_ = dom_event_target_dispatch_event(element.dom_event_target(), dom_event_create(realm, html_event_names_load))
# 2. Otherwise, fire an event named error at element.
else:
_ = dom_event_target_dispatch_event(element.dom_event_target(), dom_event_create(realm, html_event_names_error))
# 3. If element contributes a script-blocking style sheet:
if dom_element_contributes_a_script_blocking_style_sheet(element):
let document = dom_node_document(element.dom_node())
# 1. Assert: element's node document's script-blocking style sheet set contains element.
assert(ak.hash_table_contains_t(dom_document_script_blocking_style_sheet_set(document), element))
# 2. Remove element from its node document's script-blocking style sheet set.
_ = ak.hash_table_remove_t(dom_document_script_blocking_style_sheet_set(document), element)
dom_document_schedule_html_parser_end_check(document)
# 4. Unblock rendering on element.
dom_element_unblock_rendering(element)
## Ported from StyleElementBase.cpp:117 Web::DOM::StyleElementBase::finished_loading_critical_subresources.
# https://html.spec.whatwg.org/multipage/semantics.html#the-style-element:critical-subresources
pub func dom_style_element_base_finished_loading_critical_subresources(this: DomStyleElementBase*, any_failed: DomStyleElementBaseAnyFailed):
# 1. Let element be the style element associated with the style sheet in question.
let element = dom_style_element_base_as_element(this)
# 2. Let success be true.
var success = true
# 3. If the attempts to obtain any of the style sheet's critical subresources failed for any reason
# (e.g., DNS error, HTTP 404 response, a connection being prematurely closed, unsupported Content-Type),
# set success to false.
# Note that content-specific errors, e.g., CSS parse errors or PNG decoding errors, do not affect success.
if any_failed == DomStyleElementBaseAnyFailed.yes:
success = false
# 4. Queue an element task on the networking task source given element and the following steps:
let captures = new DomStyleElementBaseFinishedLoadingCriticalSubresourcesTask(element = element, success = success) else trap("out of memory")
_ = dom_element_queue_an_element_task(element, HtmlTaskSource.user_interaction, ak.Function0[unit](call = dom_style_element_base_finished_loading_critical_subresources_task, context = captures))
dom_style_element_base_clear_document_load_event_delayer(this)
## Ported from StyleElementBase.cpp:156 Web::DOM::StyleElementBase::sheet.
# https://www.w3.org/TR/cssom/#dom-linkstyle-sheet
pub func dom_style_element_base_sheet(this: DomStyleElementBase*) -> CssCssStyleSheet*?:
# The sheet attribute must return the associated CSS style sheet for the node or null if there is no associated CSS style sheet.
return this.m_associated_css_style_sheet
## Ported from StyleElementBase.cpp:163 Web::DOM::StyleElementBase::sheet.
# https://www.w3.org/TR/cssom/#dom-linkstyle-sheet
pub func dom_style_element_base_sheet_const(this: DomStyleElementBase*) -> CssCssStyleSheet*?:
# The sheet attribute must return the associated CSS style sheet for the node or null if there is no associated CSS style sheet.
return this.m_associated_css_style_sheet
## Ported from StyleElementBase.cpp:169 Web::DOM::StyleElementBase::visit_style_element_edges.
pub func dom_style_element_base_visit_style_element_edges(this: DomStyleElementBase*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_t(visitor, this.m_associated_css_style_sheet)
gc.cell_visitor_visit_t(visitor, this.m_style_sheet_list)
# mark: StyleElementBase.h ======================================================================
## Ported from StyleElementBase.h:25 Web::DOM::StyleElementBase::style_sheet_list.
pub func dom_style_element_base_style_sheet_list(this: DomStyleElementBase*) -> CssStyleSheetList*?:
return this.m_style_sheet_list
## Ported from StyleElementBase.h:26 Web::DOM::StyleElementBase::style_sheet_list.
pub func dom_style_element_base_style_sheet_list_const(this: DomStyleElementBase*) -> CssStyleSheetList*?:
return this.m_style_sheet_list
## Ported from StyleElementBase.h:36 Web::DOM::StyleElementBase::as_element (virtual).
pub func dom_style_element_base_as_element_impl(this: DomStyleElementBase*) -> DomElement*:
trap("pure virtual: Web::DOM::StyleElementBase::as_element")