Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_script_element_2.lucb

#==============================================================================================
#
#   html_script_element_2 - HTML::HTMLScriptElement: connection steps, readiness and the IDL
#                           attributes
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLScriptElement.cpp (lines 644-876) and the inlines of
#       HTMLScriptElement.h: the children changed and post-connection steps, marking as ready,
#       the Range hooks, the Trusted Types text / src / textContent / innerText accessors,
#       async, the cloning steps, implicit render blocking, and the parser's setters (C++'s
#       Badge<T> parameters dropped).
#
#==============================================================================================

# mark: HTMLScriptElement.cpp ====================================================================

## Ported from HTMLScriptElement.cpp:645 Web::HTML::HTMLScriptElement::children_changed (override).
# https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:children-changed-steps
pub func html_script_element_children_changed_impl(base: DomNode*, metadata: DomNodeChildrenChangedMetadata):
    dom_element_children_changed_impl(base, metadata)

    # AD-HOC: Avoid running script on child removal, matching the behaviour of other browsers.
    #         We also do not run script on child mutation, matching the behaviour of chromium and webkit.
    #         See: https://github.com/whatwg/html/issues/12279
    if metadata.type_ != DomNodeChildrenChangedMetadataType.inserted:
        return

    # 1. If the script element is not connected, then return.
    if not dom_node_is_connected(base):
        return

    # 2. Run the script HTML element post-connection steps, given the script element.
    dom_node_post_connection(base)

## Ported from HTMLScriptElement.cpp:664 Web::HTML::HTMLScriptElement::post_connection (override).
# https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-steps
pub func html_script_element_post_connection_impl(base: DomNode*):
    let this = (HtmlScriptElement*)base
    # 1. If insertedNode is parser-inserted, then return.
    if html_script_element_is_parser_inserted(this):
        return

    # 2. Prepare the script element given insertedNode.
    html_script_element_prepare_script(this)

## Ported from HTMLScriptElement.cpp:675 Web::HTML::HTMLScriptElement::mark_as_ready.
# https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready
pub func html_script_element_mark_as_ready(this: HtmlScriptElement*, result: HtmlScriptElementResult):
    # 1. Set el's result to result.
    this.m_result = result

    # 2. If el's steps to run when the result is ready are not null, then run them.
    if ak.function0_is_set(&this.m_steps_to_run_when_the_result_is_ready):
        ak.function0_call(&this.m_steps_to_run_when_the_result_is_ready)

    # 3. Set el's steps to run when the result is ready to null.
    this.m_steps_to_run_when_the_result_is_ready = ak.Function0[unit](call = none, context = none)

    # 4. Set el's delaying the load event to false.
    html_script_element_clear_document_load_event_delayer(this)

    if let preparation_time_document = this.m_preparation_time_document:
        dom_document_schedule_html_parser_end_check(preparation_time_document)

## Ported from HTMLScriptElement.cpp:694 Web::HTML::HTMLScriptElement::unmark_as_already_started.
pub func html_script_element_unmark_as_already_started(this: HtmlScriptElement*):
    this.m_already_started = false

## Ported from HTMLScriptElement.cpp:699 Web::HTML::HTMLScriptElement::unmark_as_parser_inserted.
pub func html_script_element_unmark_as_parser_inserted(this: HtmlScriptElement*):
    this.m_parser_document = none

# mark: Trusted Types accessors ==================================================================

