dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / shadow_root_1.lucb
#==============================================================================================
#
# shadow_root_1 - DOM::ShadowRoot: construction, event parent, innerHTML, style sheets, ids
#
# DESCRIPTION:
# Port of LibWeb/DOM/ShadowRoot.cpp (lines 1-283): the constructor and finalizer,
# fullscreenElement, onslotchange, the event parent, innerHTML / getHTML /
# setHTMLUnsafe, activeElement, the style sheet lists (document and adopted), getAnimations,
# the id map and the slot registry. shadow_root_2 holds the part element map and the
# inline functions of ShadowRoot.h.
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from ShadowRoot.cpp:31 Web::DOM::ShadowRoot::ShadowRoot.
pub func dom_shadow_root_construct(this: DomShadowRoot*, document: DomDocument*, host: DomElement*, mode: BindShadowRootMode):
dom_document_fragment_construct(this.dom_document_fragment(), document)
dom_shadow_root_init_fields(this)
this.m_mode = mode
css_style_scope_construct(&this.m_style_scope, this.dom_node())
dom_document_register_shadow_root(document, this)
dom_document_fragment_set_host(this.dom_document_fragment(), host)
## Ported from ShadowRoot.cpp:40 Web::DOM::ShadowRoot::finalize (override).
pub func dom_shadow_root_finalize_impl(base: gc.Cell*):
let this = (DomShadowRoot*)base
# Base::finalize(): DocumentFragment inherits Node's.
dom_node_finalize_impl(base)
dom_document_unregister_shadow_root(dom_node_document(this.dom_node()), this)
## Ported from ShadowRoot.cpp:47 Web::DOM::ShadowRoot::fullscreen_element_for_bindings.
# https://fullscreen.spec.whatwg.org/#dom-document-fullscreenelement
pub func dom_shadow_root_fullscreen_element_for_bindings(this: DomShadowRoot*) -> DomElement*?:
# 1. If this is a shadow root and its host is not connected, then return null.
let host = dom_document_fragment_host(this.dom_document_fragment()) else return none
if not dom_node_is_connected(host.dom_node()):
return none
# 2. Let candidate be the result of retargeting fullscreen element against this.
# NB: ShadowRoot does not have it's own top layer. But the algorithm says to get the fullscreen element from the
# top layer, so it's grabbed from this' document.
var fullscreen_element: DomEventTarget*? = none
if let element = dom_document_fullscreen_element(dom_node_document(this.dom_node())):
fullscreen_element = element.dom_event_target()
let candidate = dom_retarget(fullscreen_element, this.dom_event_target()) else return none
# 3. If candidate and this are in the same tree, then return candidate.
let retargeted_element = as_dom_element(candidate)
if tree_node_root(retargeted_element.dom_node()) == tree_node_root(this.dom_node()):
return retargeted_element
# 4. Return null.
return none
## Ported from ShadowRoot.cpp:68 Web::DOM::ShadowRoot::initialize (override).
pub func dom_shadow_root_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.shadow_root)
dom_document_fragment_initialize_impl(base, realm)
# mark: Events ==================================================================================
## Ported from ShadowRoot.cpp:75 Web::DOM::ShadowRoot::set_onslotchange.
# https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
pub func dom_shadow_root_set_onslotchange(this: DomShadowRoot*, event_handler: IdlCallbackType*?):
dom_event_target_set_event_handler_attribute(this.dom_event_target(), html_event_names_slotchange, event_handler)
## Ported from ShadowRoot.cpp:81 Web::DOM::ShadowRoot::onslotchange.
# https://dom.spec.whatwg.org/#dom-shadowroot-onslotchange
pub func dom_shadow_root_onslotchange(this: DomShadowRoot*) -> IdlCallbackType*?:
return dom_event_target_event_handler_attribute(this.dom_event_target(), html_event_names_slotchange)
## Ported from ShadowRoot.cpp:87 Web::DOM::ShadowRoot::get_parent (override).
# https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
pub func dom_shadow_root_get_parent_impl(base: DomEventTarget*, event: DomEvent*) -> DomEventTarget*?:
let this = (DomShadowRoot*)base
if not dom_event_composed(event):
let first_target = ak.vector_first(dom_event_path(event)).invocation_target else trap("VERIFY: invocation target")
let events_first_invocation_target = as_dom_node(first_target)
if tree_node_root(events_first_invocation_target) == this.dom_node():
return none
return (DomEventTarget*?)dom_document_fragment_host(this.dom_document_fragment())
# mark: Markup ==================================================================================
## Ported from ShadowRoot.cpp:99 Web::DOM::ShadowRoot::inner_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
pub func dom_shadow_root_inner_html(this: DomShadowRoot*) -> TrustedTypesTrustedHtmlOrString!:
return .utf16_string(try dom_node_serialize_fragment(this.dom_node(), HtmlRequireWellFormed.yes))
## Ported from ShadowRoot.cpp:105 Web::DOM::ShadowRoot::set_inner_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
pub func dom_shadow_root_set_inner_html(this: DomShadowRoot*, value: TrustedTypesTrustedHtmlOrString) -> !:
# 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
# TrustedHTML, this's relevant global object, the given value, "ShadowRoot innerHTML", and "script".
# (TrustedTypes::Script.to_string() is "'script'".)
let compliant_string = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_html, html_relevant_global_object((JsObject*)this), dom_element_trusted_html_or_string_as_trusted_type(value), TrustedTypesInjectionSink.shadow_root_inner_html, ak.string_from_str("'script'"))
# 2. Let context be this's host.
let context = dom_document_fragment_host(this.dom_document_fragment()) else trap("VERIFY: shadow root host")
# 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString.
let compliant_utf8 = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&compliant_string.utf16_string_base)
let fragment = try dom_element_parse_fragment(context, ak.string_bytes_as_string_view(&compliant_utf8))
# 4. Replace all with fragment within this.
dom_node_replace_all(this.dom_node(), fragment.dom_node())
# NOTE: We don't invalidate style & layout for <template> elements since they don't affect rendering.
if not is_html_template_element(this.dom_node()):
dom_node_set_needs_style_update(this.dom_node(), true)
if dom_node_is_connected(this.dom_node()):
# NOTE: Since the DOM has changed, we have to rebuild this shadow root's layout subtree.
dom_node_set_needs_layout_tree_update(this.dom_node(), true, DomSetNeedsLayoutTreeUpdateReason.shadow_root_set_inner_html)
dom_node_set_needs_style_update(this.dom_node(), true)
## Ported from ShadowRoot.cpp:141 Web::DOM::ShadowRoot::get_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-gethtml
pub func dom_shadow_root_get_html(this: DomShadowRoot*, options: DomGetHtmlOptions) -> ak.String!:
# ShadowRoot's getHTML(options) method steps are to return the result
# of HTML fragment serialization algorithm with this,
# options["serializableShadowRoots"], and options["shadowRoots"].
let serializable_shadow_roots = HtmlParserSerializableShadowRoots.yes if options.serializable_shadow_roots else HtmlParserSerializableShadowRoots.no
return html_parser_serialize_html_fragment(this.dom_node(), serializable_shadow_roots, options.shadow_roots)
## Ported from ShadowRoot.cpp:153 Web::DOM::ShadowRoot::set_html_unsafe.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-sethtmlunsafe
pub func dom_shadow_root_set_html_unsafe(this: DomShadowRoot*, html: TrustedTypesTrustedHtmlOrString) -> !:
# 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with
# TrustedHTML, this's relevant global object, html, "ShadowRoot setHTMLUnsafe", and "script".
# (TrustedTypes::Script.to_string() is "'script'".)
let compliant_html = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_html, html_relevant_global_object((JsObject*)this), dom_element_trusted_html_or_string_as_trusted_type(html), TrustedTypesInjectionSink.shadow_root_set_html_unsafe, ak.string_from_str("'script'"))
# 2. Unsafely set HTML given this, this's shadow host, and compliantHTML.
let host = dom_document_fragment_host(this.dom_document_fragment()) else trap("VERIFY: shadow root host")
let compliant_utf8 = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&compliant_html.utf16_string_base)
try dom_node_unsafely_set_html(this.dom_node(), host, ak.string_bytes_as_string_view(&compliant_utf8))
## Ported from ShadowRoot.cpp:170 Web::DOM::ShadowRoot::active_element.
pub func dom_shadow_root_active_element(this: DomShadowRoot*) -> DomElement*?:
return dom_calculate_active_element(this.dom_node())
# mark: Style sheets ============================================================================
## Ported from ShadowRoot.cpp:175 Web::DOM::ShadowRoot::style_sheets.
pub func dom_shadow_root_style_sheets(this: DomShadowRoot*) -> CssStyleSheetList*:
if this.m_style_sheets == none:
this.m_style_sheets = css_style_sheet_list_create(this.dom_node())
return this.m_style_sheets else trap("unreachable")
## Ported from ShadowRoot.cpp:182 Web::DOM::ShadowRoot::style_sheets.
pub func dom_shadow_root_style_sheets_const(this: DomShadowRoot*) -> CssStyleSheetList*:
return dom_shadow_root_style_sheets(this)
## Ported from ShadowRoot.cpp:187 Web::DOM::ShadowRoot::visit_edges (override).
pub func dom_shadow_root_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomShadowRoot*)base
dom_document_fragment_visit_edges_impl(base, visitor)
css_style_scope_visit_edges(&this.m_style_scope, visitor)
gc.cell_visitor_visit_t(visitor, this.m_style_sheets)
gc.cell_visitor_visit_t(visitor, this.m_adopted_style_sheets)
dom_anchor_name_map_visit_edges(&this.m_anchor_name_map, visitor)
for entry in ak.hash_map_begin(&this.m_part_element_map):
for element in ak.ordered_hash_table_begin(&entry.value):
dom_abstract_element_visit(element, visitor)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_custom_element_registry) # opaque in phase 1
## Ported from ShadowRoot.cpp:201 Web::DOM::ShadowRoot::adopted_style_sheets.
pub func dom_shadow_root_adopted_style_sheets(this: DomShadowRoot*) -> 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")
## Ported from ShadowRoot.cpp:208 Web::DOM::ShadowRoot::set_adopted_style_sheets.
pub func dom_shadow_root_set_adopted_style_sheets(this: DomShadowRoot*, 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")
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")
## Ported from ShadowRoot.cpp:225 Web::DOM::ShadowRoot::for_each_css_style_sheet.
pub func dom_shadow_root_for_each_css_style_sheet(this: DomShadowRoot*, callback: ak.Function1[CssCssStyleSheet*, unit]):
let call = callback.call else trap("VERIFY: callback")
for style_sheet in ak.vector_span(css_style_sheet_list_sheets(dom_shadow_root_style_sheets(this))):
call(callback.context, style_sheet)
if let adopted_style_sheets = this.m_adopted_style_sheets:
idl_observable_array_for_each[CssCssStyleSheet, ak.Function1[CssCssStyleSheet*, unit]](adopted_style_sheets, callback)
## for_each_active_css_style_sheet's adopted-sheet lambda: the callback for sheets that are not disabled.
func dom_shadow_root_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()):
let call = callback.call else trap("VERIFY: callback")
call(callback.context, style_sheet)
## Ported from ShadowRoot.cpp:237 Web::DOM::ShadowRoot::for_each_active_css_style_sheet.
pub func dom_shadow_root_for_each_active_css_style_sheet(this: DomShadowRoot*, callback: ak.Function1[CssCssStyleSheet*, unit]):
let call = callback.call else trap("VERIFY: callback")
for style_sheet in ak.vector_span(css_style_sheet_list_sheets(dom_shadow_root_style_sheets(this))):
if not css_style_sheet_disabled(style_sheet.css_style_sheet()):
call(callback.context, style_sheet)
if let adopted_style_sheets = this.m_adopted_style_sheets:
var captured_callback = callback
let adopted = ak.Function1[CssCssStyleSheet*, unit](call = dom_shadow_root_for_each_active_css_style_sheet_adopted, context = &captured_callback)
idl_observable_array_for_each[CssCssStyleSheet, ak.Function1[CssCssStyleSheet*, unit]](adopted_style_sheets, adopted)
## Ported from ShadowRoot.cpp:252 Web::DOM::ShadowRoot::get_animations.
pub func dom_shadow_root_get_animations(this: DomShadowRoot*) -> ak.Vector[AnimationsAnimation*]!:
dom_document_update_style(dom_node_document(this.dom_node()))
return try dom_calculate_get_animations(this.dom_node())
# mark: Ids and slots ===========================================================================
## Ported from ShadowRoot.cpp:258 Web::DOM::ShadowRoot::element_by_id.
pub func dom_shadow_root_element_by_id(this: DomShadowRoot*) -> DomElementByIdMap*:
if this.m_element_by_id == none:
this.m_element_by_id = new DomElementByIdMap else trap("out of memory")
return this.m_element_by_id else trap("unreachable")
## Ported from ShadowRoot.cpp:265 Web::DOM::ShadowRoot::register_slot.
pub func dom_shadow_root_register_slot(this: DomShadowRoot*, slot: HtmlSlotElement*):
if this.m_slot_registry == none:
this.m_slot_registry = new DomSlotRegistry else trap("out of memory")
let slot_registry = this.m_slot_registry else trap("unreachable")
dom_slot_registry_add(slot_registry, slot)
## Ported from ShadowRoot.cpp:272 Web::DOM::ShadowRoot::unregister_slot.
pub func dom_shadow_root_unregister_slot(this: DomShadowRoot*, slot: HtmlSlotElement*):
if let slot_registry = this.m_slot_registry:
dom_slot_registry_remove(slot_registry, slot)
## Ported from ShadowRoot.cpp:278 Web::DOM::ShadowRoot::first_slot_with_name.
pub func dom_shadow_root_first_slot_with_name(this: DomShadowRoot*, name: ak.FlyString) -> HtmlSlotElement*?:
let slot_registry = this.m_slot_registry else return none
return dom_slot_registry_first_slot_with_name(slot_registry, name)