dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / range_2.lucb
#==============================================================================================
#
# range_2 - DOM::Range: points, stringifying, extracting, containment and inserting
#
# DESCRIPTION:
# Port of LibWeb/DOM/Range.cpp (lines 476-924): intersectsNode, isPointInRange,
# comparePoint, the stringifier, extractContents / "extract", "contained" and
# "partially contained", insertNode / "insert" and surroundContents.
#
#==============================================================================================
# mark: Points ==================================================================================
## Ported from Range.cpp:479 Web::DOM::Range::intersects_node.
# https://dom.spec.whatwg.org/#dom-range-intersectsnode
pub func dom_range_intersects_node(this: DomRange*, node: DomNode*) -> bool:
# 1. If node’s root is different from this’s root, return false.
if tree_node_root(node) != dom_range_root(this):
return false
# 2. Let parent be node’s parent.
# 3. If parent is null, return true.
let parent = tree_node_parent(node) else return true
# 4. Let offset be node’s index.
let offset = (u32)tree_node_index(node)
# 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
let relative_position_to_end = dom_position_of_boundary_point_relative_to_other_boundary_point(DomBoundaryPoint(node = parent, offset = offset), dom_abstract_range_end(this.dom_abstract_range()))
let relative_position_to_start = dom_position_of_boundary_point_relative_to_other_boundary_point(DomBoundaryPoint(node = parent, offset = offset +% 1), dom_abstract_range_start(this.dom_abstract_range()))
if relative_position_to_end == DomRelativeBoundaryPointPosition.before and relative_position_to_start == DomRelativeBoundaryPointPosition.after:
return true
# 6. Return false.
return false
## Ported from Range.cpp:506 Web::DOM::Range::is_point_in_range.
# https://dom.spec.whatwg.org/#dom-range-ispointinrange
pub func dom_range_is_point_in_range(this: DomRange*, node: DomNode*, offset: u32) -> bool!:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. If node’s root is different from this’s root, return false.
if tree_node_root(node) != dom_range_root(this):
return false
# 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if is_dom_document_type(node):
try idl_throw(.idl_dom_exception(idl_invalid_node_type_error_create(realm, ak.utf16_string_from_str("Node cannot be a DocumentType."))))
# 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
if (usize)offset > dom_node_length(node):
try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, dom_range_no_child_at_offset_message(offset))))
# 4. If (node, offset) is before start or after end, return false.
let point = DomBoundaryPoint(node = node, offset = offset)
let relative_position_to_start = dom_position_of_boundary_point_relative_to_other_boundary_point(point, dom_abstract_range_start(this.dom_abstract_range()))
let relative_position_to_end = dom_position_of_boundary_point_relative_to_other_boundary_point(point, dom_abstract_range_end(this.dom_abstract_range()))
if relative_position_to_start == DomRelativeBoundaryPointPosition.before or relative_position_to_end == DomRelativeBoundaryPointPosition.after:
return false
# 5. Return true.
return true
## Ported from Range.cpp:531 Web::DOM::Range::compare_point.
# https://dom.spec.whatwg.org/#dom-range-comparepoint
pub func dom_range_compare_point(this: DomRange*, node: DomNode*, offset: u32) -> i16!:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. If node’s root is different from this’s root, then throw a "WrongDocumentError" DOMException.
if tree_node_root(node) != dom_range_root(this):
try idl_throw(.idl_dom_exception(idl_wrong_document_error_create(realm, ak.utf16_string_from_str("Given node is not in the same document as the range."))))
# 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if is_dom_document_type(node):
try idl_throw(.idl_dom_exception(idl_invalid_node_type_error_create(realm, ak.utf16_string_from_str("Node cannot be a DocumentType."))))
# 3. If offset is greater than node’s length, then throw an "IndexSizeError" DOMException.
if (usize)offset > dom_node_length(node):
try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, dom_range_no_child_at_offset_message(offset))))
# 4. If (node, offset) is before start, return −1.
let point = DomBoundaryPoint(node = node, offset = offset)
let relative_position_to_start = dom_position_of_boundary_point_relative_to_other_boundary_point(point, dom_abstract_range_start(this.dom_abstract_range()))
if relative_position_to_start == DomRelativeBoundaryPointPosition.before:
return -1
# 5. If (node, offset) is after end, return 1.
let relative_position_to_end = dom_position_of_boundary_point_relative_to_other_boundary_point(point, dom_abstract_range_end(this.dom_abstract_range()))
if relative_position_to_end == DomRelativeBoundaryPointPosition.after:
return 1
# 6. Return 0.
return 0
# mark: The stringifier =========================================================================
## to_string's for_each_contained lambda: appends the data of a contained Text node.
func dom_range_to_string_append_text(context: void*?, node: DomNode*) -> ak.IterationDecision:
let builder = (ak.StringBuilder*)(context else trap("no builder"))
if let text_node = as_if_dom_text(node):
let data = dom_character_data_data(text_node.dom_character_data())
ak.string_builder_append_utf16_view(builder, ak.utf16_string_base_utf16_view(&data.utf16_string_base))
return ak.IterationDecision.continue_
## Ported from Range.cpp:560 Web::DOM::Range::to_string.
# https://dom.spec.whatwg.org/#dom-range-stringifier
pub func dom_range_to_string(this: DomRange*) -> ak.Utf16String:
let range = this.dom_abstract_range()
# 1. Let s be the empty string.
var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
# 2. If this’s start node is this’s end node and it is a Text node,
# then return the substring of that Text node’s data beginning at this’s start offset and ending at this’s end offset.
let start_text = as_if_dom_text(dom_abstract_range_start_container(range))
if let text = start_text:
if dom_abstract_range_start_container(range) == dom_abstract_range_end_container(range):
return dom_character_data_substring_data(text.dom_character_data(), (usize)dom_abstract_range_start_offset(range), (usize)(dom_abstract_range_end_offset(range) -% dom_abstract_range_start_offset(range))) else trap("MUST: substring_data")
# 3. If this’s start node is a Text node, then append the substring of that node’s data from this’s start offset until the end to s.
if let text = start_text:
let substring = dom_character_data_substring_data(text.dom_character_data(), (usize)dom_abstract_range_start_offset(range), (usize)(dom_character_data_length_in_utf16_code_units(text.dom_character_data()) -% dom_abstract_range_start_offset(range))) else trap("MUST: substring_data")
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_utf16_view(&substring.utf16_string_base))
# 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
dom_range_for_each_contained(this, ak.Function1[DomNode*, ak.IterationDecision](call = dom_range_to_string_append_text, context = &builder))
# 5. If this’s end node is a Text node, then append the substring of that node’s data from its start until this’s end offset to s.
if let end_text = as_if_dom_text(dom_abstract_range_end_container(range)):
let substring = dom_character_data_substring_data(end_text.dom_character_data(), 0, (usize)dom_abstract_range_end_offset(range)) else trap("MUST: substring_data")
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_utf16_view(&substring.utf16_string_base))
# 6. Return s.
return ak.string_builder_to_utf16_string(&builder)
# mark: Extracting ==============================================================================
## Ported from Range.cpp:591 Web::DOM::Range::extract_contents.
# https://dom.spec.whatwg.org/#dom-range-extractcontents
pub func dom_range_extract_contents(this: DomRange*) -> DomDocumentFragment*!:
return try dom_range_extract(this)
## `as<CharacterData>(*clone).set_data(substring)` after `clone_node()`: a CharacterData clone with the substring.
func dom_range_set_clone_data(clone: DomNode*, data: ak.Utf16String):
dom_character_data_set_data(as_dom_character_data(clone), data)
## Ported from Range.cpp:597 Web::DOM::Range::extract.
# https://dom.spec.whatwg.org/#concept-range-extract
func dom_range_extract(this: DomRange*) -> DomDocumentFragment*!:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. Let fragment be a new DocumentFragment node whose node document is range’s start node’s node document.
let fragment = realm_create_dom_document_fragment(realm, dom_node_document(dom_abstract_range_start_container(this.dom_abstract_range())))
# 2. If range is collapsed, then return fragment.
if dom_abstract_range_collapsed(this.dom_abstract_range()):
return fragment
# 3. Let originalStartNode, originalStartOffset, originalEndNode, and originalEndOffset be range’s start node,
# start offset, end node, and end offset, respectively.
let original_start_node = this.abstract_range.m_start_container
let original_start_offset = this.abstract_range.m_start_offset
let original_end_node = this.abstract_range.m_end_container
let original_end_offset = this.abstract_range.m_end_offset
# 4. If originalStartNode is originalEndNode and it is a CharacterData node:
if original_start_node == original_end_node and is_dom_character_data(original_start_node):
# 1. Let clone be a clone of originalStartNode.
let clone = try dom_node_clone_node(original_start_node)
# 2. Set clone’s data to the result of substringing data of originalStartNode with originalStartOffset and
# originalEndOffset − originalStartOffset.
let result = try dom_character_data_substring_data((DomCharacterData*)original_start_node, (usize)original_start_offset, (usize)(original_end_offset -% original_start_offset))
dom_range_set_clone_data(clone, result)
# 3. Append clone to fragment.
_ = try dom_node_append_child(fragment.dom_node(), clone)
# 4. Replace data of originalStartNode with originalStartOffset, originalEndOffset − originalStartOffset, and
# the empty string.
try dom_character_data_replace_data((DomCharacterData*)original_start_node, (usize)original_start_offset, (usize)(original_end_offset -% original_start_offset), ak.utf16_view_new())
# 5. Return fragment.
return fragment
# 5. Let commonAncestor be originalStartNode.
var common_ancestor = original_start_node
# 6. While commonAncestor is not an inclusive ancestor of originalEndNode: set commonAncestor to its own parent.
while not tree_node_is_inclusive_ancestor_of(common_ancestor, original_end_node):
common_ancestor = dom_node_parent_node(common_ancestor) else trap("VERIFY: parent node")
# 7. Let firstPartiallyContainedChild be null.
var first_partially_contained_child: DomNode*? = none
# 8. If originalStartNode is not an inclusive ancestor of originalEndNode, then set firstPartiallyContainedChild
# to the first child of commonAncestor that is partially contained in range.
if not tree_node_is_inclusive_ancestor_of(original_start_node, original_end_node):
var child_node = tree_node_first_child(common_ancestor)
while let child = child_node:
if dom_range_partially_contains_node(this, child):
first_partially_contained_child = child
break
child_node = tree_node_next_sibling(child)
# 9. Let lastPartiallyContainedChild be null.
var last_partially_contained_child: DomNode*? = none
# 10. If originalEndNode is not an inclusive ancestor of originalStartNode, then set lastPartiallyContainedChild
# to the last child of commonAncestor that is partially contained in range.
if not tree_node_is_inclusive_ancestor_of(original_end_node, original_start_node):
var child_node = tree_node_last_child(common_ancestor)
while let child = child_node:
if dom_range_partially_contains_node(this, child):
last_partially_contained_child = child
break
child_node = tree_node_previous_sibling(child)
# 11. Let containedChildren be a list of all children of commonAncestor that are contained in range, in tree order.
var contained_children: ak.Vector[DomNode*]
for node in dom_node_children(common_ancestor):
if dom_range_contains_node(this, node):
ak.vector_append_t(&contained_children, node)
# 12. If any member of containedChildren is a doctype, then throw a "HierarchyRequestError" DOMException.
for child in ak.vector_span(&contained_children):
if is_dom_document_type(child):
try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(realm, ak.utf16_string_from_str("Contained child is a DocumentType"))))
# 13. Let newNode and newOffset be null.
var new_node: DomNode*? = none
var new_offset: usize = 0
# 14. If originalStartNode is an inclusive ancestor of originalEndNode, then set newNode to originalStartNode and
# newOffset to originalStartOffset.
if tree_node_is_inclusive_ancestor_of(original_start_node, original_end_node):
new_node = original_start_node
new_offset = (usize)original_start_offset
# 15. Otherwise:
else:
# 1. Let referenceNode be originalStartNode.
var reference_node = original_start_node
# 2. While referenceNode’s parent is non-null and is not an inclusive ancestor of originalEndNode:
# set referenceNode to its parent.
while let parent = dom_node_parent_node(reference_node):
if tree_node_is_inclusive_ancestor_of(parent, original_end_node):
break
reference_node = parent
# 3. Set newNode to the parent of referenceNode, and newOffset to referenceNode’s index + 1.
new_node = dom_node_parent_node(reference_node)
new_offset = tree_node_index(reference_node) + 1
# 16. Set range’s start and end to (newNode, newOffset).
let new_node_value = new_node else trap("VERIFY: new node")
try dom_range_set_start(this, new_node_value, (u32)new_offset)
try dom_range_set_end(this, new_node_value, (u32)new_offset)
# 17. If firstPartiallyContainedChild is a CharacterData node:
if first_partially_contained_child != none and is_dom_character_data(first_partially_contained_child else trap("unreachable")):
# 1. Let clone be a clone of originalStartNode.
let clone = try dom_node_clone_node(original_start_node)
# 2. Set the data of clone to the result of substringing data of originalStartNode with originalStartOffset
# and originalStartNode’s length − originalStartOffset.
let result = try dom_character_data_substring_data((DomCharacterData*)original_start_node, (usize)original_start_offset, dom_node_length(original_start_node) -% (usize)original_start_offset)
dom_range_set_clone_data(clone, result)
# 3. Append clone to fragment.
_ = try dom_node_append_child(fragment.dom_node(), clone)
# 4. Replace data of originalStartNode with originalStartOffset, originalStartNode’s length −
# originalStartOffset, and the empty string.
try dom_character_data_replace_data((DomCharacterData*)original_start_node, (usize)original_start_offset, dom_node_length(original_start_node) -% (usize)original_start_offset, ak.utf16_view_new())
# 18. Otherwise, if firstPartiallyContainedChild is non-null:
elif let first_child = first_partially_contained_child:
# 1. Let clone be a clone of firstPartiallyContainedChild.
let clone = try dom_node_clone_node(first_child)
# 2. Append clone to fragment.
_ = try dom_node_append_child(fragment.dom_node(), clone)
# 3. Let subrange be a new live range whose start is (originalStartNode, originalStartOffset) and whose end is
# (firstPartiallyContainedChild, firstPartiallyContainedChild’s length).
let subrange = dom_range_create_dom_node(original_start_node, original_start_offset, first_child, (u32)dom_node_length(first_child))
# 4. Let subfragment be the result of extracting subrange.
let subfragment = try dom_range_extract(subrange)
# 5. Append subfragment to clone.
_ = try dom_node_append_child(clone, subfragment.dom_node())
# 19. For each contained child of containedChildren: append contained child to fragment.
for contained_child in ak.vector_span(&contained_children):
_ = try dom_node_append_child(fragment.dom_node(), contained_child)
# 20. If lastPartiallyContainedChild is a CharacterData node:
if last_partially_contained_child != none and is_dom_character_data(last_partially_contained_child else trap("unreachable")):
# 1. Let clone be a clone of originalEndNode.
let clone = try dom_node_clone_node(original_end_node)
# 2. Set clone’s data to the result of substringing data of originalEndNode with 0 and originalEndOffset.
let result = try dom_character_data_substring_data((DomCharacterData*)original_end_node, 0, (usize)original_end_offset)
dom_range_set_clone_data(clone, result)
# 3. Append clone to fragment.
_ = try dom_node_append_child(fragment.dom_node(), clone)
# 4. Replace data of originalEndNode with 0, originalEndOffset, and the empty string.
try dom_character_data_replace_data(as_dom_character_data(original_end_node), 0, (usize)original_end_offset, ak.utf16_view_new())
# 21. Otherwise, if lastPartiallyContainedChild is non-null:
elif let last_child = last_partially_contained_child:
# 1. Let clone be a clone of lastPartiallyContainedChild.
let clone = try dom_node_clone_node(last_child)
# 2. Append clone to fragment.
_ = try dom_node_append_child(fragment.dom_node(), clone)
# 3. Let subrange be a new live range whose start is (lastPartiallyContainedChild, 0) and whose end is
# (originalEndNode, originalEndOffset).
let subrange = dom_range_create_dom_node(last_child, 0, original_end_node, original_end_offset)
# 4. Let subfragment be the result of extracting subrange.
let subfragment = try dom_range_extract(subrange)
# 5. Append subfragment to clone.
_ = try dom_node_append_child(clone, subfragment.dom_node())
# 22. Return fragment.
return fragment
# mark: Containment =============================================================================
## Ported from Range.cpp:791 Web::DOM::Range::contains_node.
# https://dom.spec.whatwg.org/#contained
pub func dom_range_contains_node(this: DomRange*, node: DomNode*) -> bool:
# A node node is contained in a live range range if node’s root is range’s root,
if tree_node_root(node) != dom_range_root(this):
return false
# and (node, 0) is after range’s start,
if dom_position_of_boundary_point_relative_to_other_boundary_point(DomBoundaryPoint(node = node, offset = 0), dom_abstract_range_start(this.dom_abstract_range())) != DomRelativeBoundaryPointPosition.after:
return false
# and (node, node’s length) is before range’s end.
if dom_position_of_boundary_point_relative_to_other_boundary_point(DomBoundaryPoint(node = node, offset = (u32)dom_node_length(node)), dom_abstract_range_end(this.dom_abstract_range())) != DomRelativeBoundaryPointPosition.before:
return false
return true
## Ported from Range.cpp:809 Web::DOM::Range::partially_contains_node.
# https://dom.spec.whatwg.org/#partially-contained
func dom_range_partially_contains_node(this: DomRange*, node: DomNode*) -> bool:
# A node is partially contained in a live range if it’s an inclusive ancestor of the live range’s start node but
# not its end node, or vice versa.
return tree_node_is_inclusive_ancestor_of(node, this.abstract_range.m_start_container) != tree_node_is_inclusive_ancestor_of(node, this.abstract_range.m_end_container)
# mark: Inserting ===============================================================================
## Ported from Range.cpp:817 Web::DOM::Range::insert_node.
# https://dom.spec.whatwg.org/#dom-range-insertnode
pub func dom_range_insert_node(this: DomRange*, node: DomNode*) -> !:
try dom_range_insert(this, node)
## Ported from Range.cpp:823 Web::DOM::Range::insert.
# https://dom.spec.whatwg.org/#concept-range-insert
func dom_range_insert(this: DomRange*, node: DomNode*) -> !:
let start_container = this.abstract_range.m_start_container
# 1. If range’s start node is a ProcessingInstruction or Comment node, is a Text node whose parent is null, or is node, then throw a "HierarchyRequestError" DOMException.
if (is_dom_processing_instruction(start_container) or is_dom_comment(start_container)) or (is_dom_text(start_container) and dom_node_parent_node(start_container) == none) or start_container == node:
try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Range has inappropriate start node for insertion"))))
# 2. Let referenceNode be null.
var reference_node: DomNode*? = none
# 3. If range’s start node is a Text node, set referenceNode to that Text node.
if is_dom_text(start_container):
reference_node = start_container
# 4. Otherwise, set referenceNode to the child of start node whose index is start offset, and null if there is no such child.
else:
reference_node = tree_node_child_at_index(start_container, (usize)this.abstract_range.m_start_offset)
# 5. Let parent be range’s start node if referenceNode is null, and referenceNode’s parent otherwise.
var parent_node: DomNode*? = none
if let reference = reference_node:
parent_node = tree_node_parent(reference)
else:
parent_node = start_container
let parent = parent_node else trap("VERIFY: parent")
# 6. Ensure pre-insertion validity of node into parent before referenceNode.
try dom_node_ensure_pre_insertion_validity(parent, bind_platform_object_realm(node.bind_platform_object()), node, reference_node)
# 7. If range’s start node is a Text node, set referenceNode to the result of splitting it with offset range’s start offset.
if is_dom_text(this.abstract_range.m_start_container):
reference_node = (try dom_text_split_text((DomText*)this.abstract_range.m_start_container, (usize)this.abstract_range.m_start_offset)).dom_node()
# 8. If node is referenceNode, set referenceNode to its next sibling.
if reference_node == node:
reference_node = tree_node_next_sibling(node)
# 9. If node’s parent is non-null, then remove node.
if tree_node_parent(node) != none:
dom_node_remove(node)
# 10. Let newOffset be parent’s length if referenceNode is null, and referenceNode’s index otherwise.
var new_offset: usize = 0
if let reference = reference_node:
new_offset = tree_node_index(reference)
else:
new_offset = dom_node_length(parent)
# 11. Increase newOffset by node’s length if node is a DocumentFragment node, and one otherwise.
if is_dom_document_fragment(node):
new_offset += dom_node_length(node)
else:
new_offset += 1
# 12. Pre-insert node into parent before referenceNode.
_ = try dom_node_pre_insert(parent, node, reference_node)
# 13. If range is collapsed, then set range’s end to (parent, newOffset).
if dom_abstract_range_collapsed(this.dom_abstract_range()):
try dom_range_set_end(this, parent, (u32)new_offset)
## Ported from Range.cpp:890 Web::DOM::Range::surround_contents.
# https://dom.spec.whatwg.org/#dom-range-surroundcontents
pub func dom_range_surround_contents(this: DomRange*, new_parent: DomNode*) -> !:
let realm = bind_platform_object_realm(this.bind_platform_object())
# 1. If a non-Text node is partially contained in this, then throw an "InvalidStateError" DOMException.
var start_non_text_node: DomNode*? = dom_abstract_range_start_container(this.dom_abstract_range())
if is_dom_text(dom_abstract_range_start_container(this.dom_abstract_range())):
start_non_text_node = dom_node_parent_node(dom_abstract_range_start_container(this.dom_abstract_range()))
var end_non_text_node: DomNode*? = dom_abstract_range_end_container(this.dom_abstract_range())
if is_dom_text(dom_abstract_range_end_container(this.dom_abstract_range())):
end_non_text_node = dom_node_parent_node(dom_abstract_range_end_container(this.dom_abstract_range()))
if start_non_text_node != end_non_text_node:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(realm, ak.utf16_string_from_str("Non-Text node is partially contained in range."))))
# 2. If newParent is a Document, DocumentType, or DocumentFragment node, then throw an "InvalidNodeTypeError" DOMException.
if is_dom_document(new_parent) or is_dom_document_type(new_parent) or is_dom_document_fragment(new_parent):
try idl_throw(.idl_dom_exception(idl_invalid_node_type_error_create(realm, ak.utf16_string_from_str("Invalid parent node type"))))
# 3. Let fragment be the result of extracting this.
let fragment = try dom_range_extract(this)
# 4. If newParent has children, then replace all with null within newParent.
if tree_node_has_children(new_parent):
dom_node_replace_all(new_parent, none)
# 5. Insert newParent into this.
try dom_range_insert(this, new_parent)
# 6. Append fragment to newParent.
_ = try dom_node_append_child(new_parent, fragment.dom_node())
# 7. Select newParent within this.
try dom_range_select(this, new_parent)