## Ported from HTMLScriptElement.cpp:705 Web::HTML::HTMLScriptElement::src.
# https://github.com/w3c/trusted-types/blob/0f01aee35c3684e8cb20e7d83445ad65938f545d/spec/index.bs#L1341
pub func html_script_element_src(this: HtmlScriptElement*) -> TrustedTypesTrustedScriptUrlOrString:
    # 1. Let element be the result of running this's get the element.

    # 2. Let contentAttributeValue be the result of running this's get the content attribute.
    let maybe_src = dom_element_get_attribute(this.dom_element(), html_attribute_names_src)

    # 3. If contentAttributeValue is null, then return the empty string.
    let raw = maybe_src else return TrustedTypesTrustedScriptUrlOrString.utf16_string(ak.utf16_string_new())

    # 4. Let urlString be the result of encoding-parsing-and-serializing a URL given contentAttributeValue, relative to element's node document.
    let url_string = dom_document_encoding_parse_and_serialize_url(dom_node_document(this.dom_node()), ak.string_bytes_as_string_view(&raw))

    # 5. If urlString is not failure, then return urlString.
    if let serialized = url_string:
        return TrustedTypesTrustedScriptUrlOrString.utf16_string(ak.utf16_string_from_utf8_without_validation(ak.string_bytes_as_string_view(&serialized)))

    # 6. Return contentAttributeValue, converted to a scalar value string.
    return TrustedTypesTrustedScriptUrlOrString.utf16_string(ak.utf16_string_new())

## The implicit conversion of a TrustedScriptOrString to get_trusted_type_compliant_string's input
## variant (TrustedHTML, TrustedScript, TrustedScriptURL or Utf16String).
func html_script_element_trusted_script_input(text: TrustedTypesTrustedScriptOrString) -> TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String:
    return match text:
        .trusted_types_trusted_script(script) => TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String.trusted_types_trusted_script(script)
        .utf16_string(string) => TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String.utf16_string(string)

## Ported from HTMLScriptElement.cpp:730 Web::HTML::HTMLScriptElement::set_text.
# https://www.w3.org/TR/trusted-types/#the-text-idl-attribute
pub func html_script_element_set_text(this: HtmlScriptElement*, text: TrustedTypesTrustedScriptOrString) -> !:
    # 1. Let value be the result of calling Get Trusted Type compliant string with
    #    TrustedScript, this’s relevant global object, the given value, HTMLScriptElement text, and script.
    let value = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_script, html_relevant_global_object((JsObject*)this), html_script_element_trusted_script_input(text), TrustedTypesInjectionSink.html_script_element_text, ak.fly_string_to_string_const(&trusted_types_script))

    # 2. Set this’s script text value to the given value.
    this.m_script_text = value

    # 3. String replace all with the given value within this.
    dom_node_string_replace_all(this.dom_node(), value)

## Ported from HTMLScriptElement.cpp:750 Web::HTML::HTMLScriptElement::set_src.
# https://www.w3.org/TR/trusted-types/#the-src-idl-attribute
pub func html_script_element_set_src(this: HtmlScriptElement*, text: TrustedTypesTrustedScriptUrlOrString) -> !:
    # 1. Let value be the result of calling Get Trusted Type compliant string with
    #    TrustedScriptURL, this’s relevant global object, the given value, HTMLScriptElement src, and script.
    let input = match text:
        .trusted_types_trusted_script_url(url) => TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String.trusted_types_trusted_script_url(url)
        .utf16_string(string) => TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String.utf16_string(string)
    let value = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_script_url, html_relevant_global_object((JsObject*)this), input, TrustedTypesInjectionSink.html_script_element_src, ak.fly_string_to_string_const(&trusted_types_script))

    # 2. Set this’s src content attribute to value.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_src, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&value.utf16_string_base))

## Ported from HTMLScriptElement.cpp:767 Web::HTML::HTMLScriptElement::text_content.
# https://w3c.github.io/trusted-types/dist/spec/#the-textContent-idl-attribute
pub func html_script_element_text_content(this: HtmlScriptElement*) -> TrustedTypesNullableTrustedScriptOrString:
    # 1. Return the result of running get text content with this.
    return TrustedTypesNullableTrustedScriptOrString.utf16_string(dom_node_descendant_text_content(this.dom_node()))

