dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_options_collection.lucb
#==============================================================================================
#
# html_options_collection - HTML::HTMLOptionsCollection: the options of a <select>
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLOptionsCollection.cpp (lines 1-193): an HTMLCollection rooted at
# a select element with an indexed property setter, its length setter, add(), remove()
# and selectedIndex.
#
# Copyright (c) 2022, the SerenityOS developers.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from HTMLOptionsCollection.cpp:21 Web::HTML::HTMLOptionsCollection::create.
pub func html_options_collection_create(root: DomParentNode*, filter: ak.Function1[DomElement*, bool]) -> HtmlOptionsCollection*:
return realm_create_html_options_collection(bind_platform_object_realm(root.bind_platform_object()), root, filter)
## Ported from HTMLOptionsCollection.cpp:26 Web::HTML::HTMLOptionsCollection::HTMLOptionsCollection.
pub func html_options_collection_construct(this: HtmlOptionsCollection*, root: DomParentNode*, filter: ak.Function1[DomElement*, bool]):
dom_html_collection_construct(this.dom_html_collection(), root, DomHtmlCollectionScope.descendants, filter, ak.Function2[DomElement*, DomElement*, bool]())
var flags = this.bind_platform_object().m_legacy_platform_object_flags else trap("VERIFY: the legacy platform object flags")
flags.has_indexed_property_setter = 1
flags.indexed_property_setter_has_identifier = 1
this.bind_platform_object().m_legacy_platform_object_flags = flags
## Ported from HTMLOptionsCollection.cpp:35 Web::HTML::HTMLOptionsCollection::initialize (override).
pub func html_options_collection_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_options_collection)
dom_html_collection_initialize_impl(base, realm)
## `root()` of the collection as a node.
func html_options_collection_root(this: HtmlOptionsCollection*) -> DomNode*:
return (DomNode*)dom_html_collection_root(this.dom_html_collection())
## Ported from HTMLOptionsCollection.cpp:42 Web::HTML::HTMLOptionsCollection::set_length.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-length
pub func html_options_collection_set_length(this: HtmlOptionsCollection*, value: u32) -> !:
# 1. Let current be the number of nodes represented by the collection.
let current = (u32)dom_html_collection_length(this.dom_html_collection())
# 2. If the given value is greater than current, then:
if value > current:
# 2.1. If the given value is greater than 100,000, then return.
if value > 100000:
return
# 2.2. Let n be value − current.
let n = value - current
# 2.3. Append n new option elements with no attributes and no child nodes to the select element on which this is rooted.
# Mutation events must be fired as if a DocumentFragment containing the new option elements had been inserted.
let root_element = html_options_collection_root(this)
for i: u32 in 0..<n:
_ = i
let option = try dom_create_element(dom_node_document(root_element), html_tag_names_option, namespace_html)
_ = try dom_node_append_child(root_element, option.dom_node())
# 3. If the given value is less than current, then:
if value < current:
# 3.1. Let n be current − value.
let n = current - value
# 3.2. Remove the last n nodes in the collection from their parent nodes.
for i: u32 in 0..<n:
_ = i
let element = dom_html_collection_item(this.dom_html_collection(), dom_html_collection_length(this.dom_html_collection()) - 1) else trap("VERIFY: item")
dom_node_remove(element.dom_node())
## Ported from HTMLOptionsCollection.cpp:77 Web::HTML::HTMLOptionsCollection::set_value_of_indexed_property (override).
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-setter
pub func html_options_collection_set_value_of_indexed_property_impl(base: BindPlatformObject*, index: u32, unconverted_option: JsValue) -> !:
let this = (HtmlOptionsCollection*)base
# The spec doesn't seem to require this, but it's consistent with length handling and other browsers
if index >= 100000:
return
# 1. If value is null, invoke the steps for the remove method with index as the argument, and return.
if js_value_is_null(unconverted_option):
html_options_collection_remove(this, (i32)index)
return
# `unconverted_option.as_if<DOM::Element>()`.
var option: DomElement*? = none
if js_value_is_object(unconverted_option):
option = as_if_dom_element((gc.Cell*)js_value_as_object(unconverted_option))
if option == none:
try idl_throw(.idl_dom_exception(idl_type_mismatch_error_create(bind_platform_object_realm(base), ak.utf16_string_from_str("The value provided is not an HTMLOptionElement"))))
let option_element = option else trap("unreachable")
# 2. Let length be the number of nodes represented by the collection.
let length = dom_html_collection_length(this.dom_html_collection())
let root_element = html_options_collection_root(this)
# 3. Let delta be index minus length
let delta = (i64)index - (i64)length
# 4. If delta is greater than zero, then append a DocumentFragment consisting of delta new option elements with
# no attributes and no child nodes to the select element on which the HTMLOptionsCollection is rooted.
if delta > 0:
var i: i64 = 0
while i < delta:
let new_option = try dom_create_element(dom_node_document(root_element), html_tag_names_option, namespace_html)
_ = try dom_node_append_child(root_element, new_option.dom_node())
i += 1
# 5. If delta is greater than or equal to zero, append value to the select element. Otherwise, replace the indexth
# element in the collection by value.
if delta >= 0:
_ = try dom_node_append_child(root_element, option_element.dom_node())
else:
let child = dom_html_collection_item(this.dom_html_collection(), (usize)index) else trap("VERIFY: item")
_ = try dom_node_replace_child(root_element, option_element.dom_node(), child.dom_node())
## Ported from HTMLOptionsCollection.cpp:122 Web::HTML::HTMLOptionsCollection::add.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-add
pub func html_options_collection_add(this: HtmlOptionsCollection*, element: HtmlOptionOrOptGroupElement, before: HtmlNullableHtmlElementOrElementIndex = .empty) -> !:
let realm = bind_platform_object_realm(this.bind_platform_object())
let resolved_element = match element:
.html_option_element(option) => (option else trap("VERIFY: a root")).html_element()
.html_opt_group_element(optgroup) => (optgroup else trap("VERIFY: a root")).html_element()
var before_element: DomNode*? = none
match before:
.html_element(before_html_element):
if let before_node = before_html_element:
before_element = before_node.dom_node()
_:
_ = before_element
let root = html_options_collection_root(this)
# 1. If element is an ancestor of the select element on which the HTMLOptionsCollection is rooted, then throw a "HierarchyRequestError" DOMException.
if tree_node_is_ancestor_of(resolved_element.dom_node(), root):
try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(realm, ak.utf16_string_from_str("The provided element is an ancestor of the root select element."))))
# 2. If before is an element, but that element isn't a descendant of the select element on which the HTMLOptionsCollection is rooted, then throw a "NotFoundError" DOMException.
if before_element != none and not tree_node_is_descendant_of(before_element else trap("unreachable"), root):
try idl_throw(.idl_dom_exception(idl_not_found_error_create(realm, ak.utf16_string_from_str("The 'before' element is not a descendant of the root select element."))))
# 3. If element and before are the same element, then return.
if before_element != none and (resolved_element.dom_node() == (before_element else trap("unreachable"))):
return
# 4. If before is a node, then let reference be that node. Otherwise, if before is an integer, and there is a beforeth node in the collection, let reference be that node. Otherwise, let reference be null.
var reference: DomNode*? = none
if before_element != none:
reference = before_element
else:
match before:
.i32_(before_index):
if let item = dom_html_collection_item(this.dom_html_collection(), (usize)before_index):
reference = item.dom_node()
_:
_ = reference
# 5. If reference is not null, let parent be the parent node of reference. Otherwise, let parent be the select element on which the HTMLOptionsCollection is rooted.
var parent = root
if let reference_node = reference:
parent = reference_node.tree_node.m_parent else trap("VERIFY: a parent")
# 6. Pre-insert element into parent node before reference.
_ = try dom_node_pre_insert(parent, resolved_element.dom_node(), reference)
## Ported from HTMLOptionsCollection.cpp:163 Web::HTML::HTMLOptionsCollection::remove.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-remove
pub func html_options_collection_remove(this: HtmlOptionsCollection*, index: i32):
# 1. If the number of nodes represented by the collection is zero, return.
if dom_html_collection_length(this.dom_html_collection()) == 0:
return
# 2. If index is not a number greater than or equal to 0 and less than the number of nodes represented by the collection, return.
if index < 0 or (usize)(u32)index >= dom_html_collection_length(this.dom_html_collection()):
return
# 3. Let element be the indexth element in the collection.
let element = dom_html_collection_item(this.dom_html_collection(), (usize)index) else trap("VERIFY: item")
# 4. Remove element from its parent node.
dom_node_remove(element.dom_node())
## Ported from HTMLOptionsCollection.cpp:181 Web::HTML::HTMLOptionsCollection::selected_index.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-selectedindex
pub func html_options_collection_selected_index(this: HtmlOptionsCollection*) -> i32:
# The selectedIndex IDL attribute must act like the identically named attribute
# on the select element on which the HTMLOptionsCollection is rooted.
return html_select_element_selected_index(as_html_select_element(html_options_collection_root(this)))
## Ported from HTMLOptionsCollection.cpp:188 Web::HTML::HTMLOptionsCollection::set_selected_index.
pub func html_options_collection_set_selected_index(this: HtmlOptionsCollection*, index: i32) -> !:
try html_select_element_set_selected_index(as_html_select_element(html_options_collection_root(this)), index)