dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / dom_token_list.lucb
#==============================================================================================
#
# dom_token_list - DOM::DOMTokenList: an element attribute as an ordered set of tokens
#
# DESCRIPTION:
# Port of LibWeb/DOM/DOMTokenList.cpp (lines 1-348) and the inline functions of
# DOMTokenList.h: the ordered-set helpers of Infra, add/remove/toggle/replace, the
# supported-token validation steps and the update steps that write the attribute back.
#
# `run_validation_steps`'s function-local `static HashMap<SupportedTokenKey,
# Vector<StringView>>` is a constant table here (dom_token_list_supported_tokens): a
# lookup by (element local name, attribute name) answers the same token lists.
#
#==============================================================================================
# mark: Ordered sets ============================================================================
## Ported from DOMTokenList.cpp:20 (anonymous)::append_to_ordered_set.
# https://infra.spec.whatwg.org/#set-append
func append_to_ordered_set(set: ak.Vector[ak.String]*, item: ak.String):
if not ak.vector_contains_slow[ak.String, ak.StringTraits](set, item):
ak.vector_append_t(set, item)
## remove_from_ordered_set's predicate: `value == item`.
func remove_from_ordered_set_matches(context: void*?, value: const ak.String*) -> bool:
let item = (ak.StringView*)(context else trap("no item"))
return ak.string_eq_string_view(value, *item)
## Ported from DOMTokenList.cpp:27 (anonymous)::remove_from_ordered_set.
# https://infra.spec.whatwg.org/#list-remove
func remove_from_ordered_set(set: ak.Vector[ak.String]*, item: ak.StringView):
var item_view = item
let predicate = ak.Function1[const ak.String*, bool](call = remove_from_ordered_set_matches, context = &item_view)
_ = ak.vector_remove_first_matching(set, predicate)
## `Vector<String>::find_first_index(value)`.
func dom_token_list_find_first_index(set: const ak.Vector[ak.String]*, value: ak.String) -> usize?:
for (index, candidate) in ak.vector_span_const(set).indexed():
if ak.string_equals(&candidate, &value):
return index
return none
## Ported from DOMTokenList.cpp:33 (anonymous)::replace_in_ordered_set.
# https://infra.spec.whatwg.org/#set-replace
func replace_in_ordered_set(set: ak.Vector[ak.String]*, item: ak.String, replacement: ak.String):
let item_index = dom_token_list_find_first_index(set, item) else trap("VERIFY: item_index.has_value()")
let found_replacement_index = dom_token_list_find_first_index(set, replacement)
if found_replacement_index == none:
*ak.vector_at(set, item_index) = replacement
return
let replacement_index = found_replacement_index else trap("unreachable")
let index_to_set = item_index if item_index < replacement_index else replacement_index
let index_to_remove = item_index if item_index > replacement_index else replacement_index
if index_to_set == index_to_remove:
return
*ak.vector_at(set, index_to_set) = replacement
ak.vector_remove(set, index_to_remove)
# mark: Construction ============================================================================
## Ported from DOMTokenList.cpp:59 Web::DOM::DOMTokenList::create.
pub func dom_token_list_create(associated_element: DomElement*, associated_attribute: ak.FlyString) -> DomTokenList*:
let realm = bind_platform_object_realm(associated_element.bind_platform_object())
return realm_create_dom_token_list(realm, associated_element, associated_attribute)
## Ported from DOMTokenList.cpp:66 Web::DOM::DOMTokenList::DOMTokenList.
# https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A3
pub func dom_token_list_construct(this: DomTokenList*, associated_element: DomElement*, associated_attribute: ak.FlyString):
bind_platform_object_construct_realm(this.bind_platform_object(), bind_platform_object_realm(associated_element.bind_platform_object()))
this.m_associated_element = associated_element
this.m_associated_attribute = associated_attribute
this.platform_object.m_legacy_platform_object_flags = BindPlatformObjectLegacyPlatformObjectFlags(supports_indexed_properties = 1)
# When a DOMTokenList object set is created:
# 1. Let element be set’s element.
# 2. Let attributeName be set’s attribute name.
# 3. Let value be the result of getting an attribute value given element and attributeName.
let value = dom_element_get_attribute_value(this.m_associated_element, this.m_associated_attribute)
# 4. Run the attribute change steps for element, attributeName, value, value, and null.
dom_token_list_associated_attribute_changed(this, ak.string_bytes_as_string_view(&value))
## Ported from DOMTokenList.cpp:83 Web::DOM::DOMTokenList::initialize (override).
pub func dom_token_list_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.dom_token_list)
# Base::initialize(realm): PlatformObject inherits JS::Object's.
js_cell_initialize_impl(base, realm)
## Ported from DOMTokenList.cpp:89 Web::DOM::DOMTokenList::visit_edges (override).
pub func dom_token_list_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomTokenList*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_associated_element)
## Ported from DOMTokenList.cpp:96 Web::DOM::DOMTokenList::associated_attribute_changed.
# https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A1
pub func dom_token_list_associated_attribute_changed(this: DomTokenList*, value: ak.StringView):
# 1. If localName is set’s attribute name, namespace is null, and value is null, then empty token set.
# 2. Otherwise, if localName is set’s attribute name and namespace is null, then set set’s token set to value,
# parsed.
# AD-HOC: The caller is responsible for checking the name and namespace.
ak.vector_clear(&this.m_token_set)
if ak.string_view_is_empty(&value):
return
this.m_token_set = dom_token_list_parse_ordered_set(this, value)
# mark: The token list ==========================================================================
## Ported from DOMTokenList.cpp:109 Web::DOM::DOMTokenList::item.
# https://dom.spec.whatwg.org/#dom-domtokenlist-item
pub func dom_token_list_item(this: DomTokenList*, index: usize) -> ak.String?:
# 1. If index is equal to or greater than this’s token set’s size, then return null.
if index >= ak.vector_size(&this.m_token_set):
return none
# 2. Return this’s token set[index].
return *ak.vector_at(&this.m_token_set, index)
## Ported from DOMTokenList.cpp:120 Web::DOM::DOMTokenList::contains.
# https://dom.spec.whatwg.org/#dom-domtokenlist-contains
pub func dom_token_list_contains(this: DomTokenList*, token: ak.String) -> bool:
return ak.vector_contains_slow[ak.String, ak.StringTraits](&this.m_token_set, token)
## Ported from DOMTokenList.cpp:126 Web::DOM::DOMTokenList::add.
# https://dom.spec.whatwg.org/#dom-domtokenlist-add
pub func dom_token_list_add(this: DomTokenList*, tokens: ak.Vector[ak.String]) -> !:
# 1. For each token of tokens:
for token in ak.vector_span_const(&tokens):
# a. If token is the empty string, then throw a "SyntaxError" DOMException.
# b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
try dom_token_list_validate_token(this, ak.string_bytes_as_string_view(&token))
# 2. For each token of tokens, append token to this’s token set.
append_to_ordered_set(&this.m_token_set, token)
# 3. Run the update steps.
dom_token_list_run_update_steps(this)
## Ported from DOMTokenList.cpp:144 Web::DOM::DOMTokenList::remove.
# https://dom.spec.whatwg.org/#dom-domtokenlist-remove
pub func dom_token_list_remove(this: DomTokenList*, tokens: ak.Vector[ak.String]) -> !:
# 1. For each token of tokens:
for token in ak.vector_span_const(&tokens):
# a. If token is the empty string, then throw a "SyntaxError" DOMException.
# b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
try dom_token_list_validate_token(this, ak.string_bytes_as_string_view(&token))
# 2. For each token of tokens, remove token from this’s token set.
remove_from_ordered_set(&this.m_token_set, ak.string_bytes_as_string_view(&token))
# 3. Run the update steps.
dom_token_list_run_update_steps(this)
## Ported from DOMTokenList.cpp:162 Web::DOM::DOMTokenList::toggle.
# https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
pub func dom_token_list_toggle(this: DomTokenList*, token: ak.String, force: bool?) -> bool!:
# 1. If token is the empty string, then throw a "SyntaxError" DOMException.
# 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
try dom_token_list_validate_token(this, ak.string_bytes_as_string_view(&token))
# 3. If this’s token set[token] exists, then:
if dom_token_list_contains(this, token):
# a. If force is either not given or is false, then remove token from this’s token set, run the update steps and return false.
if force == none or force == false:
remove_from_ordered_set(&this.m_token_set, ak.string_bytes_as_string_view(&token))
dom_token_list_run_update_steps(this)
return false
# b. Return true.
return true
# 4. Otherwise, if force not given or is true, append token to this’s token set, run the update steps, and return true.
if force == none or force == true:
append_to_ordered_set(&this.m_token_set, token)
dom_token_list_run_update_steps(this)
return true
# 5. Return false.
return false
## Ported from DOMTokenList.cpp:193 Web::DOM::DOMTokenList::replace.
# https://dom.spec.whatwg.org/#dom-domtokenlist-replace
pub func dom_token_list_replace(this: DomTokenList*, token: ak.String, new_token: ak.String) -> bool!:
# 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException.
try dom_token_list_validate_token_not_empty(this, ak.string_bytes_as_string_view(&token))
try dom_token_list_validate_token_not_empty(this, ak.string_bytes_as_string_view(&new_token))
# 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
try dom_token_list_validate_token_not_whitespace(this, ak.string_bytes_as_string_view(&token))
try dom_token_list_validate_token_not_whitespace(this, ak.string_bytes_as_string_view(&new_token))
# 3. If this’s token set does not contain token, then return false.
if not dom_token_list_contains(this, token):
return false
# 4. Replace token in this’s token set with newToken.
replace_in_ordered_set(&this.m_token_set, token, new_token)
# 5. Run the update steps.
dom_token_list_run_update_steps(this)
# 6. Return true.
return true
## Ported from DOMTokenList.cpp:218 Web::DOM::DOMTokenList::supports.
# https://dom.spec.whatwg.org/#dom-domtokenlist-supports
pub func dom_token_list_supports(this: DomTokenList*, token: ak.StringView) -> bool!:
# 1. Let result be the return value of validation steps called with token.
let result = try dom_token_list_run_validation_steps(this, token)
# 2. Return result.
return result
# mark: Validation ==============================================================================
# The supported tokens of run_validation_steps' static map, entry by entry.
# https://html.spec.whatwg.org/multipage/links.html#linkTypes
let dom_token_list_link_rel_tokens: str[14] = ["modulepreload", "preload", "preconnect", "dns-prefetch", "stylesheet", "icon", "alternate", "prefetch", "prerender", "next", "manifest", "apple-touch-icon", "apple-touch-icon-precomposed", "canonical"]
let dom_token_list_hyperlink_rel_tokens: str[3] = ["noreferrer", "noopener", "opener"]
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-sandbox
let dom_token_list_iframe_sandbox_tokens: str[13] = ["allow-downloads", "allow-forms", "allow-modals", "allow-orientation-lock", "allow-pointer-lock", "allow-popups", "allow-popups-to-escape-sandbox", "allow-presentation", "allow-same-origin", "allow-scripts", "allow-top-navigation", "allow-top-navigation-by-user-activation", "allow-top-navigation-to-custom-protocols"]
## `supported_tokens_map.get({ element_name, attribute_name })`: the supported tokens of an
## element's attribute, keyed by { HTML::TagNames::x, HTML::AttributeNames::y }.
func dom_token_list_supported_tokens(element_name: ak.FlyString, attribute_name: ak.FlyString) -> const str[]?:
if ak.fly_string_eq_string_view(&attribute_name, ak.sv("rel")):
if ak.fly_string_eq_string_view(&element_name, ak.sv("link")):
return dom_token_list_link_rel_tokens
if ak.fly_string_eq_string_view(&element_name, ak.sv("a")) or ak.fly_string_eq_string_view(&element_name, ak.sv("area")) or ak.fly_string_eq_string_view(&element_name, ak.sv("form")):
return dom_token_list_hyperlink_rel_tokens
if ak.fly_string_eq_string_view(&element_name, ak.sv("iframe")) and ak.fly_string_eq_string_view(&attribute_name, ak.sv("sandbox")):
return dom_token_list_iframe_sandbox_tokens
return none
## Ported from DOMTokenList.cpp:228 Web::DOM::DOMTokenList::run_validation_steps.
# https://dom.spec.whatwg.org/#concept-domtokenlist-validation
func dom_token_list_run_validation_steps(this: DomTokenList*, token: ak.StringView) -> bool!:
# 1. If set’s element and attribute name does not define supported tokens, then throw a TypeError.
let found_supported_tokens = dom_token_list_supported_tokens(dom_element_local_name(this.m_associated_element), this.m_associated_attribute)
if found_supported_tokens == none:
var message = ak.string_builder_new()
ak.string_builder_append(&message, "Attribute ")
ak.string_builder_append_string_view(&message, ak.fly_string_bytes_as_string_view(&this.m_associated_attribute))
ak.string_builder_append(&message, " does not define any supported tokens")
let message_string = ak.string_builder_to_string(&message) else trap("MUST: String::formatted")
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string(message_string))
let supported_tokens = found_supported_tokens else trap("unreachable")
# 2. Let lowercaseToken be token, in ASCII lowercase.
let lowercase_token = ak.string_view_to_ascii_lowercase_string(&token)
# 3. If lowercaseToken is present in the supported tokens of set’s element and attribute name, then return true.
for supported_token in supported_tokens:
if ak.string_eq_string_view(&lowercase_token, ak.sv(supported_token)):
return true
# 4. Return false.
return false
## Ported from DOMTokenList.cpp:263 Web::DOM::DOMTokenList::parse_ordered_set.
# https://dom.spec.whatwg.org/#concept-ordered-set-parser
func dom_token_list_parse_ordered_set(this: DomTokenList*, input: ak.StringView) -> ak.Vector[ak.String]:
# 1. Let inputTokens be the result of splitting input on ASCII whitespace.
let predicate = DomElementIsAsciiWhitespace(unused = 0)
let split_values = ak.string_view_split_view_if(&input, &predicate)
# 2. Let tokens be a new ordered set.
var tokens: ak.Vector[ak.String]
# 3. For each token of inputTokens: append token to tokens.
for split_value in ak.vector_span_const(&split_values):
append_to_ordered_set(&tokens, ak.string_from_utf8(split_value) else trap("MUST: String::from_utf8"))
# 4. Return tokens.
return tokens
## Ported from DOMTokenList.cpp:280 Web::DOM::DOMTokenList::serialize_ordered_set.
# https://dom.spec.whatwg.org/#concept-ordered-set-serializer
func dom_token_list_serialize_ordered_set(this: DomTokenList*) -> ak.String:
# The ordered set serializer takes a set and returns the concatenation of set using U+0020 SPACE.
return ak.string_join_string(ak.sv(" "), ak.vector_span_const(&this.m_token_set)) else trap("MUST: String::join")
## Ported from DOMTokenList.cpp:287 Web::DOM::DOMTokenList::value.
# https://dom.spec.whatwg.org/#dom-domtokenlist-value
pub func dom_token_list_value(this: DomTokenList*) -> ak.String:
return dom_element_get_attribute_value(this.m_associated_element, this.m_associated_attribute)
## Ported from DOMTokenList.cpp:293 Web::DOM::DOMTokenList::set_value.
# https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
pub func dom_token_list_set_value(this: DomTokenList*, value: ak.String):
# (C++ tests the GC::Ref for null, which it never is.)
let associated_element = this.m_associated_element
dom_element_set_attribute_value(associated_element, this.m_associated_attribute, value)
## Ported from DOMTokenList.cpp:302 Web::DOM::DOMTokenList::validate_token.
func dom_token_list_validate_token(this: DomTokenList*, token: ak.StringView) -> !:
try dom_token_list_validate_token_not_empty(this, token)
try dom_token_list_validate_token_not_whitespace(this, token)
## Ported from DOMTokenList.cpp:309 Web::DOM::DOMTokenList::validate_token_not_empty.
func dom_token_list_validate_token_not_empty(this: DomTokenList*, token: ak.StringView) -> !:
if ak.string_view_is_empty(&token):
try idl_throw(.idl_dom_exception(idl_syntax_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Non-empty DOM tokens are not allowed"))))
## Ported from DOMTokenList.cpp:316 Web::DOM::DOMTokenList::validate_token_not_whitespace.
func dom_token_list_validate_token_not_whitespace(this: DomTokenList*, token: ak.StringView) -> !:
# any_of(token, Infra::is_ascii_whitespace)
for byte in ak.string_view_bytes(&token):
if web_infra.is_ascii_whitespace((u32)byte):
try idl_throw(.idl_dom_exception(idl_invalid_character_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("DOM tokens containing ASCII whitespace are not allowed"))))
## Ported from DOMTokenList.cpp:324 Web::DOM::DOMTokenList::run_update_steps.
# https://dom.spec.whatwg.org/#concept-dtl-update
func dom_token_list_run_update_steps(this: DomTokenList*):
# (C++ tests the GC::Ref for null, which it never is.)
let associated_element = this.m_associated_element
# 1. If get an attribute by namespace and local name given null, set’s attribute name, and set’s element returns null and set’s token set is empty, then return.
let attribute = dom_element_get_attribute_ns(associated_element, none, this.m_associated_attribute)
if attribute == none and ak.vector_is_empty(&this.m_token_set):
return
# 2. Set an attribute value given set’s element, set’s attribute name, and the result of running the ordered set
# serializer for set’s token set.
dom_element_set_attribute_value(associated_element, this.m_associated_attribute, dom_token_list_serialize_ordered_set(this))
## Ported from DOMTokenList.cpp:340 Web::DOM::DOMTokenList::item_value (override).
pub func dom_token_list_item_value_impl(base: BindPlatformObject*, index: usize) -> JsValue?:
let this = (DomTokenList*)base
let string = dom_token_list_item(this, index) else return none
return js_primitive_string_create_string(js_cell_vm(this.js_cell()), string)
# mark: DOMTokenList.h ==========================================================================
## Ported from DOMTokenList.h:35 Web::DOM::DOMTokenList::length.
pub func dom_token_list_length(this: DomTokenList*) -> usize:
return ak.vector_size(&this.m_token_set)