Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_base_element.lucb

#==============================================================================================
#
#   html_base_element - HTML::HTMLBaseElement: <base> and its frozen base URL
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLBaseElement.cpp (lines 1-131) and HTMLBaseElement.h: the
#       constructor (which moves the mixin tables to HTMLBaseElement's, as C++'s vptrs move),
#       initialize, the insertion/removal/href change steps that keep the document's base
#       element and frozen base URL current, and the href IDL attribute.
#
#==============================================================================================

## Ported from HTMLBaseElement.cpp:16 Web::HTML::HTMLBaseElement::HTMLBaseElement.
pub func html_base_element_construct(this: HtmlBaseElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLBaseElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_base_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_base_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_base_element_animatable_vtable
    element.global_event_handlers.vtable = &html_base_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_base_element_form_associated_element_vtable
    # URL m_frozen_base_url: a default-constructed URL.
    this.m_frozen_base_url = web_url.url_empty()

## Ported from HTMLBaseElement.cpp:23 Web::HTML::HTMLBaseElement::initialize (override).
pub func html_base_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_base_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLBaseElement.cpp:29 Web::HTML::HTMLBaseElement::inserted (override).
pub func html_base_element_inserted_impl(base: DomNode*):
    let this = (HtmlBaseElement*)base
    html_element_inserted_impl(base)

    let document = dom_node_document(base)
    dom_document_update_base_element(document)

    # The frozen base URL must be immediately set for an element whenever any of the following situations occur:
    # - The base element becomes the first base element in tree order with an href content attribute in its Document.

    # NOTE: inserted() is called after this element has been inserted into the document.
    let first_base_element_with_href_in_document = dom_document_first_base_element_with_href_in_tree_order(document)
    if first_base_element_with_href_in_document == this:
        html_base_element_set_the_frozen_base_url(this)

## Ported from HTMLBaseElement.cpp:44 Web::HTML::HTMLBaseElement::removed_from (override).
pub func html_base_element_removed_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    html_element_removed_from_impl(base, is_subtree_root, old_ancestor, old_root)
    let document = dom_node_document(base)
    let old_first_base_element_with_href_in_tree_order = dom_document_first_base_element_with_href_in_tree_order(document)
    dom_document_update_base_element(document)

    # The frozen base URL must be immediately set for an element whenever any of the following situations occur:
    # - The base element becomes the first base element in tree order with an href content attribute in its Document.
    if let first_base_element_with_href_in_document = dom_document_first_base_element_with_href_in_tree_order(document):
        if old_first_base_element_with_href_in_tree_order != first_base_element_with_href_in_document:
            html_base_element_set_the_frozen_base_url(first_base_element_with_href_in_document)

## Ported from HTMLBaseElement.cpp:57 Web::HTML::HTMLBaseElement::attribute_changed (override).
pub func html_base_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlBaseElement*)base
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)

    # The frozen base URL must be immediately set for an element whenever any of the following situations occur:
    # - The base element is the first base element in tree order with an href content attribute in its Document, and its href content attribute is changed.
    if not ak.fly_string_eq_fly_string(&name, &html_attribute_names_href):
        return

    let document = dom_node_document(base.dom_node())
    dom_document_update_base_element(document)

    let first_base_element_with_href_in_document = dom_document_first_base_element_with_href_in_tree_order(document)
    if first_base_element_with_href_in_document == this:
        html_base_element_set_the_frozen_base_url(this)

## Ported from HTMLBaseElement.cpp:74 Web::HTML::HTMLBaseElement::set_the_frozen_base_url.
# https://html.spec.whatwg.org/multipage/semantics.html#set-the-frozen-base-url
pub func html_base_element_set_the_frozen_base_url(this: HtmlBaseElement*):
    # 1. Let document be element's node document.
    let document = dom_node_document(this.dom_node())

    # 2. Let urlRecord be the result of parsing the value of element's href content attribute with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.)
    let href = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_href)
    let fallback_base_url = dom_document_fallback_base_url(document)
    let url_record = web_url.url_complete_url(&fallback_base_url, ak.string_bytes_as_string_view(&href))

    # 3. If any of the following are true:
    #    - urlRecord is failure;
    #    - urlRecord's scheme is "data" or "javascript"; or
    #    - running Is base allowed for Document? on urlRecord and document returns "Blocked",
    var blocked = true
    if let url = url_record:
        let scheme = web_url.url_scheme(&url)
        blocked = ak.string_eq_string_view(&scheme, ak.sv("data")) or ak.string_eq_string_view(&scheme, ak.sv("javascript")) or content_security_policy_is_base_allowed_for_document(html_element_realm(this.html_element()), url, document) == ContentSecurityPolicyResult.blocked
    if blocked:
        # then set element's frozen base URL to document's fallback base URL and return.
        this.m_frozen_base_url = dom_document_fallback_base_url(document)
        return

    # 4. Set element's frozen base URL to urlRecord.
    this.m_frozen_base_url = url_record else trap("VERIFY_NOT_REACHED")

    # 5. Respond to base URL changes given document.
    dom_document_respond_to_base_url_changes(document)

## Ported from HTMLBaseElement.cpp:104 Web::HTML::HTMLBaseElement::href.
# https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href
pub func html_base_element_href(this: HtmlBaseElement*) -> ak.String:
    # 1. Let document be element's node document.
    let document = dom_node_document(this.dom_node())

    # 2. Let url be the value of the href attribute of this element, if it has one, and the empty string otherwise.
    let url = dom_element_attribute(this.dom_element(), html_attribute_names_href) else ak.string_new()

    # 3. Let urlRecord be the result of parsing url with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by other base elements or itself.)
    # FIXME: Pass in document's character encoding.
    let fallback_base_url = dom_document_fallback_base_url(document)
    let url_record = web_url.url_complete_url(&fallback_base_url, ak.string_bytes_as_string_view(&url))

    # 4. If urlRecord is failure, return url.
    let record = url_record else return url

    # 5. Return the serialization of urlRecord.
    return web_url.url_to_string(&record)

## Ported from HTMLBaseElement.cpp:125 Web::HTML::HTMLBaseElement::set_href.
# https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href
pub func html_base_element_set_href(this: HtmlBaseElement*, href: ak.String):
    # The href IDL attribute, on setting, must set the href content attribute to the given new value.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_href, href)

## Ported from HTMLBaseElement.h:23 Web::HTML::HTMLBaseElement::frozen_base_url.
pub func html_base_element_frozen_base_url(this: HtmlBaseElement*) -> web_url.Url:
    return this.m_frozen_base_url

## Ported from HTMLBaseElement.h:33 Web::HTML::HTMLBaseElement::is_html_base_element (override).
pub func html_base_element_is_html_base_element_impl(base: DomNode*) -> bool:
    return true