dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / html_collection.lucb
#==============================================================================================
#
# html_collection - DOM::HTMLCollection: a live, filtered (and optionally sorted) element list
#
# DESCRIPTION:
# Port of LibWeb/DOM/HTMLCollection.cpp (lines 1-186) and the inline functions of
# HTMLCollection.h. The elements are cached (as weak references) until the document's
# DOM tree version changes; the name → element mappings are built lazily from the cache.
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from HTMLCollection.cpp:21 Web::DOM::HTMLCollection::create.
pub func dom_html_collection_create(root: DomParentNode*, scope: DomHtmlCollectionScope, filter: ak.Function1[DomElement*, bool], sort: ak.Function2[DomElement*, DomElement*, bool]) -> DomHtmlCollection*:
let realm = bind_platform_object_realm(root.bind_platform_object())
return realm_create_dom_html_collection(realm, root, scope, filter, sort)
## Ported from HTMLCollection.cpp:26 Web::DOM::HTMLCollection::HTMLCollection.
pub func dom_html_collection_construct(this: DomHtmlCollection*, root: DomParentNode*, scope: DomHtmlCollectionScope, filter: ak.Function1[DomElement*, bool], sort: ak.Function2[DomElement*, DomElement*, bool]):
bind_platform_object_construct_realm(this.bind_platform_object(), bind_platform_object_realm(root.bind_platform_object()))
dom_html_collection_init_fields(this)
this.m_root = root
this.m_filter = filter
this.m_sort = sort
this.m_scope = scope
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 HTMLCollection.cpp:42 Web::DOM::HTMLCollection::initialize (override).
pub func dom_html_collection_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_collection)
# Base::initialize(realm): PlatformObject inherits JS::Object's.
js_cell_initialize_impl(base, realm)
## Ported from HTMLCollection.cpp:48 Web::DOM::HTMLCollection::visit_edges (override).
pub func dom_html_collection_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomHtmlCollection*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_root)
# mark: The cache ===============================================================================
## Ported from HTMLCollection.cpp:54 Web::DOM::HTMLCollection::update_name_to_element_mappings_if_needed.
func dom_html_collection_update_name_to_element_mappings_if_needed(this: DomHtmlCollection*):
dom_html_collection_update_cache_if_needed(this)
if this.m_cached_name_to_element_mappings != none:
return
let mappings = new ak.OrderedHashMap[ak.FlyString, gc.Weak[DomElement], ak.FlyStringTraits] else trap("out of memory")
this.m_cached_name_to_element_mappings = mappings
for weak_element in ak.vector_span(&this.m_cached_elements):
let element = gc.weak_ptr(&weak_element) else trap("VERIFY: cached element is alive")
# 1. If element has an ID which is not in result, append element’s ID to result.
if let id = dom_element_id(element):
if not ak.fly_string_is_empty(&id) and not ak.ordered_hash_map_contains_k(mappings, id):
_ = ak.ordered_hash_map_set(mappings, id, weak_element)
# 2. If element is in the HTML namespace and has a name attribute whose value is neither the empty string nor is in result, append element’s name attribute value to result.
if dom_node_optional_fly_string_is(dom_element_namespace_uri(element), namespace_html):
if let element_name = dom_element_name(element):
if not ak.fly_string_is_empty(&element_name) and not ak.ordered_hash_map_contains_k(mappings, element_name):
_ = ak.ordered_hash_map_set(mappings, element_name, weak_element)
## `m_filter(element)`.
func dom_html_collection_matches(this: DomHtmlCollection*, element: DomElement*) -> bool:
let call = this.m_filter.call else trap("VERIFY: filter")
return call(this.m_filter.context, element)
## update_cache_if_needed's insertion_sort comparator: `this->m_sort(*a, *b)`.
func dom_html_collection_update_cache_less_than(context: void*?, a: const gc.Weak[DomElement]*, b: const gc.Weak[DomElement]*) -> bool:
let this = (DomHtmlCollection*)(context else trap("no collection"))
let call = this.m_sort.call else trap("VERIFY: sort")
let a_element = gc.weak_ptr(a) else trap("VERIFY: cached element is alive")
let b_element = gc.weak_ptr(b) else trap("VERIFY: cached element is alive")
return call(this.m_sort.context, a_element, b_element)
## Ported from HTMLCollection.cpp:76 Web::DOM::HTMLCollection::update_cache_if_needed.
func dom_html_collection_update_cache_if_needed(this: DomHtmlCollection*):
let document = dom_node_document(this.m_root.dom_node())
# Nothing to do, the DOM hasn't updated since we last built the cache.
if this.m_cached_dom_tree_version == dom_document_dom_tree_version(document):
return
ak.vector_clear(&this.m_cached_elements)
this.m_cached_name_to_element_mappings = none
if this.m_scope == DomHtmlCollectionScope.descendants:
for node in dom_node_subtree(this.m_root.dom_node()):
let element = as_if_dom_element(node) else continue
if dom_html_collection_matches(this, element):
ak.vector_append_t(&this.m_cached_elements, gc.make_weak[DomElement](element))
else:
for node in dom_node_children(this.m_root.dom_node()):
let element = as_if_dom_element(node) else continue
if dom_html_collection_matches(this, element):
ak.vector_append_t(&this.m_cached_elements, gc.make_weak[DomElement](element))
if this.m_sort.call != none:
let comparator = ak.Function2[const gc.Weak[DomElement]*, const gc.Weak[DomElement]*, bool](call = dom_html_collection_update_cache_less_than, context = this)
ak.insertion_sort_t_comparator(ak.vector_span(&this.m_cached_elements), comparator)
this.m_cached_dom_tree_version = dom_document_dom_tree_version(document)
## Ported from HTMLCollection.cpp:107 Web::DOM::HTMLCollection::collect_matching_elements.
pub func dom_html_collection_collect_matching_elements(this: DomHtmlCollection*) -> ak.Vector[DomElement*]:
dom_html_collection_update_cache_if_needed(this)
var elements: ak.Vector[DomElement*]
for element in ak.vector_span(&this.m_cached_elements):
ak.vector_append_t(&elements, gc.weak_ptr(&element) else trap("VERIFY: cached element is alive"))
return elements
# mark: The collection ==========================================================================
## Ported from HTMLCollection.cpp:117 Web::DOM::HTMLCollection::length.
# https://dom.spec.whatwg.org/#dom-htmlcollection-length
pub func dom_html_collection_length(this: DomHtmlCollection*) -> usize:
# The length getter steps are to return the number of nodes represented by the collection.
dom_html_collection_update_cache_if_needed(this)
return ak.vector_size(&this.m_cached_elements)
## Ported from HTMLCollection.cpp:125 Web::DOM::HTMLCollection::item.
# https://dom.spec.whatwg.org/#dom-htmlcollection-item
pub func dom_html_collection_item(this: DomHtmlCollection*, index: usize) -> DomElement*?:
# The item(index) method steps are to return the indexth element in the collection. If there is no indexth element in the collection, then the method must return null.
dom_html_collection_update_cache_if_needed(this)
if index >= ak.vector_size(&this.m_cached_elements):
return none
return gc.weak_ptr(ak.vector_at(&this.m_cached_elements, index))
## Ported from HTMLCollection.cpp:135 Web::DOM::HTMLCollection::named_item.
# https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
pub func dom_html_collection_named_item(this: DomHtmlCollection*, key: ak.FlyString) -> DomElement*?:
# 1. If key is the empty string, return null.
if ak.fly_string_is_empty(&key):
return none
dom_html_collection_update_name_to_element_mappings_if_needed(this)
let mappings = this.m_cached_name_to_element_mappings else trap("VERIFY: name mappings")
if let element = ak.ordered_hash_map_get_k(mappings, key):
return gc.weak_ptr(element)
return none
## Ported from HTMLCollection.cpp:148 Web::DOM::HTMLCollection::is_supported_property_name (override).
# https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
pub func dom_html_collection_is_supported_property_name_impl(base: BindPlatformObject*, name: ak.FlyString) -> bool:
let this = (DomHtmlCollection*)base
dom_html_collection_update_name_to_element_mappings_if_needed(this)
let mappings = this.m_cached_name_to_element_mappings else trap("VERIFY: name mappings")
return ak.ordered_hash_map_contains_k(mappings, name)
## Ported from HTMLCollection.cpp:155 Web::DOM::HTMLCollection::supported_property_names (override).
# https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
pub func dom_html_collection_supported_property_names_impl(base: BindPlatformObject*) -> ak.Vector[ak.FlyString]:
let this = (DomHtmlCollection*)base
# 1. Let result be an empty list.
var result: ak.Vector[ak.FlyString]
# 2. For each element represented by the collection, in tree order:
dom_html_collection_update_name_to_element_mappings_if_needed(this)
let mappings = this.m_cached_name_to_element_mappings else trap("VERIFY: name mappings")
for it in ak.ordered_hash_map_begin(mappings):
ak.vector_append_t(&result, it.key)
# 3. Return result.
return result
## Ported from HTMLCollection.cpp:170 Web::DOM::HTMLCollection::item_value (override).
pub func dom_html_collection_item_value_impl(base: BindPlatformObject*, index: usize) -> JsValue?:
let element = dom_html_collection_item((DomHtmlCollection*)base, index) else return none
return bind_platform_object_wrapper(element.bind_platform_object())
## Ported from HTMLCollection.cpp:178 Web::DOM::HTMLCollection::named_item_value (override).
pub func dom_html_collection_named_item_value_impl(base: BindPlatformObject*, name: ak.FlyString) -> JsValue:
let element = dom_html_collection_named_item((DomHtmlCollection*)base, name) else return js_undefined()
return bind_platform_object_wrapper(element.bind_platform_object())
# mark: HTMLCollection.h ========================================================================
## Ported from HTMLCollection.h:53 Web::DOM::HTMLCollection::root.
pub func dom_html_collection_root(this: DomHtmlCollection*) -> DomParentNode*:
return this.m_root
## Ported from HTMLCollection.h:54 Web::DOM::HTMLCollection::root.
pub func dom_html_collection_root_const(this: DomHtmlCollection*) -> DomParentNode*:
return this.m_root