dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / parent_node.lucb
#==============================================================================================
#
# parent_node - DOM::ParentNode: element children, selectors, collections, prepend/append
#
# DESCRIPTION:
# Port of LibWeb/DOM/ParentNode.cpp (lines 1-282) and the inline functions of
# LibWeb/DOM/ParentNode.h (lines 1-97): scope-matching a selectors string for
# querySelector()/querySelectorAll(), the element-child queries, the children,
# getElementsByTagName(NS)() and getElementsByClassName() collections, prepend(),
# append(), replaceChildren(), moveBefore() and getElementById().
#
#==============================================================================================
# mark: ParentNode.h ============================================================================
## Ported from ParentNode.h:45 Web::DOM::ParentNode::has_child_affected_by_backward_structural_changes.
pub func dom_parent_node_has_child_affected_by_backward_structural_changes(this: DomParentNode*) -> bool:
return this.m_has_child_affected_by_backward_structural_changes
## Ported from ParentNode.h:46 Web::DOM::ParentNode::set_has_child_affected_by_backward_structural_changes.
pub func dom_parent_node_set_has_child_affected_by_backward_structural_changes(this: DomParentNode*, value: bool):
this.m_has_child_affected_by_backward_structural_changes = value
## Ported from ParentNode.h:49 Web::DOM::ParentNode::ParentNode(JS::Realm&, Document&, NodeType).
pub func dom_parent_node_construct_realm(this: DomParentNode*, realm: Realm*, document: DomDocument*, type_: DomNodeType):
dom_node_construct_realm(this.dom_node(), realm, document, type_)
## Ported from ParentNode.h:54 Web::DOM::ParentNode::ParentNode(Document&, NodeType).
pub func dom_parent_node_construct_dom_document(this: DomParentNode*, document: DomDocument*, type_: DomNodeType):
dom_node_construct_dom_document(this.dom_node(), document, type_)
## Ported from ParentNode.h:70 Web::DOM::Node::first_flat_tree_ancestor_of_type: the nearest
## flat-tree ancestor of class `class_info` (the caller downcasts, DESIGN.md §2.4).
pub func dom_node_first_flat_tree_ancestor_of_type(this: DomNode*, class_info: const ak.ClassInfo*) -> DomNode*?:
var ancestor = dom_node_flat_tree_parent(this)
while let parent_node = ancestor:
let id = parent_node.cell().class_id
if id >= class_info.first_id and id <= class_info.last_id:
return parent_node.dom_node()
ancestor = dom_node_flat_tree_parent(parent_node.dom_node())
return none
## Ported from ParentNode.h:80 Web::DOM::ParentNode::for_each_child const: each child given to
## `callback` until it answers Break.
pub func dom_parent_node_for_each_child_const(this: DomParentNode*, callback: ak.Function1[DomNode*, ak.IterationDecision]):
var node = tree_node_first_child(this.dom_node())
while let child = node:
if ak.function1_call(&callback, child) == ak.IterationDecision.break_:
return
node = tree_node_next_sibling(child)
## Ported from ParentNode.h:89 Web::DOM::ParentNode::for_each_child: each child given to `callback`
## until it answers Break.
pub func dom_parent_node_for_each_child(this: DomParentNode*, callback: ak.Function1[DomNode*, ak.IterationDecision]):
var node = tree_node_first_child(this.dom_node())
while let child = node:
if ak.function1_call(&callback, child) == ak.IterationDecision.break_:
return
node = tree_node_next_sibling(child)
# mark: Selectors ===============================================================================
## `AbstractElement { element }`: the implicit conversion from Element& that SelectorEngine::matches
## receives.
func dom_parent_node_abstract_element(element: DomElement*) -> DomAbstractElement:
var abstract_element = DomAbstractElement(m_element = element)
dom_abstract_element_construct(&abstract_element, element)
return abstract_element
## Ported from ParentNode.cpp:31 Web::DOM::scope_match_a_selectors_string.
# https://dom.spec.whatwg.org/#scope-match-a-selectors-string
pub func dom_scope_match_a_selectors_string(node: DomParentNode*, selector_text: ak.StringView, return_matches: DomReturnMatches) -> DomElementOrDomNodeList!:
# To scope-match a selectors string selectors against a node, run these steps:
let document = dom_node_document(node.dom_node())
# 1. Let s be the result of parse a selector selectors.
let maybe_selectors = dom_document_parse_or_cache_selector_list(document, selector_text)
# 2. If s is failure, then throw a "SyntaxError" DOMException.
let selectors = maybe_selectors else return try idl_throw(.idl_dom_exception(idl_syntax_error_create(bind_platform_object_realm(node.bind_platform_object()), ak.utf16_string_from_str("Failed to parse selector"))))
# 3. Return the result of match a selector against a tree with s and node’s root using scoping root node.
var single_result: DomElement*? = none
var results: ak.Vector[DomNode*?]
# FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
# for_each_in_subtree_of_type<Element>
subtree: for descendant in dom_node_subtree(node.dom_node()):
let element = as_if_dom_element(descendant) else continue
for selector in ak.vector_span_const(&selectors):
var context = SelectorEngineMatchContext()
if selector_engine_matches(selector, dom_parent_node_abstract_element(element), none, &context, node):
if return_matches == DomReturnMatches.first:
single_result = element
break subtree
ak.vector_append_t(&results, element.dom_node())
break
if return_matches == DomReturnMatches.first:
return .dom_element(single_result)
return .dom_node_list(dom_static_node_list_create(bind_platform_object_realm(node.bind_platform_object()), results))
## Ported from ParentNode.cpp:71 Web::DOM::ParentNode::query_selector.
# https://dom.spec.whatwg.org/#dom-parentnode-queryselector
pub func dom_parent_node_query_selector(this: DomParentNode*, selector_text: ak.StringView) -> DomElement*?!:
# The querySelector(selectors) method steps are to return the first result of running scope-match a selectors string selectors against this,
# if the result is not an empty list; otherwise null.
match try dom_scope_match_a_selectors_string(this, selector_text, DomReturnMatches.first):
.dom_element(element):
return element
.dom_node_list(_):
trap("Variant::get<GC::Ptr<Element>>: holds a NodeList")
## Ported from ParentNode.cpp:79 Web::DOM::ParentNode::query_selector_all.
# https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall
pub func dom_parent_node_query_selector_all(this: DomParentNode*, selector_text: ak.StringView) -> DomNodeList*!:
# The querySelectorAll(selectors) method steps are to return the static result of running scope-match a selectors string selectors against this.
match try dom_scope_match_a_selectors_string(this, selector_text, DomReturnMatches.all):
.dom_node_list(node_list):
return node_list
.dom_element(_):
trap("Variant::get<GC::Ref<NodeList>>: holds an Element")
# mark: Element children ========================================================================
## Ported from ParentNode.cpp:85 Web::DOM::ParentNode::first_element_child.
pub func dom_parent_node_first_element_child(this: DomParentNode*) -> DomElement*?:
let child = tree_node_first_child_of_type(this.dom_node(), &dom_element_class) else return none
return (DomElement*)child
## Ported from ParentNode.cpp:90 Web::DOM::ParentNode::last_element_child.
pub func dom_parent_node_last_element_child(this: DomParentNode*) -> DomElement*?:
let child = tree_node_last_child_of_type(this.dom_node(), &dom_element_class) else return none
return (DomElement*)child
## Ported from ParentNode.cpp:96 Web::DOM::ParentNode::child_element_count.
# https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
pub func dom_parent_node_child_element_count(this: DomParentNode*) -> u32:
var count: u32 = 0
var node = tree_node_first_child(this.dom_node())
while let child = node:
if is_dom_element(child):
count += 1
node = tree_node_next_sibling(child)
return count
## Ported from ParentNode.cpp:106 Web::DOM::ParentNode::visit_edges (override).
pub func dom_parent_node_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomParentNode*)base
dom_node_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_children)
# mark: Collections =============================================================================
## The HTMLCollection filter of ParentNode::children and of the "*" cases: every element matches.
func dom_parent_node_match_every_element(context: void*?, element: DomElement*) -> bool:
return true
## Ported from ParentNode.cpp:113 Web::DOM::ParentNode::children.
# https://dom.spec.whatwg.org/#dom-parentnode-children
pub func dom_parent_node_children_2(this: DomParentNode*) -> DomHtmlCollection*:
# The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
if this.m_children == none:
this.m_children = dom_html_collection_create(this, DomHtmlCollectionScope.children, ak.Function1[DomElement*, bool](call = dom_parent_node_match_every_element, context = none), ak.Function2[DomElement*, DomElement*, bool]())
return this.m_children else trap("unreachable")
## The captures of ParentNode::get_elements_by_tag_name's HTML-document filter.
struct DomParentNodeGetElementsByTagNameHtmlFilter:
var qualified_name: ak.FlyString
var qualified_name_in_ascii_lowercase: ak.FlyString
## ParentNode::get_elements_by_tag_name's filter for an HTML document.
func dom_parent_node_get_elements_by_tag_name_html_filter(context: void*?, element: DomElement*) -> bool:
let captures = (DomParentNodeGetElementsByTagNameHtmlFilter*)(context else trap("no captures"))
let element_qualified_name = dom_element_qualified_name(element)
# - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
if dom_node_optional_fly_string_is(dom_element_namespace_uri(element), namespace_html):
return ak.fly_string_eq_fly_string(&element_qualified_name, &captures.qualified_name_in_ascii_lowercase)
# - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
return ak.fly_string_eq_fly_string(&element_qualified_name, &captures.qualified_name)
## ParentNode::get_elements_by_tag_name's filter for other documents (the context is the captured
## qualified name).
func dom_parent_node_get_elements_by_tag_name_filter(context: void*?, element: DomElement*) -> bool:
let qualified_name = (ak.FlyString*)(context else trap("no captures"))
let element_qualified_name = dom_element_qualified_name(element)
return ak.fly_string_eq_fly_string(&element_qualified_name, qualified_name)
## Ported from ParentNode.cpp:126 Web::DOM::ParentNode::get_elements_by_tag_name.
# https://dom.spec.whatwg.org/#concept-getelementsbytagname
# NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
pub func dom_parent_node_get_elements_by_tag_name(this: DomParentNode*, qualified_name: ak.FlyString) -> DomHtmlCollection*:
# 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
if ak.fly_string_eq_string_view(&qualified_name, ak.sv("*")):
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_match_every_element, context = none), ak.Function2[DomElement*, DomElement*, bool]())
# 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
if dom_document_document_type(dom_node_document(tree_node_root(this.dom_node()))) == DocumentDomDocumentType.html:
let qualified_name_in_ascii_lowercase = ak.fly_string_to_ascii_lowercase(&qualified_name)
let captures = new DomParentNodeGetElementsByTagNameHtmlFilter(qualified_name = qualified_name, qualified_name_in_ascii_lowercase = qualified_name_in_ascii_lowercase) else trap("out of memory")
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_tag_name_html_filter, context = captures), ak.Function2[DomElement*, DomElement*, bool]())
# 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
let captured_qualified_name = new ak.FlyString else trap("out of memory")
*captured_qualified_name = qualified_name
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_tag_name_filter, context = captured_qualified_name), ak.Function2[DomElement*, DomElement*, bool]())
## The captures of ParentNode::get_elements_by_tag_name_ns's filters.
struct DomParentNodeGetElementsByTagNameNsFilter:
var namespace_: ak.FlyString?
var local_name: ak.FlyString
## ParentNode::get_elements_by_tag_name_ns's filter when only the namespace is "*".
func dom_parent_node_get_elements_by_tag_name_ns_local_name_filter(context: void*?, element: DomElement*) -> bool:
let captures = (DomParentNodeGetElementsByTagNameNsFilter*)(context else trap("no captures"))
let element_local_name = dom_element_local_name(element)
return ak.fly_string_eq_fly_string(&element_local_name, &captures.local_name)
## ParentNode::get_elements_by_tag_name_ns's filter when only the local name is "*".
func dom_parent_node_get_elements_by_tag_name_ns_namespace_filter(context: void*?, element: DomElement*) -> bool:
let captures = (DomParentNodeGetElementsByTagNameNsFilter*)(context else trap("no captures"))
return dom_node_optional_fly_strings_equal(dom_element_namespace_uri(element), captures.namespace_)
## ParentNode::get_elements_by_tag_name_ns's filter on both the namespace and the local name.
func dom_parent_node_get_elements_by_tag_name_ns_filter(context: void*?, element: DomElement*) -> bool:
let captures = (DomParentNodeGetElementsByTagNameNsFilter*)(context else trap("no captures"))
let element_local_name = dom_element_local_name(element)
return dom_node_optional_fly_strings_equal(dom_element_namespace_uri(element), captures.namespace_) and ak.fly_string_eq_fly_string(&element_local_name, &captures.local_name)
## Ported from ParentNode.cpp:156 Web::DOM::ParentNode::get_elements_by_tag_name_ns.
# https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
# NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
pub func dom_parent_node_get_elements_by_tag_name_ns(this: DomParentNode*, namespace_: ak.FlyString?, local_name: ak.FlyString) -> DomHtmlCollection*:
# 1. If namespace is the empty string, set it to null.
var maybe_namespace = namespace_
if dom_node_optional_fly_strings_equal(maybe_namespace, ak.fly_string_new()):
maybe_namespace = none
let namespace_is_star = dom_node_optional_fly_string_is(maybe_namespace, ak.fly_string_from_str("*"))
let local_name_is_star = ak.fly_string_eq_string_view(&local_name, ak.sv("*"))
# 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
if namespace_is_star and local_name_is_star:
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_match_every_element, context = none), ak.Function2[DomElement*, DomElement*, bool]())
let captures = new DomParentNodeGetElementsByTagNameNsFilter(namespace_ = maybe_namespace, local_name = local_name) else trap("out of memory")
# 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
if namespace_is_star:
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_tag_name_ns_local_name_filter, context = captures), ak.Function2[DomElement*, DomElement*, bool]())
# 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
if local_name_is_star:
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_tag_name_ns_namespace_filter, context = captures), ak.Function2[DomElement*, DomElement*, bool]())
# 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_tag_name_ns_filter, context = captures), ak.Function2[DomElement*, DomElement*, bool]())
# mark: Prepend, append, replace children and move before =======================================
## Ported from ParentNode.cpp:190 Web::DOM::ParentNode::prepend.
# https://dom.spec.whatwg.org/#dom-parentnode-prepend
pub func dom_parent_node_prepend(this: DomParentNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
# 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
let node = try dom_convert_nodes_to_single_node(nodes, dom_node_document(this.dom_node()))
# 2. Pre-insert node into this before this’s first child.
_ = try dom_node_pre_insert(this.dom_node(), node, tree_node_first_child(this.dom_node()))
## Ported from ParentNode.cpp:201 Web::DOM::ParentNode::append.
pub func dom_parent_node_append(this: DomParentNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
# 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
let node = try dom_convert_nodes_to_single_node(nodes, dom_node_document(this.dom_node()))
# 2. Append node to this.
_ = try dom_node_append_child(this.dom_node(), node)
## Ported from ParentNode.cpp:212 Web::DOM::ParentNode::replace_children.
pub func dom_parent_node_replace_children(this: DomParentNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
# 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
let node = try dom_convert_nodes_to_single_node(nodes, dom_node_document(this.dom_node()))
# 2. Ensure pre-insertion validity of node into this before null.
try dom_node_ensure_pre_insertion_validity(this.dom_node(), bind_platform_object_realm(this.bind_platform_object()), node, none)
# 3. Replace all with node within this.
dom_node_replace_all(this.dom_node(), node)
## Ported from ParentNode.cpp:226 Web::DOM::ParentNode::move_before.
# https://dom.spec.whatwg.org/#dom-parentnode-movebefore
pub func dom_parent_node_move_before(this: DomParentNode*, node: DomNode*, child: DomNode*?) -> !:
# 1. Let referenceChild be child.
var reference_child = child
# 2. If referenceChild is node, then set referenceChild to node’s next sibling.
if reference_child == node:
reference_child = tree_node_next_sibling(node)
# 3. Move node into this before referenceChild.
try dom_node_move_node(node, this.dom_node(), reference_child)
# mark: Class names and ids =====================================================================
## The captures of ParentNode::get_elements_by_class_name's filter.
struct DomParentNodeGetElementsByClassNameFilter:
var list_of_class_names: ak.Vector[ak.FlyString]
var quirks_mode: bool
## ParentNode::get_elements_by_class_name's filter.
func dom_parent_node_get_elements_by_class_name_filter(context: void*?, element: DomElement*) -> bool:
let captures = (DomParentNodeGetElementsByClassNameFilter*)(context else trap("no captures"))
for name in ak.vector_span(&captures.list_of_class_names):
if not dom_element_has_class(element, name, ak.CaseSensitivity.case_insensitive if captures.quirks_mode else ak.CaseSensitivity.case_sensitive):
return false
return not ak.vector_is_empty(&captures.list_of_class_names)
## Ported from ParentNode.cpp:242 Web::DOM::ParentNode::get_elements_by_class_name.
# https://dom.spec.whatwg.org/#dom-document-getelementsbyclassname
pub func dom_parent_node_get_elements_by_class_name(this: DomParentNode*, class_names: ak.StringView) -> DomHtmlCollection*:
var list_of_class_names: ak.Vector[ak.FlyString]
let predicate = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
var names = ak.string_view_split_view_if(&class_names, &predicate)
for name in ak.vector_span(&names):
ak.vector_append_t(&list_of_class_names, ak.fly_string_from_utf8(name) else trap("FIXME: should propagate errors"))
let captures = new DomParentNodeGetElementsByClassNameFilter(list_of_class_names = list_of_class_names, quirks_mode = dom_document_in_quirks_mode(dom_node_document(this.dom_node()))) else trap("out of memory")
return dom_html_collection_create(this, DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = dom_parent_node_get_elements_by_class_name_filter, context = captures), ak.Function2[DomElement*, DomElement*, bool]())
## Ported from ParentNode.cpp:257 Web::DOM::ParentNode::get_element_by_id.
pub func dom_parent_node_get_element_by_id(this: DomParentNode*, id: ak.FlyString) -> DomElement*?:
if dom_node_is_connected(this.dom_node()):
# For connected document and shadow root we have a cache that allows fast lookup.
if dom_node_is_document(this.dom_node()):
let document = (DomDocument*)this
return dom_element_by_id_map_get(dom_document_element_by_id(document), id, document.dom_node())
if dom_node_is_shadow_root(this.dom_node()):
let shadow_root = (DomShadowRoot*)this
return dom_element_by_id_map_get(dom_shadow_root_element_by_id(shadow_root), id, shadow_root.dom_node())
var found_element: DomElement*? = none
# for_each_in_inclusive_subtree_of_type<Element>
for node in dom_node_inclusive_subtree(this.dom_node()):
let element = as_if_dom_element(node) else continue
if dom_node_optional_fly_string_is(dom_element_id(element), id):
found_element = element
break
return found_element