## Ported from HTMLScriptElement.cpp:774 Web::HTML::HTMLScriptElement::set_text_content.
# https://w3c.github.io/trusted-types/dist/spec/#the-textContent-idl-attribute
pub func html_script_element_set_text_content(this: HtmlScriptElement*, text: TrustedTypesNullableTrustedScriptOrString) -> !:
    # NOTE: We still require this from the base implementation.
    # https://dom.spec.whatwg.org/#dom-node-textcontent
    # The textContent setter steps are to, if the given value is null, act as if it was the empty string instead, and then run set text content with this and the given value.
    let non_null_text = match text:
        .empty => TrustedTypesTrustedScriptOrString.utf16_string(ak.utf16_string_new())
        .trusted_types_trusted_script(script) => TrustedTypesTrustedScriptOrString.trusted_types_trusted_script(script)
        .utf16_string(string) => TrustedTypesTrustedScriptOrString.utf16_string(string)

    # 1. Let value be the result of calling Get Trusted Type compliant string with
    #    TrustedScript, this's relevant global object, the given value, HTMLScriptElement textContent, and script.
    let value = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_script, html_relevant_global_object((JsObject*)this), html_script_element_trusted_script_input(non_null_text), TrustedTypesInjectionSink.html_script_element_text_content, ak.fly_string_to_string_const(&trusted_types_script))

    # 2. Set this’s script text value to value.
    this.m_script_text = value

    # 3. Run set text content with this and value.
    dom_node_string_replace_all(this.dom_node(), value)

## Ported from HTMLScriptElement.cpp:801 Web::HTML::HTMLScriptElement::inner_text.
# https://w3c.github.io/trusted-types/dist/spec/#the-innerText-idl-attribute
pub func html_script_element_inner_text(this: HtmlScriptElement*) -> TrustedTypesTrustedScriptOrString:
    # 1. Return the result of running get text content with this.
    return TrustedTypesTrustedScriptOrString.utf16_string(html_element_get_the_text_steps(this.html_element()))

## Ported from HTMLScriptElement.cpp:808 Web::HTML::HTMLScriptElement::set_inner_text.
# https://w3c.github.io/trusted-types/dist/spec/#the-innerText-idl-attribute
pub func html_script_element_set_inner_text(this: HtmlScriptElement*, text: TrustedTypesTrustedScriptOrString) -> !:
    # 1. Let value be the result of calling Get Trusted Type compliant string with
    #    TrustedScript, this’s relevant global object, the given value, HTMLScriptElement innerText, and script.
    let value = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_script, html_relevant_global_object((JsObject*)this), html_script_element_trusted_script_input(text), TrustedTypesInjectionSink.html_script_element_inner_text, ak.fly_string_to_string_const(&trusted_types_script))

    # 2. Set this’s script text value to value.
    this.m_script_text = value

    # 3. Run set the inner text steps with this and value.
    html_element_set_inner_text(this.html_element(), ak.utf16_string_base_utf16_view(&value.utf16_string_base))

# mark: async, cloning and render blocking =======================================================

## Ported from HTMLScriptElement.cpp:828 Web::HTML::HTMLScriptElement::async.
# https://html.spec.whatwg.org/multipage/scripting.html#dom-script-async
pub func html_script_element_async(this: HtmlScriptElement*) -> bool:
    # 1. If this's force async is true, then return true.
    if this.m_force_async:
        return true

    # 2. If this's async content attribute is present, then return true.
    if dom_element_has_attribute(this.dom_element(), html_attribute_names_async):
        return true

    # 3. Return false.
    return false

## Ported from HTMLScriptElement.cpp:842 Web::HTML::HTMLScriptElement::set_async.
pub func html_script_element_set_async(this: HtmlScriptElement*, async: bool):
    # 1. Set this's force async to false.
    this.m_force_async = false

    # 2. If the given value is true, then set this's async content attribute to the empty string.
    if async:
        dom_element_set_attribute_value(this.dom_element(), html_attribute_names_async, ak.string_new())
    # 3. Otherwise, remove this's async content attribute.
    else:
        dom_element_remove_attribute(this.dom_element(), html_attribute_names_async)

## Ported from HTMLScriptElement.cpp:858 Web::HTML::HTMLScriptElement::cloned (override).
# https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:concept-node-clone-ext
pub func html_script_element_cloned_impl(base: DomNode*, copy: DomNode*, subtree: bool) -> !:
    let this = (HtmlScriptElement*)base
    try html_element_cloned_impl(base, copy, subtree)

    # The cloning steps for script elements given node, copy, and subtree are to set copy's already started to node's already started.
    let script_copy = as_html_script_element(copy)
    script_copy.m_already_started = this.m_already_started

