dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / dom_string_map.lucb
#==============================================================================================
#
# dom_string_map - HTML::DOMStringMap: an element's dataset
#
# DESCRIPTION:
# Port of LibWeb/HTML/DOMStringMap.cpp (lines 1-214): creation, the name-value pairs of the
# associated element's data-* attributes, the supported property names, and the named
# property getter, setter and deleter. The setter converts its JS value with
# Value::to_string, which is phase 3 (luce-js).
#
#==============================================================================================
# mark: Creation ================================================================================
## Ported from DOMStringMap.cpp:18 Web::HTML::DOMStringMap::create.
pub func html_dom_string_map_create(element: DomElement*) -> HtmlDomStringMap*:
let realm = dom_element_realm(element)
return realm_create_html_dom_string_map(realm, element)
## Ported from DOMStringMap.cpp:24 Web::HTML::DOMStringMap::DOMStringMap.
pub func html_dom_string_map_construct(this: HtmlDomStringMap*, element: DomElement*):
bind_platform_object_construct_realm(this.bind_platform_object(), dom_element_realm(element))
this.m_associated_element = element
this.platform_object.m_legacy_platform_object_flags = BindPlatformObjectLegacyPlatformObjectFlags(
supports_named_properties = 1,
has_named_property_setter = 1,
has_named_property_deleter = 1,
has_legacy_override_built_ins_interface_extended_attribute = 1,
)
## Ported from DOMStringMap.cpp:38 Web::HTML::DOMStringMap::initialize (override).
pub func html_dom_string_map_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.dom_string_map)
# Base::initialize(realm): PlatformObject inherits JS::Object's.
js_cell_initialize_impl(base, realm)
## Ported from DOMStringMap.cpp:44 Web::HTML::DOMStringMap::visit_edges (override).
pub func html_dom_string_map_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlDomStringMap*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t[DomElement](visitor, this.m_associated_element)
# mark: Name-value pairs ========================================================================
## The `[&](auto& name, auto& value) {...}` of get_name_value_pairs: its list.
struct HtmlDomStringMapGetNameValuePairsAttribute:
var list: ak.Vector[HtmlDomStringMapNameValuePair]*
static func call(context: void*?, name: const ak.FlyString*, value: const ak.String*):
let captures = (HtmlDomStringMapGetNameValuePairsAttribute*)(context else trap("for_each_attribute: no captures"))
let name_view = ak.fly_string_bytes_as_string_view(name)
if not ak.string_view_starts_with_string_view(&name_view, ak.sv("data-")):
return
let name_after_starting_data = ak.string_view_substring_view(&name_view, 5)
for character in ak.string_view_bytes(&name_after_starting_data):
if ak.is_ascii_upper_alpha(character):
return
# 3. For each name in list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by an ASCII lower alpha, remove the U+002D HYPHEN-MINUS character (-) and replace the character
# that followed it by the same character converted to ASCII uppercase.
var builder = ak.string_builder_new()
var character_index: usize = 0
while character_index < ak.string_view_length(&name_after_starting_data):
let current_character = ak.string_view_at(&name_after_starting_data, character_index)
if character_index + 1 < ak.string_view_length(&name_after_starting_data) and current_character == '-':
let next_character = ak.string_view_at(&name_after_starting_data, character_index + 1)
if ak.is_ascii_lower_alpha(next_character):
ak.string_builder_append_u8(&builder, (u8)ak.to_ascii_uppercase(next_character))
# Skip the next character
character_index += 2
continue
ak.string_builder_append_u8(&builder, current_character)
character_index += 1
let pair_name = ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
ak.vector_append_t(captures.list, HtmlDomStringMapNameValuePair(name = ak.fly_string_from_string(pair_name), value = *value))
## Ported from DOMStringMap.cpp:51 Web::HTML::DOMStringMap::get_name_value_pairs.
# https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
pub func html_dom_string_map_get_name_value_pairs(this: HtmlDomStringMap*) -> ak.Vector[HtmlDomStringMapNameValuePair]:
# 1. Let list be an empty list of name-value pairs.
var list: ak.Vector[HtmlDomStringMapNameValuePair]
# 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas,
# in the order that those attributes are listed in the element's attribute list, add a name-value pair to list whose name is the attribute's name with the first five characters removed and whose value
# is the attribute's value.
var captures = HtmlDomStringMapGetNameValuePairsAttribute(list = &list)
dom_element_for_each_attribute_function2(this.m_associated_element, ak.Function2[const ak.FlyString*, const ak.String*, unit](call = HtmlDomStringMapGetNameValuePairsAttribute.call, context = &captures))
# 4. Return list.
return list
## Ported from DOMStringMap.cpp:101 Web::HTML::DOMStringMap::supported_property_names (override).
# https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
# NOTE: There isn't a direct link to this, so the link is to one of the algorithms above it.
pub func html_dom_string_map_supported_property_names_impl(base: BindPlatformObject*) -> ak.Vector[ak.FlyString]:
let this = (HtmlDomStringMap*)base
# The supported property names on a DOMStringMap object at any instant are the names of each pair returned from getting the DOMStringMap's name-value pairs at that instant, in the order returned.
var names: ak.Vector[ak.FlyString]
var name_value_pairs = html_dom_string_map_get_name_value_pairs(this)
for name_value_pair in ak.vector_span(&name_value_pairs):
ak.vector_append_t(&names, name_value_pair.name)
return names
## Ported from DOMStringMap.cpp:113 Web::HTML::DOMStringMap::determine_value_of_named_property.
# https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-nameditem
pub func html_dom_string_map_determine_value_of_named_property(this: HtmlDomStringMap*, name: ak.FlyString) -> ak.String:
# To determine the value of a named property name for a DOMStringMap, return the value component of the name-value pair whose name component is name in the list returned from getting the
# DOMStringMap's name-value pairs.
var name_value_pairs = html_dom_string_map_get_name_value_pairs(this)
var optional_value: HtmlDomStringMapNameValuePair? = none
for name_value_pair in ak.vector_span(&name_value_pairs):
if ak.fly_string_eq_fly_string(&name_value_pair.name, &name):
optional_value = name_value_pair
break
# NOTE: determine_value_of_named_property is only called if `name` is in supported_property_names.
let found = optional_value else trap("VERIFY: optional_value.has_value()")
return found.value
# mark: Named property setter and deleter =======================================================
## Ported from DOMStringMap.cpp:129 Web::HTML::DOMStringMap::set_value_of_new_named_property (override).
# https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
pub func html_dom_string_map_set_value_of_new_named_property_impl(base: BindPlatformObject*, name: ak.String, unconverted_value: JsValue) -> !:
let this = (HtmlDomStringMap*)base
let realm = bind_platform_object_realm(base)
# NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
# The type of `value` is `DOMString`.
let value = try js_value_to_string(unconverted_value, js_cell_vm(this.js_cell()))
var builder = ak.string_builder_new()
# 3. Insert the string data- at the front of name.
# NOTE: This is done out of order because StringBuilder doesn't have prepend.
ak.string_builder_append(&builder, "data-")
let name_view = ak.string_bytes_as_string_view(&name)
for character_index in 0..<ak.string_view_length(&name_view):
# 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
let current_character = ak.string_view_at(&name_view, character_index)
if current_character == '-' and character_index + 1 < ak.string_view_length(&name_view):
let next_character = ak.string_view_at(&name_view, character_index + 1)
if ak.is_ascii_lower_alpha(next_character):
try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_str("Name cannot contain a '-' followed by a lowercase character."))))
# 2. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
if ak.is_ascii_upper_alpha(current_character):
ak.string_builder_append_u8(&builder, '-')
ak.string_builder_append_u8(&builder, (u8)ak.to_ascii_lowercase(current_character))
continue
ak.string_builder_append_u8(&builder, current_character)
let data_name = ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
# 4. If name is not a valid attribute local name, then throw an "InvalidCharacterError" DOMException.
if not dom_is_valid_attribute_local_name(ak.fly_string_from_string(data_name)):
try idl_throw(.idl_dom_exception(idl_invalid_character_error_create(realm, ak.utf16_string_from_str("Name is not a valid attribute local name."))))
# 5. Set an attribute value for the DOMStringMap's associated element using name and value.
dom_element_set_attribute_value(this.m_associated_element, ak.fly_string_from_string(data_name), value)
## Ported from DOMStringMap.cpp:176 Web::HTML::DOMStringMap::set_value_of_existing_named_property (override).
# https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
pub func html_dom_string_map_set_value_of_existing_named_property_impl(base: BindPlatformObject*, name: ak.String, value: JsValue) -> !:
try html_dom_string_map_set_value_of_new_named_property_impl(base, name, value)
## Ported from DOMStringMap.cpp:182 Web::HTML::DOMStringMap::delete_value (override).
# https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
pub func html_dom_string_map_delete_value_impl(base: BindPlatformObject*, name: ak.String) -> BindPlatformObjectDidDeletionFail!:
let this = (HtmlDomStringMap*)base
var builder = ak.string_builder_new()
# 2. Insert the string data- at the front of name.
# NOTE: This is done out of order because StringBuilder doesn't have prepend.
ak.string_builder_append(&builder, "data-")
let name_view = ak.string_bytes_as_string_view(&name)
for character in ak.string_view_bytes(&name_view):
# 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
if ak.is_ascii_upper_alpha(character):
ak.string_builder_append_u8(&builder, '-')
ak.string_builder_append_u8(&builder, (u8)ak.to_ascii_lowercase(character))
continue
ak.string_builder_append_u8(&builder, character)
# Remove an attribute by name given name and the DOMStringMap's associated element.
let data_name = ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
dom_element_remove_attribute(this.m_associated_element, ak.fly_string_from_string(data_name))
# The spec doesn't have the step. This indicates that the deletion was successful.
return BindPlatformObjectDidDeletionFail.no
## Ported from DOMStringMap.cpp:209 Web::HTML::DOMStringMap::named_item_value (override).
pub func html_dom_string_map_named_item_value_impl(base: BindPlatformObject*, name: ak.FlyString) -> JsValue:
let this = (HtmlDomStringMap*)base
return js_primitive_string_create_string(js_cell_vm(this.js_cell()), html_dom_string_map_determine_value_of_named_property(this, name))