dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / named_node_map.lucb
#==============================================================================================
#
# named_node_map - DOM::NamedNodeMap: an element's attribute list
#
# DESCRIPTION:
# Port of LibWeb/DOM/NamedNodeMap.cpp (lines 1-360) and the inline functions of
# NamedNodeMap.h: the JavaScript-facing NamedNodeMap methods and the DOM's internal
# attribute-list algorithms (get by name / by namespace, set, replace, append, remove).
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from NamedNodeMap.cpp:21 Web::DOM::NamedNodeMap::create.
pub func dom_named_node_map_create(element: DomElement*) -> DomNamedNodeMap*:
let realm = bind_platform_object_realm(element.bind_platform_object())
return realm_create_dom_named_node_map(realm, element)
## Ported from NamedNodeMap.cpp:27 Web::DOM::NamedNodeMap::NamedNodeMap.
pub func dom_named_node_map_construct(this: DomNamedNodeMap*, element: DomElement*):
bind_platform_object_construct_realm(this.bind_platform_object(), bind_platform_object_realm(element.bind_platform_object()))
this.m_element = element
this.platform_object.m_legacy_platform_object_flags = BindPlatformObjectLegacyPlatformObjectFlags(supports_indexed_properties = 1, supports_named_properties = 1, has_legacy_unenumerable_named_properties_interface_extended_attribute = 1)
## Ported from NamedNodeMap.cpp:38 Web::DOM::NamedNodeMap::initialize (override).
pub func dom_named_node_map_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.named_node_map)
# Base::initialize(realm): PlatformObject inherits JS::Object's.
js_cell_initialize_impl(base, realm)
## Ported from NamedNodeMap.cpp:44 Web::DOM::NamedNodeMap::visit_edges (override).
pub func dom_named_node_map_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomNamedNodeMap*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_element)
gc.cell_visitor_visit_vector(visitor, &this.m_attributes)
# mark: The JavaScript-facing methods ===========================================================
## supported_property_names's remove_all_matching predicate: `name != name.to_ascii_lowercase()`.
func dom_named_node_map_name_is_not_lowercase(context: void*?, name: const ak.FlyString*) -> bool:
let lowercase = ak.fly_string_to_ascii_lowercase(name)
return not ak.fly_string_eq_fly_string(name, &lowercase)
## Ported from NamedNodeMap.cpp:52 Web::DOM::NamedNodeMap::supported_property_names (override).
# https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
pub func dom_named_node_map_supported_property_names_impl(base: BindPlatformObject*) -> ak.Vector[ak.FlyString]:
let this = (DomNamedNodeMap*)base
# 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
var names: ak.Vector[ak.FlyString]
ak.vector_ensure_capacity(&names, ak.vector_size(&this.m_attributes))
for attribute in ak.vector_span(&this.m_attributes):
let attribute_name = dom_attr_name(attribute)
if not ak.vector_contains_slow[ak.FlyString, ak.FlyStringTraits](&names, attribute_name):
ak.vector_append_t(&names, attribute_name)
# 2. If this NamedNodeMap object’s element is in the HTML namespace and its node document is an HTML document, then for each name of names:
let element = dom_named_node_map_associated_element_const(this)
if dom_node_optional_fly_string_is(dom_element_namespace_uri(element), namespace_html) and dom_document_is_html_document(dom_node_document(element.dom_node())):
# 1. Let lowercaseName be name, in ASCII lowercase.
# 2. If lowercaseName is not equal to name, remove name from names.
let predicate = ak.Function1[const ak.FlyString*, bool](call = dom_named_node_map_name_is_not_lowercase, context = none)
_ = ak.vector_remove_all_matching(&names, predicate)
# 3. Return names.
return names
## Ported from NamedNodeMap.cpp:76 Web::DOM::NamedNodeMap::item.
# https://dom.spec.whatwg.org/#dom-namednodemap-item
pub func dom_named_node_map_item(this: DomNamedNodeMap*, index: u32) -> DomAttr*?:
# 1. If index is equal to or greater than this’s attribute list’s size, then return null.
if (usize)index >= ak.vector_size(&this.m_attributes):
return none
# 2. Otherwise, return this’s attribute list[index].
return *ak.vector_at(&this.m_attributes, (usize)index)
## Ported from NamedNodeMap.cpp:87 Web::DOM::NamedNodeMap::get_named_item.
# https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
pub func dom_named_node_map_get_named_item(this: DomNamedNodeMap*, qualified_name: ak.FlyString) -> DomAttr*?:
return dom_named_node_map_get_attribute_const(this, qualified_name)
## Ported from NamedNodeMap.cpp:93 Web::DOM::NamedNodeMap::get_named_item_ns.
# https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns
pub func dom_named_node_map_get_named_item_ns(this: DomNamedNodeMap*, namespace_: ak.FlyString?, local_name: ak.FlyString) -> DomAttr*?:
return dom_named_node_map_get_attribute_ns_const(this, namespace_, local_name)
## Ported from NamedNodeMap.cpp:99 Web::DOM::NamedNodeMap::set_named_item.
# https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
pub func dom_named_node_map_set_named_item(this: DomNamedNodeMap*, attribute: DomAttr*) -> DomAttr*?!:
return try dom_named_node_map_set_attribute(this, attribute)
## Ported from NamedNodeMap.cpp:105 Web::DOM::NamedNodeMap::set_named_item_ns.
# https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns
pub func dom_named_node_map_set_named_item_ns(this: DomNamedNodeMap*, attribute: DomAttr*) -> DomAttr*?!:
return try dom_named_node_map_set_attribute(this, attribute)
## Ported from NamedNodeMap.cpp:111 Web::DOM::NamedNodeMap::remove_named_item.
# https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
pub func dom_named_node_map_remove_named_item(this: DomNamedNodeMap*, qualified_name: ak.FlyString) -> DomAttr*?!:
# 1. Let attr be the result of removing an attribute given qualifiedName and element.
let attribute = dom_named_node_map_remove_attribute(this, qualified_name)
# 2. If attr is null, then throw a "NotFoundError" DOMException.
if attribute == none:
# Utf16String::formatted("Attribute with name '{}' not found", qualified_name)
var message = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
ak.string_builder_append(&message, "Attribute with name '")
ak.string_builder_append_string_view(&message, ak.fly_string_bytes_as_string_view(&qualified_name))
ak.string_builder_append(&message, "' not found")
try idl_throw(.idl_dom_exception(idl_not_found_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.string_builder_to_utf16_string(&message))))
# 3. Return attr.
return attribute
## Ported from NamedNodeMap.cpp:125 Web::DOM::NamedNodeMap::remove_named_item_ns.
# https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
pub func dom_named_node_map_remove_named_item_ns(this: DomNamedNodeMap*, namespace_: ak.FlyString?, local_name: ak.FlyString) -> DomAttr*?!:
# 1. Let attr be the result of removing an attribute given namespace, localName, and element.
let attribute = dom_named_node_map_remove_attribute_ns(this, namespace_, local_name)
# 2. If attr is null, then throw a "NotFoundError" DOMException.
if attribute == none:
# Utf16String::formatted("Attribute with namespace '{}' and local name '{}' not found", namespace_, local_name);
# AK formats an empty Optional as "None".
var message = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
ak.string_builder_append(&message, "Attribute with namespace '")
if let namespace_value = namespace_:
ak.string_builder_append_string_view(&message, ak.fly_string_bytes_as_string_view(&namespace_value))
else:
ak.string_builder_append(&message, "None")
ak.string_builder_append(&message, "' and local name '")
ak.string_builder_append_string_view(&message, ak.fly_string_bytes_as_string_view(&local_name))
ak.string_builder_append(&message, "' not found")
try idl_throw(.idl_dom_exception(idl_not_found_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.string_builder_to_utf16_string(&message))))
# 3. Return attr.
return attribute
# mark: Getting attributes ======================================================================
## Ported from NamedNodeMap.cpp:139 Web::DOM::NamedNodeMap::get_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
pub func dom_named_node_map_get_attribute(this: DomNamedNodeMap*, qualified_name: ak.FlyString, item_index: usize*? = none) -> DomAttr*?:
return dom_named_node_map_get_attribute_const(this, qualified_name, item_index)
## Ported from NamedNodeMap.cpp:145 Web::DOM::NamedNodeMap::get_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
pub func dom_named_node_map_get_attribute_const(this: DomNamedNodeMap*, qualified_name: ak.FlyString, item_index: usize*? = none) -> DomAttr*?:
if let index = item_index:
*index = 0
# 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
var effective_qualified_name = qualified_name
let element = dom_named_node_map_associated_element_const(this)
if dom_node_optional_fly_string_is(dom_element_namespace_uri(element), namespace_html) and dom_document_is_html_document(dom_node_document(element.dom_node())):
effective_qualified_name = ak.fly_string_to_ascii_lowercase(&qualified_name)
# 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
for attribute in ak.vector_span(&this.m_attributes):
let name = dom_attr_name(attribute)
if ak.fly_string_eq_fly_string(&name, &effective_qualified_name):
return attribute
if let index = item_index:
*index += 1
return none
## Ported from NamedNodeMap.cpp:171 Web::DOM::NamedNodeMap::get_attribute_ns.
# https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
pub func dom_named_node_map_get_attribute_ns(this: DomNamedNodeMap*, namespace_: ak.FlyString?, local_name: ak.FlyString, item_index: usize*? = none) -> DomAttr*?:
return dom_named_node_map_get_attribute_ns_const(this, namespace_, local_name, item_index)
## Ported from NamedNodeMap.cpp:177 Web::DOM::NamedNodeMap::get_attribute_ns.
# https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
pub func dom_named_node_map_get_attribute_ns_const(this: DomNamedNodeMap*, namespace_: ak.FlyString?, local_name: ak.FlyString, item_index: usize*? = none) -> DomAttr*?:
if let index = item_index:
*index = 0
# 1. If namespace is the empty string, then set it to null.
var normalized_namespace: ak.FlyString? = none
if not dom_node_optional_fly_string_is(namespace_, ak.fly_string_new()):
normalized_namespace = namespace_
# 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null.
for attribute in ak.vector_span(&this.m_attributes):
let attribute_local_name = dom_attr_local_name(attribute)
if dom_node_optional_fly_strings_equal(dom_attr_namespace_uri(attribute), normalized_namespace) and ak.fly_string_eq_fly_string(&attribute_local_name, &local_name):
return attribute
if let index = item_index:
*index += 1
return none
# mark: Setting attributes ======================================================================
## Ported from NamedNodeMap.cpp:199 Web::DOM::NamedNodeMap::set_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-set
pub func dom_named_node_map_set_attribute(this: DomNamedNodeMap*, attribute: DomAttr*) -> DomAttr*?!:
# 1. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value
# with attr’s local name, attr’s namespace, element, and attr’s value
var namespace_value: ak.Utf16String? = none
if let namespace_uri = dom_attr_namespace_uri(attribute):
namespace_value = ak.utf16_string_from_utf8_fly_string(&namespace_uri)
let attribute_value = dom_attr_value(attribute)
let verified_value = try trusted_types_get_trusted_types_compliant_attribute_value(dom_attr_local_name(attribute), namespace_value, dom_named_node_map_associated_element(this), .utf16_string(ak.utf16_string_from_utf8_string(&attribute_value)))
# 2. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
if let owner_element = dom_attr_owner_element(attribute):
if owner_element != dom_named_node_map_associated_element(this):
try idl_throw(.idl_dom_exception(idl_in_use_attribute_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Attribute must not already be in use"))))
# 3. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
var old_attribute_index: usize = 0
let old_attribute = dom_named_node_map_get_attribute_ns(this, dom_attr_namespace_uri(attribute), dom_attr_local_name(attribute), &old_attribute_index)
# 4. If oldAttr is attr, return attr.
if old_attribute == attribute:
return attribute
# 5. Set attr’s value to verifiedValue.
try dom_attr_set_value(attribute, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&verified_value.utf16_string_base))
# 6. If oldAttr is non-null, then replace oldAttr with attr.
if let old = old_attribute:
dom_named_node_map_replace_attribute(this, old, attribute, old_attribute_index)
# 7. Otherwise, append attr to element.
else:
dom_named_node_map_append_attribute(this, attribute)
# 8. Return oldAttr.
return old_attribute
## Ported from NamedNodeMap.cpp:238 Web::DOM::NamedNodeMap::replace_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-replace
pub func dom_named_node_map_replace_attribute(this: DomNamedNodeMap*, old_attribute: DomAttr*, new_attribute: DomAttr*, old_attribute_index: usize):
assert(dom_attr_owner_element(old_attribute) != none)
# 1. Let element be oldAttribute’s element.
let element = dom_attr_owner_element(old_attribute) else trap("unreachable")
# 2. Replace oldAttribute by newAttribute in element’s attribute list.
ak.vector_remove(&this.m_attributes, old_attribute_index)
ak.vector_insert(&this.m_attributes, old_attribute_index, new_attribute)
# 3. Set newAttribute’s element to element.
dom_attr_set_owner_element(new_attribute, element)
# 4. Set newAttribute’s node document to element’s node document.
dom_node_set_document_named_node_map(new_attribute.dom_node(), dom_node_document(element.dom_node()))
# 5. Set oldAttribute’s element to null.
dom_attr_set_owner_element(old_attribute, none)
# 6. Handle attribute changes for oldAttribute with element, oldAttribute’s value, and newAttribute’s value.
let new_owner = dom_attr_owner_element(new_attribute) else trap("VERIFY: new attribute's element")
dom_attr_handle_attribute_changes(old_attribute, new_owner, dom_attr_value(old_attribute), dom_attr_value(new_attribute))
## Ported from NamedNodeMap.cpp:263 Web::DOM::NamedNodeMap::append_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-append
pub func dom_named_node_map_append_attribute(this: DomNamedNodeMap*, attribute: DomAttr*):
# 1. Append attribute to element’s attribute list.
ak.vector_append_t(&this.m_attributes, attribute)
# 2. Set attribute’s element to element.
dom_attr_set_owner_element(attribute, dom_named_node_map_associated_element(this))
# 3. Set attribute’s node document to element’s node document.
dom_node_set_document_named_node_map(attribute.dom_node(), dom_node_document(dom_named_node_map_associated_element(this).dom_node()))
# 4. Handle attribute changes for attribute with element, null, and attribute’s value.
dom_attr_handle_attribute_changes(attribute, dom_named_node_map_associated_element(this), none, dom_attr_value(attribute))
# mark: Removing attributes =====================================================================
## Ported from NamedNodeMap.cpp:279 Web::DOM::NamedNodeMap::remove_attribute_at_index.
# https://dom.spec.whatwg.org/#concept-element-attributes-remove
func dom_named_node_map_remove_attribute_at_index(this: DomNamedNodeMap*, attribute_index: usize):
let attribute = *ak.vector_at(&this.m_attributes, attribute_index)
# 1. Let element be attribute’s element.
let element = dom_attr_owner_element(attribute) else trap("VERIFY: attribute's element")
# 2. Remove attribute from element’s attribute list.
ak.vector_remove(&this.m_attributes, attribute_index)
# 3. Set attribute’s element to null.
dom_attr_set_owner_element(attribute, none)
# 4. Handle attribute changes for attribute with element, attribute’s value, and null.
dom_attr_handle_attribute_changes(attribute, element, dom_attr_value(attribute), none)
## Ported from NamedNodeMap.cpp:298 Web::DOM::NamedNodeMap::remove_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
pub func dom_named_node_map_remove_attribute(this: DomNamedNodeMap*, qualified_name: ak.FlyString) -> DomAttr*?:
var item_index: usize = 0
# 1. Let attr be the result of getting an attribute given qualifiedName and element.
let attribute = dom_named_node_map_get_attribute(this, qualified_name, &item_index)
# 2. If attr is non-null, then remove attr.
if attribute != none:
dom_named_node_map_remove_attribute_at_index(this, item_index)
# 3. Return attr.
return attribute
## Ported from NamedNodeMap.cpp:314 Web::DOM::NamedNodeMap::remove_attribute_ns.
# https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
pub func dom_named_node_map_remove_attribute_ns(this: DomNamedNodeMap*, namespace_: ak.FlyString?, local_name: ak.FlyString) -> DomAttr*?:
var item_index: usize = 0
# 1. Let attr be the result of getting an attribute given namespace, localName, and element.
let attribute = dom_named_node_map_get_attribute_ns(this, namespace_, local_name, &item_index)
# 2. If attr is non-null, then remove attr.
if attribute != none:
dom_named_node_map_remove_attribute_at_index(this, item_index)
# 3. Return attr.
return attribute
# mark: Legacy platform object properties =======================================================
## Ported from NamedNodeMap.cpp:329 Web::DOM::NamedNodeMap::item_value (override).
pub func dom_named_node_map_item_value_impl(base: BindPlatformObject*, index: usize) -> JsValue?:
let this = (DomNamedNodeMap*)base
let node = dom_named_node_map_item(this, (u32)index) else return none
return bind_platform_object_wrapper(node.bind_platform_object())
## Ported from NamedNodeMap.cpp:337 Web::DOM::NamedNodeMap::named_item_value (override).
pub func dom_named_node_map_named_item_value_impl(base: BindPlatformObject*, name: ak.FlyString) -> JsValue:
let this = (DomNamedNodeMap*)base
let node = dom_named_node_map_get_named_item(this, name) else return js_undefined()
return bind_platform_object_wrapper(node.bind_platform_object())
## Ported from NamedNodeMap.cpp:346 Web::DOM::NamedNodeMap::remove_attribute_node.
# https://dom.spec.whatwg.org/#dom-element-removeattributenode
pub func dom_named_node_map_remove_attribute_node(this: DomNamedNodeMap*, attr: DomAttr*) -> DomAttr*!:
# 1. If this’s attribute list does not contain attr, then throw a "NotFoundError" DOMException.
let index = ak.vector_find_first_index(&this.m_attributes, attr)
if index == none:
try idl_throw(.idl_dom_exception(idl_not_found_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Attribute not found"))))
# 2. Remove attr.
dom_named_node_map_remove_attribute_at_index(this, index else trap("unreachable"))
# 3. Return attr.
return attr
# mark: NamedNodeMap.h ==========================================================================
## Ported from NamedNodeMap.h:32 Web::DOM::NamedNodeMap::length.
pub func dom_named_node_map_length(this: DomNamedNodeMap*) -> usize:
return ak.vector_size(&this.m_attributes)
## Ported from NamedNodeMap.h:33 Web::DOM::NamedNodeMap::is_empty.
pub func dom_named_node_map_is_empty(this: DomNamedNodeMap*) -> bool:
return ak.vector_is_empty(&this.m_attributes)
## Ported from NamedNodeMap.h:65 Web::DOM::NamedNodeMap::associated_element.
func dom_named_node_map_associated_element(this: DomNamedNodeMap*) -> DomElement*:
return this.m_element
## Ported from NamedNodeMap.h:66 Web::DOM::NamedNodeMap::associated_element.
func dom_named_node_map_associated_element_const(this: DomNamedNodeMap*) -> DomElement*:
return this.m_element