Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / dom / attr.lucb

#==============================================================================================
#
#   attr - DOM::Attr: attribute nodes, setting and changing their values
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Attr.cpp (lines 1-142) and the inline functions of Attr.h: creation
#       and cloning, the owner element, "set an existing attribute value", "change an
#       attribute" and "handle attribute changes" (the mutation record, the
#       attributeChangedCallback reaction and the element's attribute change steps).
#
#==============================================================================================

# mark: Creation ================================================================================

## Ported from Attr.cpp:23 Web::DOM::Attr::create.
pub func dom_attr_create_fly_string(document: DomDocument*, local_name: ak.FlyString, value: ak.String, owner_element: DomElement*? = none) -> DomAttr*:
    var qualified_name: DomQualifiedName = ---
    dom_qualified_name_construct(&qualified_name, local_name, none, none)
    return realm_create_dom_attr(bind_platform_object_realm(document.bind_platform_object()), document, qualified_name, value, owner_element)

## Ported from Attr.cpp:28 Web::DOM::Attr::create.
pub func dom_attr_create_dom_qualified_name(document: DomDocument*, qualified_name: DomQualifiedName, value: ak.String, owner_element: DomElement*? = none) -> DomAttr*:
    return realm_create_dom_attr(bind_platform_object_realm(document.bind_platform_object()), document, qualified_name, value, owner_element)

## Ported from Attr.cpp:33 Web::DOM::Attr::clone.
pub func dom_attr_clone(this: DomAttr*, document: DomDocument*) -> DomAttr*:
    return realm_create_dom_attr(bind_platform_object_realm(this.bind_platform_object()), document, this.m_qualified_name, this.m_value, none)

## Ported from Attr.cpp:38 Web::DOM::Attr::Attr.
pub func dom_attr_construct(this: DomAttr*, document: DomDocument*, qualified_name: DomQualifiedName, value: ak.String, owner_element: DomElement*?):
    dom_node_construct_dom_document(this.dom_node(), document, DomNodeType.attribute_node)
    this.m_qualified_name = qualified_name
    this.m_value = value
    this.m_owner_element = owner_element

## Ported from Attr.cpp:46 Web::DOM::Attr::initialize (override).
pub func dom_attr_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.attr)
    # Base::initialize(realm): Node inherits EventTarget's.
    dom_event_target_initialize_impl(base, realm)

## Ported from Attr.cpp:52 Web::DOM::Attr::visit_edges (override).
pub func dom_attr_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (DomAttr*)base
    dom_node_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_owner_element)

# mark: Owner element ===========================================================================

## Ported from Attr.cpp:58 Web::DOM::Attr::owner_element.
pub func dom_attr_owner_element(this: DomAttr*) -> DomElement*?:
    return this.m_owner_element

## Ported from Attr.cpp:63 Web::DOM::Attr::owner_element.
pub func dom_attr_owner_element_const(this: DomAttr*) -> DomElement*?:
    return this.m_owner_element

## Ported from Attr.cpp:68 Web::DOM::Attr::set_owner_element.
pub func dom_attr_set_owner_element(this: DomAttr*, owner_element: DomElement*?):
    this.m_owner_element = owner_element

# mark: Values ==================================================================================

## Ported from Attr.cpp:74 Web::DOM::Attr::set_value.
# https://dom.spec.whatwg.org/#set-an-existing-attribute-value
pub func dom_attr_set_value(this: DomAttr*, value: ak.String) -> !:
    # 1. If attribute’s element is null, then set attribute’s value to value and return.
    if dom_attr_owner_element(this) == none:
        this.m_value = value
        return

    # 2. Let element be attribute’s element.
    let element = dom_attr_owner_element(this) else trap("unreachable")

    # 3. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value with
    #    attribute’s local name, attribute’s namespace, element, and value.
    var namespace_value: ak.Utf16String? = none
    if let namespace_uri = dom_attr_namespace_uri(this):
        namespace_value = ak.utf16_string_from_utf8_fly_string(&namespace_uri)
    let verified_value = try trusted_types_get_trusted_types_compliant_attribute_value(dom_attr_local_name(this), namespace_value, element, .utf16_string(ak.utf16_string_from_utf8_string(&value)))

    # 4. If attribute’s element is null, then set attribute’s value to verifiedValue, and return.
    if dom_attr_owner_element(this) == none:
        this.m_value = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&verified_value.utf16_string_base)
        return

    # 5. Change attribute to verifiedValue.
    dom_attr_change_attribute(this, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&verified_value.utf16_string_base))

