dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / html_form_controls_collection.lucb
#==============================================================================================
#
# html_form_controls_collection - HTML::HTMLFormControlsCollection: a form's elements
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLFormControlsCollection.cpp (lines 1-87): creation, initialize,
# namedItem (an element, or a RadioNodeList of the elements sharing the name) and the
# named property value.
#
#==============================================================================================
## Ported from HTMLFormControlsCollection.cpp:19 Web::HTML::HTMLFormControlsCollection::create.
pub func html_form_controls_collection_create(root: DomParentNode*, scope: DomHtmlCollectionScope, filter: ak.Function1[DomElement*, bool]) -> HtmlFormControlsCollection*:
let realm = bind_platform_object_realm(root.bind_platform_object())
return realm_create_html_form_controls_collection(realm, root, scope, filter)
## Ported from HTMLFormControlsCollection.cpp:24 Web::HTML::HTMLFormControlsCollection::HTMLFormControlsCollection.
pub func html_form_controls_collection_construct(this: HtmlFormControlsCollection*, root: DomParentNode*, scope: DomHtmlCollectionScope, filter: ak.Function1[DomElement*, bool]):
dom_html_collection_construct(&this.html_collection, root, scope, filter, ak.Function2[DomElement*, DomElement*, bool]())
## Ported from HTMLFormControlsCollection.cpp:31 Web::HTML::HTMLFormControlsCollection::initialize (override).
pub func html_form_controls_collection_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_form_controls_collection)
dom_html_collection_initialize_impl(base, realm)
## The `[name](auto const& node)` filter of the RadioNodeList namedItem returns: its name.
struct HtmlFormControlsCollectionNamedItemFilter:
var name: ak.FlyString
## The filter: elements whose id or name is the name.
static func call(context: void*?, node: DomNode*) -> bool:
let captures = (HtmlFormControlsCollectionNamedItemFilter*)(context else trap("namedItem filter: no captures"))
if not is_dom_element(node):
return false
let element = as_dom_element(node)
return dom_node_optional_fly_string_is(dom_element_id(element), captures.name) or dom_node_optional_fly_string_is(dom_element_name(element), captures.name)
## Ported from HTMLFormControlsCollection.cpp:38 Web::HTML::HTMLFormControlsCollection::named_item_or_radio_node_list.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmlformcontrolscollection-nameditem
pub func html_form_controls_collection_named_item_or_radio_node_list(this: HtmlFormControlsCollection*, name: ak.FlyString) -> EmptyOrDomElementOrHtmlRadioNodeList:
# 1. If name is the empty string, return null and stop the algorithm.
if ak.fly_string_is_empty(&name):
return EmptyOrDomElementOrHtmlRadioNodeList.empty
# 2. If, at the time the method is called, there is exactly one node in the collection that has either an id attribute or a name attribute equal to name, then return that node and stop the algorithm.
# 3. Otherwise, if there are no nodes in the collection that have either an id attribute or a name attribute equal to name, then return null and stop the algorithm.
var matching_element: DomElement*? = none
var multiple_matching = false
var collection = dom_html_collection_collect_matching_elements(&this.html_collection)
for element in ak.vector_span(&collection):
if not dom_node_optional_fly_string_is(dom_element_id(element), name) and not dom_node_optional_fly_string_is(dom_element_name(element), name):
continue
if matching_element != none:
multiple_matching = true
break
matching_element = element
if matching_element == none:
return EmptyOrDomElementOrHtmlRadioNodeList.empty
if not multiple_matching:
return EmptyOrDomElementOrHtmlRadioNodeList.dom_element(matching_element)
# 4. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the
# RadioNodeList object are those that have either an id attribute or a name attribute equal to name. The nodes in the RadioNodeList object must be sorted in tree
# order. Return that RadioNodeList object.
let captures = new HtmlFormControlsCollectionNamedItemFilter(name = name) else trap("out of memory")
let realm = bind_platform_object_realm(this.bind_platform_object())
let root = dom_html_collection_root(&this.html_collection)
return EmptyOrDomElementOrHtmlRadioNodeList.html_radio_node_list(html_radio_node_list_create(realm, root.dom_node(), DomLiveNodeListScope.descendants, ak.Function1[DomNode*, bool](call = HtmlFormControlsCollectionNamedItemFilter.call, context = captures)))
## Ported from HTMLFormControlsCollection.cpp:80 Web::HTML::HTMLFormControlsCollection::named_item_value (override).
pub func html_form_controls_collection_named_item_value_impl(base: BindPlatformObject*, name: ak.FlyString) -> JsValue:
let this = (HtmlFormControlsCollection*)base
match html_form_controls_collection_named_item_or_radio_node_list(this, name):
.empty:
return js_undefined()
.dom_element(element):
return js_value_from_object((JsObject*?)element)
.html_radio_node_list(radio_node_list):
return js_value_from_object((JsObject*?)radio_node_list)