## Ported from HTMLScriptElement.cpp:870 Web::HTML::HTMLScriptElement::is_implicitly_potentially_render_blocking (override).
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#implicitly-potentially-render-blocking
pub func html_script_element_is_implicitly_potentially_render_blocking_impl(base: DomElement*) -> bool:
    let this = (HtmlScriptElement*)base
    # A script element el is implicitly potentially render-blocking if el's type is "classic", el is parser-inserted, and el does not have an async or defer attribute.
    return this.m_script_type == HtmlScriptElementScriptType.classic and html_script_element_is_parser_inserted(this) and not dom_element_has_attribute(base, html_attribute_names_async) and not dom_element_has_attribute(base, html_attribute_names_defer)

# mark: Inline members of HTMLScriptElement.h ====================================================

## Ported from HTMLScriptElement.h:28 Web::HTML::HTMLScriptElement::is_force_async.
pub func html_script_element_is_force_async(this: HtmlScriptElement*) -> bool:
    return this.m_force_async

## Ported from HTMLScriptElement.h:29 Web::HTML::HTMLScriptElement::is_ready_to_be_parser_executed.
pub func html_script_element_is_ready_to_be_parser_executed(this: HtmlScriptElement*) -> bool:
    return this.m_ready_to_be_parser_executed

## Ported from HTMLScriptElement.h:30 Web::HTML::HTMLScriptElement::failed_to_load.
pub func html_script_element_failed_to_load(this: HtmlScriptElement*) -> bool:
    return this.m_failed_to_load

## Ported from HTMLScriptElement.h:33 Web::HTML::HTMLScriptElement::set_parser_document.
pub func html_script_element_set_parser_document(this: HtmlScriptElement*, document: DomDocument*):
    this.m_parser_document = document

## Ported from HTMLScriptElement.h:36 Web::HTML::HTMLScriptElement::set_force_async.
pub func html_script_element_set_force_async(this: HtmlScriptElement*, b: bool):
    this.m_force_async = b

## Ported from HTMLScriptElement.h:39 Web::HTML::HTMLScriptElement::set_already_started.
pub func html_script_element_set_already_started(this: HtmlScriptElement*, b: bool):
    this.m_already_started = b

## Ported from HTMLScriptElement.h:42 Web::HTML::HTMLScriptElement::prepare_script (the parser's entry point).
pub func html_script_element_prepare_script_t(this: HtmlScriptElement*):
    html_script_element_prepare_script(this)

## Ported from HTMLScriptElement.h:46 Web::HTML::HTMLScriptElement::is_parser_inserted.
pub func html_script_element_is_parser_inserted(this: HtmlScriptElement*) -> bool:
    return this.m_parser_document != none

## Ported from HTMLScriptElement.h:52 Web::HTML::HTMLScriptElement::supports.
# https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
pub func html_script_element_supports(arg0: Vm*, type_: ak.StringView) -> bool:
    return ak.string_view_equals(&type_, "classic") or ak.string_view_equals(&type_, "module") or ak.string_view_equals(&type_, "importmap")

## Ported from HTMLScriptElement.h:57 Web::HTML::HTMLScriptElement::set_source_line_number.
pub func html_script_element_set_source_line_number(this: HtmlScriptElement*, source_line_number: usize):
    this.m_source_line_number = source_line_number

## Ported from HTMLScriptElement.h:62 Web::HTML::HTMLScriptElement::text.
pub func html_script_element_text(this: HtmlScriptElement*) -> TrustedTypesTrustedScriptOrString:
    return TrustedTypesTrustedScriptOrString.utf16_string(dom_node_child_text_content(this.dom_node()))

## Ported from HTMLScriptElement.h:79 Web::HTML::HTMLScriptElement::set_string_text.
pub func html_script_element_set_string_text(this: HtmlScriptElement*, value: ak.Utf16String):
    this.m_script_text = value

## Ported from HTMLScriptElement.h:88 Web::HTML::HTMLScriptElement::is_html_script_element (override).
pub func html_script_element_is_html_script_element_impl(base: DomNode*) -> bool:
    return true