## Ported from Attr.cpp:106 Web::DOM::Attr::change_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-change
pub func dom_attr_change_attribute(this: DomAttr*, value: ak.String):
    # 1. Let oldValue be attribute’s value.
    let old_value = this.m_value

    # 2. Set attribute’s value to value.
    this.m_value = value

    # 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
    let element = dom_attr_owner_element(this) else trap("VERIFY: change_attribute without an element")
    dom_attr_handle_attribute_changes(this, element, old_value, this.m_value)

## Ported from Attr.cpp:119 Web::DOM::Attr::handle_attribute_changes.
# https://dom.spec.whatwg.org/#handle-attribute-changes
pub func dom_attr_handle_attribute_changes(this: DomAttr*, element: DomElement*, old_value: ak.String?, new_value: ak.String?):
    # 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
    var no_nodes: ak.Vector[DomNode*?]
    dom_node_queue_mutation_record(element.dom_node(), dom_mutation_type_attributes, dom_attr_local_name(this), dom_attr_namespace_uri(this), old_value, no_nodes, no_nodes, none, none)

    # 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback",
    #    and « attribute’s local name, oldValue, newValue, attribute’s namespace ».
    if dom_element_is_custom(element):
        let vm = js_cell_vm(this.js_cell())

        var arguments: ak.Vector[JsValue]
        ak.vector_append_t(&arguments, js_primitive_string_create_fly_string(vm, dom_attr_local_name(this)))
        if let old_string = old_value:
            ak.vector_append_t(&arguments, js_primitive_string_create_string(vm, old_string))
        else:
            ak.vector_append_t(&arguments, js_null())
        if let new_string = new_value:
            ak.vector_append_t(&arguments, js_primitive_string_create_string(vm, new_string))
        else:
            ak.vector_append_t(&arguments, js_null())
        if let namespace_uri = dom_attr_namespace_uri(this):
            ak.vector_append_t(&arguments, js_primitive_string_create_fly_string(vm, namespace_uri))
        else:
            ak.vector_append_t(&arguments, js_null())

        dom_element_enqueue_a_custom_element_callback_reaction(element, html_custom_element_reaction_names_attribute_changed_callback, arguments)

    # 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
    dom_element_run_attribute_change_steps(element, dom_attr_local_name(this), old_value, new_value, dom_attr_namespace_uri(this))

# mark: Attr.h ==================================================================================

## Ported from Attr.h:28 Web::DOM::Attr::node_name (override).
pub func dom_attr_node_name_impl(base: DomNode*) -> ak.FlyString:
    return dom_attr_name((DomAttr*)base)

## Ported from Attr.h:30 Web::DOM::Attr::namespace_uri.
pub func dom_attr_namespace_uri(this: DomAttr*) -> ak.FlyString?:
    return dom_qualified_name_namespace(&this.m_qualified_name)

## Ported from Attr.h:31 Web::DOM::Attr::prefix.
pub func dom_attr_prefix(this: DomAttr*) -> ak.FlyString?:
    return dom_qualified_name_prefix(&this.m_qualified_name)

## Ported from Attr.h:32 Web::DOM::Attr::local_name.
pub func dom_attr_local_name(this: DomAttr*) -> ak.FlyString:
    return dom_qualified_name_local_name(&this.m_qualified_name)

## Ported from Attr.h:33 Web::DOM::Attr::name.
pub func dom_attr_name(this: DomAttr*) -> ak.FlyString:
    return dom_qualified_name_as_string(&this.m_qualified_name)

## Ported from Attr.h:35 Web::DOM::Attr::value.
pub func dom_attr_value(this: DomAttr*) -> ak.String:
    return this.m_value

## Ported from Attr.h:44 Web::DOM::Attr::specified.
# Always returns true: https://dom.spec.whatwg.org/#dom-attr-specified
pub func dom_attr_specified(this: DomAttr*) -> bool:
    return true