dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / node_5.lucb
#==============================================================================================
#
# node_5 - DOM::Node: document position, JSON dumps, replace all, serialization, equality
#
# DESCRIPTION:
# Port of LibWeb/DOM/Node.cpp (lines 1886-2349): compareDocumentPosition(), the
# host-including and shadow-including tree relations, ownerDocument, the DOM tree JSON
# dump of the inspector, scripting enabled/disabled, replace all and string replace all,
# the fragment serializing algorithm, unsafely set HTML, isSameNode() and isEqualNode().
#
#==============================================================================================
# mark: Helpers =================================================================================
## A node pointer as an address, null as 0 (C++ compares raw `Node*`s with `>`).
func dom_node_address(node: DomNode*?) -> usize:
let address = node else return 0
return (usize)address
## `Optional<FlyString> == Optional<FlyString>`: both empty, or both present and equal.
func dom_node_optional_fly_strings_equal(a: ak.FlyString?, b: ak.FlyString?) -> bool:
let a_value = a else return b == none
let b_value = b else return false
return ak.fly_string_eq_fly_string(&a_value, &b_value)
## `Optional<String> == String`: present and equal.
func dom_node_optional_string_equals(a: ak.String?, b: ak.String) -> bool:
let a_value = a else return false
return ak.string_equals(&a_value, &b)
# mark: Document position =======================================================================
## Ported from Node.cpp:1887 Web::DOM::Node::compare_document_position.
# https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
pub func dom_node_compare_document_position(this: DomNode*, other: DomNode*?) -> u16:
# 1. If this is other, then return zero.
if this == other:
return (u16)DomNodeDocumentPosition.document_position_equal
# 2. Let node1 be other and node2 be this.
var node1: DomNode*? = other
var node2: DomNode*? = this
# 3. Let attr1 and attr2 be null.
var attr1: DomAttr*? = none
var attr2: DomAttr*? = none
# 4. If node1 is an attribute, then set attr1 to node1 and node1 to attr1’s element.
if let node = node1:
if is_dom_attr(node):
let attr = as_dom_attr(node)
attr1 = attr
node1 = none
if let owner = dom_attr_owner_element(attr):
node1 = owner.dom_node()
# 5. If node2 is an attribute, then:
if is_dom_attr(this):
# 1. Set attr2 to node2 and node2 to attr2’s element.
let attr = as_dom_attr(this)
attr2 = attr
node2 = none
if let owner = dom_attr_owner_element(attr):
node2 = owner.dom_node()
# 2. If attr1 and node1 are non-null, and node2 is node1, then:
# (`if (attr1 && node1 && node2 == node1)` with an empty body in C++: only the FIXME below.)
# FIXME: 1. For each attr of node2’s attribute list:
# 1. If attr equals attr1, then return the result of adding DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC and DOCUMENT_POSITION_PRECEDING.
# 2. If attr equals attr2, then return the result of adding DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC and DOCUMENT_POSITION_FOLLOWING.
# 6. If node1 or node2 is null, or node1’s root is not node2’s root, then return the result of adding
# DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, and either DOCUMENT_POSITION_PRECEDING or DOCUMENT_POSITION_FOLLOWING, with the constraint that this is to be consistent, together.
var disconnected = true
if let first = node1:
if let second = node2:
disconnected = tree_node_root(first) != tree_node_root(second)
if disconnected:
let position = DomNodeDocumentPosition.document_position_preceding if dom_node_address(node1) > dom_node_address(node2) else DomNodeDocumentPosition.document_position_following
return (u16)(DomNodeDocumentPosition.document_position_disconnected | DomNodeDocumentPosition.document_position_implementation_specific | position)
var node1_ancestors: ak.Vector[DomNode*]
var ancestor_of_node1 = node1
while let node = ancestor_of_node1:
ak.vector_append_t(&node1_ancestors, node)
ancestor_of_node1 = tree_node_parent(node)
var node2_ancestors: ak.Vector[DomNode*]
var ancestor_of_node2 = node2
while let node = ancestor_of_node2:
ak.vector_append_t(&node2_ancestors, node)
ancestor_of_node2 = tree_node_parent(node)
# (rbegin()/rend(): the remaining elements of each chain, counted from the end)
var it_node1_ancestors = ak.vector_size(&node1_ancestors)
var it_node2_ancestors = ak.vector_size(&node2_ancestors)
# Walk ancestor chains of both nodes starting from root
while it_node1_ancestors != 0 and it_node2_ancestors != 0:
let ancestor1 = *ak.vector_at(&node1_ancestors, it_node1_ancestors - 1)
let ancestor2 = *ak.vector_at(&node2_ancestors, it_node2_ancestors - 1)
# If ancestors of nodes at the same level in the tree are different then preceding node is the one with lower sibling position
if ancestor1 != ancestor2:
var node: DomNode*? = ancestor1
while let sibling = node:
if sibling == ancestor2:
return (u16)DomNodeDocumentPosition.document_position_preceding
node = tree_node_next_sibling(sibling)
return (u16)DomNodeDocumentPosition.document_position_following
it_node1_ancestors -= 1
it_node2_ancestors -= 1
# NOTE: If nodes in ancestors chains are the same but one chain is longer, then one node is ancestor of another.
# The node with shorter ancestors chain is the ancestor.
# The node with longer ancestors chain is the descendant.
# 7. If node1 is an ancestor of node2 and attr1 is null, or node1 is node2 and attr2 is non-null, then return the result of adding DOCUMENT_POSITION_CONTAINS to DOCUMENT_POSITION_PRECEDING.
if (ak.vector_size(&node1_ancestors) < ak.vector_size(&node2_ancestors) and attr1 == none) or (node1 == node2 and attr2 != none):
return (u16)(DomNodeDocumentPosition.document_position_contains | DomNodeDocumentPosition.document_position_preceding)
# 8. If node1 is a descendant of node2 and attr2 is null, or node1 is node2 and attr1 is non-null, then return the result of adding DOCUMENT_POSITION_CONTAINED_BY to DOCUMENT_POSITION_FOLLOWING.
if (ak.vector_size(&node1_ancestors) > ak.vector_size(&node2_ancestors) and attr2 == none) or (node1 == node2 and attr1 != none):
return (u16)(DomNodeDocumentPosition.document_position_contained_by | DomNodeDocumentPosition.document_position_following)
# 9. If node1 is preceding node2, then return DOCUMENT_POSITION_PRECEDING.
if ak.vector_size(&node1_ancestors) < ak.vector_size(&node2_ancestors):
return (u16)DomNodeDocumentPosition.document_position_preceding
# 10. Return DOCUMENT_POSITION_FOLLOWING.
return (u16)DomNodeDocumentPosition.document_position_following
## Ported from Node.cpp:1977 Web::DOM::Node::is_host_including_inclusive_ancestor_of.
# https://dom.spec.whatwg.org/#concept-tree-host-including-inclusive-ancestor
pub func dom_node_is_host_including_inclusive_ancestor_of(this: DomNode*, other: DomNode*) -> bool:
# An object A is a host-including inclusive ancestor of an object B,
# if either A is an inclusive ancestor of B,
if tree_node_is_inclusive_ancestor_of(this, other):
return true
# or if B’s root has a non-null host and A is a host-including inclusive ancestor of B’s root’s host
let other_root = tree_node_root(other)
if is_dom_document_fragment(other_root):
if let host = dom_document_fragment_host((DomDocumentFragment*)other_root):
if tree_node_is_inclusive_ancestor_of(this, host.dom_node()):
return true
return false
## Ported from Node.cpp:1994 Web::DOM::Node::owner_document.
# https://dom.spec.whatwg.org/#dom-node-ownerdocument
pub func dom_node_owner_document(this: DomNode*) -> DomDocument*?:
# The ownerDocument getter steps are to return null, if this is a document; otherwise this’s node document.
if dom_node_is_document(this):
return none
return this.m_document
# mark: The DOM tree as JSON ====================================================================
## Ported from Node.cpp:2006 Web::DOM::Node::is_uninteresting_whitespace_node.
# This function tells us whether a node is interesting enough to show up
# in the DOM inspector. This hides two things:
# - Non-rendered whitespace
# - Rendered whitespace between block-level elements
pub func dom_node_is_uninteresting_whitespace_node(this: DomNode*) -> bool:
if not is_dom_text(this):
return false
let data = dom_character_data_data(((DomText*)this).dom_character_data())
if not ak.utf16_string_base_is_ascii_whitespace(&data.utf16_string_base):
return false
let layout_node = dom_node_layout_node(this) else return true
if let parent = layout_node_parent(layout_node):
if layout_node_is_anonymous(parent.layout_node()):
return true
return false
## Ported from Node.cpp:2019 Web::DOM::Node::serialize_child_as_json.
pub func dom_node_serialize_child_as_json(this: DomNode*, children_array: ak.JsonArraySerializer[ak.StringBuilder]*, child: DomNode*) -> ak.IterationDecision:
if dom_node_is_uninteresting_whitespace_node(child):
return ak.IterationDecision.continue_
var child_object = ak.json_array_serializer_add_object(children_array) else trap("MUST: add_object")
dom_node_serialize_tree_as_json(child, &child_object)
ak.json_object_serializer_finish(&child_object) else trap("MUST: finish")
return ak.IterationDecision.continue_
## Node::serialize_tree_as_json's for_each_attribute lambda: add one attribute to the
## "attributes" object (the context).
func dom_node_serialize_tree_as_json_add_attribute(context: void*?, name: const ak.FlyString*, value: const ak.String*):
let attributes = (ak.JsonObjectSerializer[ak.StringBuilder]*)(context else trap("no attributes object"))
ak.json_object_serializer_add_string(attributes, ak.fly_string_bytes_as_string_view(name), *value) else trap("MUST: add")
## Ported from Node.cpp:2029 Web::DOM::Node::serialize_tree_as_json.
pub func dom_node_serialize_tree_as_json(this: DomNode*, object: ak.JsonObjectSerializer[ak.StringBuilder]*):
let name = dom_node_node_name(this)
ak.json_object_serializer_add_string_view(object, ak.sv("name"), ak.fly_string_bytes_as_string_view(&name)) else trap("MUST: add")
ak.json_object_serializer_add_i64(object, ak.sv("id"), dom_node_unique_id(this)) else trap("MUST: add")
if dom_node_is_document(this):
ak.json_object_serializer_add_string_view(object, ak.sv("type"), ak.sv("document")) else trap("MUST: add")
elif dom_node_is_element(this):
ak.json_object_serializer_add_string_view(object, ak.sv("type"), ak.sv("element")) else trap("MUST: add")
let element = (DomElement*)this
if let namespace_uri = dom_element_namespace_uri(element):
ak.json_object_serializer_add_string_view(object, ak.sv("namespace"), ak.fly_string_bytes_as_string_view(&namespace_uri)) else trap("MUST: add")
if dom_element_has_attributes(element):
var attributes = ak.json_object_serializer_add_object(object, ak.sv("attributes")) else trap("MUST: add_object")
dom_element_for_each_attribute_function2(element, ak.Function2[const ak.FlyString*, const ak.String*, unit](call = dom_node_serialize_tree_as_json_add_attribute, context = &attributes))
ak.json_object_serializer_finish(&attributes) else trap("MUST: finish")
if dom_node_is_navigable_container(element.dom_node()):
let container = (HtmlNavigableContainer*)element
if let content_document = html_navigable_container_content_document(container):
var children = ak.json_object_serializer_add_array(object, ak.sv("children")) else trap("MUST: add_array")
var content_document_object = ak.json_array_serializer_add_object(&children) else trap("MUST: add_object")
dom_node_serialize_tree_as_json(content_document.dom_node(), &content_document_object)
ak.json_object_serializer_finish(&content_document_object) else trap("MUST: finish")
ak.json_array_serializer_finish(&children) else trap("MUST: finish")
if let paintable_box = dom_node_paintable_box(this):
if paint_paintable_box_could_be_scrolled_by_wheel_event(paintable_box):
ak.json_object_serializer_add_bool(object, ak.sv("scrollable"), true) else trap("MUST: add")
if not paint_paintable_is_visible(paintable_box.paint_paintable()):
ak.json_object_serializer_add_bool(object, ak.sv("invisible"), true) else trap("MUST: add")
if paint_paintable_has_stacking_context(paintable_box.paint_paintable()):
ak.json_object_serializer_add_bool(object, ak.sv("stackingContext"), true) else trap("MUST: add")
elif dom_node_is_text(this):
ak.json_object_serializer_add_string_view(object, ak.sv("type"), ak.sv("text")) else trap("MUST: add")
let text_node = (DomText*)this
let data = dom_character_data_data(text_node.dom_character_data())
ak.json_object_serializer_add_string(object, ak.sv("text"), ak.utf16_string_base_to_utf8(&data.utf16_string_base)) else trap("MUST: add")
elif dom_node_is_comment(this):
ak.json_object_serializer_add_string_view(object, ak.sv("type"), ak.sv("comment")) else trap("MUST: add")
let data = dom_character_data_data(((DomComment*)this).dom_character_data())
ak.json_object_serializer_add_string(object, ak.sv("data"), ak.utf16_string_base_to_utf8(&data.utf16_string_base)) else trap("MUST: add")
elif dom_node_is_shadow_root(this):
ak.json_object_serializer_add_string_view(object, ak.sv("type"), ak.sv("shadow-root")) else trap("MUST: add")
let mode = ak.sv("open") if dom_shadow_root_mode((DomShadowRoot*)this) == BindShadowRootMode.open else ak.sv("closed")
ak.json_object_serializer_add_string_view(object, ak.sv("mode"), mode) else trap("MUST: add")
ak.json_object_serializer_add_bool(object, ak.sv("visible"), dom_node_layout_node(this) != none) else trap("MUST: add")
if let element = as_if_dom_element(this):
dom_element_serialize_children_as_json(element, object)
elif dom_node_has_child_nodes(this):
var children = ak.json_object_serializer_add_array(object, ak.sv("children")) else trap("MUST: add_array")
# for_each_child(add_child)
for child in dom_node_children(this):
if dom_node_serialize_child_as_json(this, &children, child) == ak.IterationDecision.break_:
break
ak.json_array_serializer_finish(&children) else trap("MUST: finish")
# mark: Scripting ===============================================================================
## Ported from Node.cpp:2101 Web::DOM::Node::is_scripting_enabled.
# https://html.spec.whatwg.org/multipage/webappapis.html#concept-n-script
pub func dom_node_is_scripting_enabled(this: DomNode*) -> bool:
# Scripting is enabled for a platform object object, when object's scripting is not disabled.
return not dom_node_is_scripting_disabled(this)
## Ported from Node.cpp:2108 Web::DOM::Node::is_scripting_disabled.
# https://html.spec.whatwg.org/multipage/webappapis.html#concept-n-noscript
pub func dom_node_is_scripting_disabled(this: DomNode*) -> bool:
# Scripting is disabled for a node when scripting is not enabled, i.e., when its node document's browsing context is null or when scripting is disabled for its relevant settings object.
return dom_document_browsing_context(dom_node_document(this)) == none or html_is_scripting_disabled(html_relevant_settings_object_dom_node(this))
# mark: Shadow-including tree relations =========================================================
## Ported from Node.cpp:2115 Web::DOM::Node::is_shadow_including_descendant_of.
# https://dom.spec.whatwg.org/#concept-shadow-including-descendant
pub func dom_node_is_shadow_including_descendant_of(this: DomNode*, other: DomNode*) -> bool:
# An object A is a shadow-including descendant of an object B,
# if A is a descendant of B,
if tree_node_is_descendant_of(this, other):
return true
# or A’s root is a shadow root
let root = tree_node_root(this)
if not is_dom_shadow_root(root):
return false
# and A’s root’s host is a shadow-including inclusive descendant of B.
let shadow_root = as_dom_shadow_root(root)
let host = dom_document_fragment_host(shadow_root.dom_document_fragment()) else return false
return dom_node_is_shadow_including_inclusive_descendant_of(host.dom_node(), other)
## Ported from Node.cpp:2132 Web::DOM::Node::is_shadow_including_inclusive_descendant_of.
# https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant
pub func dom_node_is_shadow_including_inclusive_descendant_of(this: DomNode*, other: DomNode*) -> bool:
# A shadow-including inclusive descendant is an object or one of its shadow-including descendants.
return other == this or dom_node_is_shadow_including_descendant_of(this, other)
## Ported from Node.cpp:2139 Web::DOM::Node::is_shadow_including_ancestor_of.
# https://dom.spec.whatwg.org/#concept-shadow-including-ancestor
pub func dom_node_is_shadow_including_ancestor_of(this: DomNode*, other: DomNode*) -> bool:
# An object A is a shadow-including ancestor of an object B, if and only if B is a shadow-including descendant of A.
return dom_node_is_shadow_including_descendant_of(other, this)
## Ported from Node.cpp:2146 Web::DOM::Node::is_shadow_including_inclusive_ancestor_of.
# https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
pub func dom_node_is_shadow_including_inclusive_ancestor_of(this: DomNode*, other: DomNode*) -> bool:
# A shadow-including inclusive ancestor is an object or one of its shadow-including ancestors.
return dom_node_is_shadow_including_inclusive_descendant_of(other, this)
# mark: Replace all =============================================================================
## Ported from Node.cpp:2153 Web::DOM::Node::replace_all.
# https://dom.spec.whatwg.org/#concept-node-replace-all
pub func dom_node_replace_all(this: DomNode*, node: DomNode*?):
# 1. Let removedNodes be parent’s children.
let removed_nodes = dom_node_children_as_vector(this)
# 2. Let addedNodes be the empty set.
var added_nodes: ak.Vector[DomNode*?]
if let new_node = node:
# 3. If node is a DocumentFragment node, then set addedNodes to node’s children.
if is_dom_document_fragment(new_node):
added_nodes = dom_node_children_as_vector(new_node)
# 4. Otherwise, if node is non-null, set addedNodes to « node ».
else:
ak.vector_append_t(&added_nodes, new_node)
# 5. Remove all parent’s children, in tree order, with suppressObservers set to true.
dom_node_remove_all_children(this, true)
# 6. If node is non-null, then insert node into parent before null with suppressObservers set to true.
if let new_node = node:
dom_node_insert_before(this, new_node, none, true)
# 7. If either addedNodes or removedNodes is not empty, then queue a tree mutation record for parent with
# addedNodes, removedNodes, null, and null.
if not ak.vector_is_empty(&added_nodes) or not ak.vector_is_empty(&removed_nodes):
dom_node_queue_tree_mutation_record(this, added_nodes, removed_nodes, none, none)
## Ported from Node.cpp:2185 Web::DOM::Node::string_replace_all.
# https://dom.spec.whatwg.org/#string-replace-all
pub func dom_node_string_replace_all(this: DomNode*, string: ak.Utf16String):
# 1. Let node be null.
var node: DomNode*? = none
# 2. If string is not the empty string, then set node to a new Text node whose data is string and node document is parent’s node document.
if not ak.utf16_string_base_is_empty(&string.utf16_string_base):
node = realm_create_dom_text_utf16_string(bind_platform_object_realm(this.bind_platform_object()), dom_node_document(this), string).dom_node()
# 3. Replace all with node within parent.
dom_node_replace_all(this, node)
# mark: Serialization and unsafely set HTML =====================================================
## Ported from Node.cpp:2199 Web::DOM::Node::serialize_fragment.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-serializing-algorithm-steps
pub func dom_node_serialize_fragment(this: DomNode*, require_well_formed: HtmlRequireWellFormed, fragment_serialization_mode: DomFragmentSerializationMode = DomFragmentSerializationMode.inner) -> ak.Utf16String!:
# 1. Let context document be the value of node's node document.
let context_document = dom_node_document(this)
# 2. If context document is an HTML document, return the result of HTML fragment serialization algorithm with node, false, and « ».
if dom_document_is_html_document(context_document):
var no_shadow_roots: ak.Vector[DomShadowRoot*?]
let html = html_parser_serialize_html_fragment(this, HtmlParserSerializableShadowRoots.no, no_shadow_roots, fragment_serialization_mode)
return ak.utf16_string_from_utf8_string(&html)
# 3. Return the XML serialization of node given require well-formed.
# AD-HOC: XML serialization algorithm returns the "outer" XML serialization of the node.
# For inner, concatenate the serialization of all children.
if fragment_serialization_mode == DomFragmentSerializationMode.inner:
var markup = ak.string_builder_new()
var child = tree_node_first_child(this)
while let child_node = child:
let child_markup = try html_serialize_node_to_xml_string(child_node, require_well_formed)
ak.string_builder_append_string_view(&markup, ak.string_bytes_as_string_view(&child_markup))
child = tree_node_next_sibling(child_node)
let markup_string = ak.string_builder_to_string(&markup) else trap("MUST: StringBuilder::to_string")
return ak.utf16_string_from_utf8_string(&markup_string)
let xml_string = try html_serialize_node_to_xml_string(this, require_well_formed)
return ak.utf16_string_from_utf8_string(&xml_string)
## Ported from Node.cpp:2223 Web::DOM::Node::unsafely_set_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#unsafely-set-html
pub func dom_node_unsafely_set_html(this: DomNode*, context_element: DomElement*, html: ak.StringView) -> !:
# 1. Let newChildren be the result of the HTML fragment parsing algorithm given contextElement, html, and true.
var new_children = try html_parser_parse_html_fragment(context_element, html, HtmlParserAllowDeclarativeShadowRoots.yes)
# 2. Let fragment be a new DocumentFragment whose node document is contextElement’s node document.
let fragment = realm_create_dom_document_fragment(bind_platform_object_realm(this.bind_platform_object()), dom_node_document(context_element.dom_node()))
# 3. For each node in newChildren, append node to fragment.
for maybe_child in ak.vector_span(&new_children):
let child = maybe_child else trap("null GC::Root<Node>")
# I don't know if this can throw here, but let's be safe.
_ = try dom_node_append_child(fragment.dom_node(), child)
# 4. Replace all with fragment within contextElement.
dom_node_replace_all(this, fragment.dom_node())
# mark: Node equality ===========================================================================
## Ported from Node.cpp:2243 Web::DOM::Node::is_same_node.
# https://dom.spec.whatwg.org/#dom-node-issamenode
pub func dom_node_is_same_node(this: DomNode*, other_node: DomNode*?) -> bool:
# The isSameNode(otherNode) method steps are to return true if otherNode is this; otherwise false.
return this == other_node
## The captures of Node::is_equal_node's for_each_attribute lambda.
struct DomNodeIsEqualNodeCompareAttribute:
var other_element: DomElement*
var has_same_attributes: bool
## Node::is_equal_node's for_each_attribute lambda: does B have an attribute equal to this one?
func dom_node_is_equal_node_compare_attribute(context: void*?, attribute: DomAttr*):
let captures = (DomNodeIsEqualNodeCompareAttribute*)(context else trap("no captures"))
let other_value = dom_element_get_attribute_ns(captures.other_element, dom_attr_namespace_uri(attribute), dom_attr_local_name(attribute))
if not dom_node_optional_string_equals(other_value, dom_attr_value(attribute)):
captures.has_same_attributes = false
## Ported from Node.cpp:2250 Web::DOM::Node::is_equal_node.
# https://dom.spec.whatwg.org/#dom-node-isequalnode
pub func dom_node_is_equal_node(this: DomNode*, other_node: DomNode*?) -> bool:
# The isEqualNode(otherNode) method steps are to return true if otherNode is non-null and this equals otherNode; otherwise false.
let other = other_node else return false
# Fast path for testing a node against itself.
if this == other:
return true
# A node A equals a node B if all of the following conditions are true:
# A and B implement the same interfaces.
let this_name = dom_node_node_name(this)
let other_name = dom_node_node_name(other)
if not ak.fly_string_equals_ignoring_ascii_case_fly_string(&this_name, &other_name):
return false
# The following are equal, switching on the interface A implements:
# (`switch (node_type())`, whose `default:` does nothing, as an if-chain over the NodeType.)
let node_type = dom_node_type(this)
if node_type == DomNodeType.document_type_node:
# Its name, public ID, and system ID.
let this_doctype = as_dom_document_type(this)
let other_doctype = as_dom_document_type(other)
let this_name_string = dom_document_type_name(this_doctype)
let this_public_id = dom_document_type_public_id(this_doctype)
let this_system_id = dom_document_type_system_id(this_doctype)
let other_name_string = dom_document_type_name(other_doctype)
let other_public_id = dom_document_type_public_id(other_doctype)
let other_system_id = dom_document_type_system_id(other_doctype)
if not ak.string_equals(&this_name_string, &other_name_string) or not ak.string_equals(&this_public_id, &other_public_id) or not ak.string_equals(&this_system_id, &other_system_id):
return false
elif node_type == DomNodeType.element_node:
# Its namespace, namespace prefix, local name, and its attribute list’s size.
let this_element = as_dom_element(this)
let other_element = as_dom_element(other)
let this_local_name = dom_element_local_name(this_element)
let other_local_name = dom_element_local_name(other_element)
if not dom_node_optional_fly_strings_equal(dom_element_namespace_uri(this_element), dom_element_namespace_uri(other_element)) or not dom_node_optional_fly_strings_equal(dom_element_prefix(this_element), dom_element_prefix(other_element)) or not ak.fly_string_eq_fly_string(&this_local_name, &other_local_name) or dom_element_attribute_list_size(this_element) != dom_element_attribute_list_size(other_element):
return false
# If A is an element, each attribute in its attribute list equals an attribute in B’s attribute list.
var compare_attribute = DomNodeIsEqualNodeCompareAttribute(other_element = other_element, has_same_attributes = true)
dom_element_for_each_attribute_function1(this_element, ak.Function1[DomAttr*, unit](call = dom_node_is_equal_node_compare_attribute, context = &compare_attribute))
if not compare_attribute.has_same_attributes:
return false
elif node_type == DomNodeType.comment_node or node_type == DomNodeType.text_node:
# Its data.
let this_cdata = as_dom_character_data(this)
let other_cdata = as_dom_character_data(other)
let this_data = dom_character_data_data(this_cdata)
let other_data = dom_character_data_data(other_cdata)
if not ak.utf16_string_base_eq_utf16_string_base(&this_data.utf16_string_base, &other_data.utf16_string_base):
return false
elif node_type == DomNodeType.attribute_node:
# Its namespace, local name, and value.
let this_attr = as_dom_attr(this)
let other_attr = as_dom_attr(other)
if not dom_node_optional_fly_strings_equal(dom_attr_namespace_uri(this_attr), dom_attr_namespace_uri(other_attr)):
return false
let this_local_name = dom_attr_local_name(this_attr)
let other_local_name = dom_attr_local_name(other_attr)
if not ak.fly_string_eq_fly_string(&this_local_name, &other_local_name):
return false
let this_value = dom_attr_value(this_attr)
let other_value = dom_attr_value(other_attr)
if not ak.string_equals(&this_value, &other_value):
return false
elif node_type == DomNodeType.processing_instruction_node:
# Its target and data.
let this_processing_instruction = as_dom_processing_instruction(this)
let other_processing_instruction = as_dom_processing_instruction(other)
let this_target = dom_processing_instruction_target(this_processing_instruction)
let other_target = dom_processing_instruction_target(other_processing_instruction)
if not ak.string_equals(&this_target, &other_target):
return false
let this_data = dom_character_data_data(this_processing_instruction.dom_character_data())
let other_data = dom_character_data_data(other_processing_instruction.dom_character_data())
if not ak.utf16_string_base_eq_utf16_string_base(&this_data.utf16_string_base, &other_data.utf16_string_base):
return false
# A and B have the same number of children.
if tree_node_child_count(this) != tree_node_child_count(other):
return false
# Each child of A equals the child of B at the identical index.
var this_child = tree_node_first_child(this)
var other_child = tree_node_first_child(other)
while let this_child_node = this_child:
let other_child_node = other_child else trap("VERIFY: other_child")
if not dom_node_is_equal_node(this_child_node, other_child_node):
return false
this_child = tree_node_next_sibling(this_child_node)
other_child = tree_node_next_sibling(other_child_node)
return true