dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / selection / selection_1.lucb
#==============================================================================================
#
# selection_1 - Selection::Selection: the selection's range, anchor and focus
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Selection/Selection.cpp lines 1-560: construction, the anchor
# and focus, the range accessors and the Selection API's methods (addRange, collapse,
# extend, setBaseAndExtent, selectAllChildren, modify, containsNode, the stringifier)
# with set_range, which keeps the range's back pointer and the editing overrides. The
# JS bindings over them are phase 3.
#
# Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from Selection.cpp:24 Web::Selection::Selection::create.
pub func selection_selection_create(realm: Realm*, document: DomDocument*) -> SelectionSelection*:
return realm_create_selection_selection(realm, realm, document)
## Ported from Selection.cpp:29 Web::Selection::Selection::Selection.
pub func selection_selection_construct(this: SelectionSelection*, realm: Realm*, document: DomDocument*):
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
this.m_document = document
this.m_direction = SelectionSelectionDirection.directionless
## Ported from Selection.cpp:37 Web::Selection::Selection::initialize (override).
pub func selection_selection_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.selection)
js_cell_initialize_impl(base, realm)
## Ported from Selection.cpp:54 Web::Selection::Selection::visit_edges (override).
pub func selection_selection_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (SelectionSelection*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_range)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_document)
## Ported from Selection.cpp:44 Web::Selection::Selection::is_empty.
## https://w3c.github.io/selection-api/#dfn-empty
func selection_selection_is_empty(this: SelectionSelection*) -> bool:
# Each selection can be associated with a single range.
# When there is no range associated with the selection, the selection is empty.
# The selection must be initially empty.
return this.m_range == none
## The realm the selection lives in (PlatformObject::realm).
func selection_selection_realm(this: SelectionSelection*) -> Realm*:
return bind_platform_object_realm(this.bind_platform_object())
# mark: Anchor and focus ========================================================================
## Ported from Selection.cpp:62 Web::Selection::Selection::anchor_node.
## https://w3c.github.io/selection-api/#dfn-anchor
pub func selection_selection_anchor_node(this: SelectionSelection*) -> DomNode*?:
let range = this.m_range else return none
if this.m_direction == SelectionSelectionDirection.forwards:
return dom_abstract_range_start_container(range.dom_abstract_range())
return dom_abstract_range_end_container(range.dom_abstract_range())
## Ported from Selection.cpp:72 Web::Selection::Selection::anchor_offset.
## https://w3c.github.io/selection-api/#dfn-anchor
pub func selection_selection_anchor_offset(this: SelectionSelection*) -> u32:
let range = this.m_range else return 0
if this.m_direction == SelectionSelectionDirection.forwards:
return dom_abstract_range_start_offset(range.dom_abstract_range())
return dom_abstract_range_end_offset(range.dom_abstract_range())
## Ported from Selection.cpp:82 Web::Selection::Selection::focus_node.
## https://w3c.github.io/selection-api/#dfn-focus
pub func selection_selection_focus_node(this: SelectionSelection*) -> DomNode*?:
let range = this.m_range else return none
if this.m_direction == SelectionSelectionDirection.forwards:
return dom_abstract_range_end_container(range.dom_abstract_range())
return dom_abstract_range_start_container(range.dom_abstract_range())
## Ported from Selection.cpp:92 Web::Selection::Selection::focus_offset.
## https://w3c.github.io/selection-api/#dfn-focus
pub func selection_selection_focus_offset(this: SelectionSelection*) -> u32:
let range = this.m_range else return 0
if this.m_direction == SelectionSelectionDirection.forwards:
return dom_abstract_range_end_offset(range.dom_abstract_range())
return dom_abstract_range_start_offset(range.dom_abstract_range())
## Ported from Selection.cpp:102 Web::Selection::Selection::is_collapsed.
## https://w3c.github.io/selection-api/#dom-selection-iscollapsed
pub func selection_selection_is_collapsed(this: SelectionSelection*) -> bool:
# The attribute must return true if and only if the anchor and focus are the same
# (including if both are null). Otherwise it must return false.
let range = this.m_range else return true
let abstract_range = range.dom_abstract_range()
return selection_selection_anchor_node(this) == selection_selection_focus_node(this) and dom_abstract_range_start_offset(abstract_range) == dom_abstract_range_end_offset(abstract_range)
## Ported from Selection.cpp:113 Web::Selection::Selection::range_count.
## https://w3c.github.io/selection-api/#dom-selection-rangecount
pub func selection_selection_range_count(this: SelectionSelection*) -> u32:
if this.m_range != none:
return 1
return 0
## Ported from Selection.cpp:120 Web::Selection::Selection::type.
pub func selection_selection_type(this: SelectionSelection*) -> ak.String:
let range = this.m_range else return ak.string_from_str("None")
if dom_abstract_range_collapsed(range.dom_abstract_range()):
return ak.string_from_str("Caret")
return ak.string_from_str("Range")
## Ported from Selection.cpp:129 Web::Selection::Selection::direction.
pub func selection_selection_direction(this: SelectionSelection*) -> ak.String:
if this.m_range == none or this.m_direction == SelectionSelectionDirection.directionless:
return ak.string_from_str("none")
if this.m_direction == SelectionSelectionDirection.forwards:
return ak.string_from_str("forward")
return ak.string_from_str("backward")
# mark: Ranges ==================================================================================
## Ported from Selection.cpp:139 Web::Selection::Selection::get_range_at.
## https://w3c.github.io/selection-api/#dom-selection-getrangeat
pub func selection_selection_get_range_at(this: SelectionSelection*, index: u32) -> DomRange*?!:
let focus = selection_selection_focus_node(this)
let anchor = selection_selection_anchor_node(this)
# The method must throw an IndexSizeError exception if index is not 0, or if this is empty or either focus or anchor is not in the document tree.
var is_focus_in_document_tree = false
if let focus_node = focus:
is_focus_in_document_tree = dom_node_document(focus_node) == this.m_document
var is_anchor_in_document_tree = false
if let anchor_node = anchor:
is_anchor_in_document_tree = dom_node_document(anchor_node) == this.m_document
if index != 0 or selection_selection_is_empty(this) or not is_focus_in_document_tree or not is_anchor_in_document_tree:
try idl_throw(.idl_dom_exception(idl_index_size_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.getRangeAt() on empty Selection or with invalid argument"))))
# Otherwise, it must return a reference to (not a copy of) this's range.
return this.m_range
## Ported from Selection.cpp:155 Web::Selection::Selection::add_range.
## https://w3c.github.io/selection-api/#dom-selection-addrange
pub func selection_selection_add_range(this: SelectionSelection*, range: DomRange*):
# 1. If the root of the range's boundary points are not the document associated with this, abort these steps.
if tree_node_root(dom_abstract_range_start_container(range.dom_abstract_range())) != this.m_document.dom_node():
return
# 2. If rangeCount is not 0, abort these steps.
if selection_selection_range_count(this) != 0:
return
# 3. Set this's range to range by a strong reference (not by making a copy).
selection_selection_set_range(this, range)
# AD-HOC: WPT selection/removeAllRanges.html and selection/addRange.htm expect this
this.m_direction = SelectionSelectionDirection.forwards
## Ported from Selection.cpp:173 Web::Selection::Selection::remove_range.
## https://w3c.github.io/selection-api/#dom-selection-removerange
pub func selection_selection_remove_range(this: SelectionSelection*, range: DomRange*) -> !:
# The method must make this empty by disassociating its range if this's range is range.
if this.m_range == range:
selection_selection_set_range(this, none)
return
# Otherwise, it must throw a NotFoundError.
try idl_throw(.idl_dom_exception(idl_not_found_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.removeRange() with invalid argument"))))
## Ported from Selection.cpp:186 Web::Selection::Selection::remove_all_ranges.
## https://w3c.github.io/selection-api/#dom-selection-removeallranges
pub func selection_selection_remove_all_ranges(this: SelectionSelection*):
# The method must make this empty by disassociating its range if this has an associated range.
selection_selection_set_range(this, none)
## Ported from Selection.cpp:193 Web::Selection::Selection::empty.
## https://w3c.github.io/selection-api/#dom-selection-empty
pub func selection_selection_empty(this: SelectionSelection*):
# The method must be an alias, and behave identically, to removeAllRanges().
selection_selection_remove_all_ranges(this)
# mark: Collapsing ==============================================================================
## Ported from Selection.cpp:200 Web::Selection::Selection::collapse.
## https://w3c.github.io/selection-api/#dom-selection-collapse
pub func selection_selection_collapse(this: SelectionSelection*, node: DomNode*?, offset: u32) -> !:
# 1. If node is null, this method must behave identically as removeAllRanges() and abort these steps.
if node == none:
selection_selection_remove_all_ranges(this)
return
let target = node else trap("unreachable")
# 2. If node is a DocumentType, throw an InvalidNodeTypeError exception and abort these steps.
if dom_node_is_document_type(target):
try idl_throw(.idl_dom_exception(idl_invalid_node_type_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.collapse() with DocumentType node"))))
# 3. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
if (usize)offset > dom_node_length(target):
try idl_throw(.idl_dom_exception(idl_index_size_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.collapse() with offset longer than node's length"))))
# 4. If document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
if not dom_node_is_shadow_including_inclusive_ancestor_of(this.m_document.dom_node(), target):
return
# 5. Otherwise, let newRange be a new range.
let new_range = dom_range_create_dom_document(this.m_document)
# 6. Set the start the start and the end of newRange to (node, offset).
try dom_range_set_start(new_range, target, offset)
# 7. Set this's range to newRange.
selection_selection_set_range(this, new_range)
## Ported from Selection.cpp:232 Web::Selection::Selection::set_position.
## https://w3c.github.io/selection-api/#dom-selection-setposition
pub func selection_selection_set_position(this: SelectionSelection*, node: DomNode*?, offset: u32) -> !:
# The method must be an alias, and behave identically, to collapse().
try selection_selection_collapse(this, node, offset)
## Ported from Selection.cpp:239 and :258 Web::Selection::Selection::collapse_to_start and
## collapse_to_end: a new range collapsed to one end of this's range.
## https://w3c.github.io/selection-api/#dom-selection-collapsetostart
func selection_selection_collapse_to_boundary(this: SelectionSelection*, to_start: bool, name: str) -> !:
# 1. The method must throw InvalidStateError exception if the this is empty.
if this.m_range == none:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(selection_selection_realm(this), ak.utf16_string_from_str(name))))
let range = this.m_range else trap("unreachable")
# 2. Otherwise, it must create a new range
let new_range = dom_range_create_dom_document(this.m_document)
# 3. Set the start both its start and end to the start of this's range
let boundary = dom_abstract_range_start(range.dom_abstract_range()) if to_start else dom_abstract_range_end(range.dom_abstract_range())
try dom_range_set_start(new_range, boundary.node, boundary.offset)
try dom_range_set_end(new_range, boundary.node, boundary.offset)
# 4. Then set this's range to the newly-created range.
selection_selection_set_range(this, new_range)
## Ported from Selection.cpp:239 Web::Selection::Selection::collapse_to_start.
pub func selection_selection_collapse_to_start(this: SelectionSelection*) -> !:
try selection_selection_collapse_to_boundary(this, true, "Selection.collapse_to_start() on empty range")
## Ported from Selection.cpp:258 Web::Selection::Selection::collapse_to_end.
pub func selection_selection_collapse_to_end(this: SelectionSelection*) -> !:
try selection_selection_collapse_to_boundary(this, false, "Selection.collapse_to_end() on empty range")
# mark: Extending ===============================================================================
## Ported from Selection.cpp:278 Web::Selection::Selection::extend.
## https://w3c.github.io/selection-api/#dom-selection-extend
pub func selection_selection_extend(this: SelectionSelection*, node: DomNode*, offset: u32) -> !:
# 1. If the document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
if not dom_node_is_shadow_including_inclusive_ancestor_of(this.m_document.dom_node(), node):
return
# 2. If this is empty, throw an InvalidStateError exception and abort these steps.
if this.m_range == none:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.extend() on empty range"))))
let range = this.m_range else trap("unreachable")
# 3. Let oldAnchor and oldFocus be the this's anchor and focus, and let newFocus be the boundary point (node, offset).
let old_anchor = DomBoundaryPoint(node = selection_selection_anchor_node(this) else trap("a selection with a range has an anchor"), offset = selection_selection_anchor_offset(this))
let new_focus = DomBoundaryPoint(node = node, offset = offset)
# 4. Let newRange be a new range.
let new_range = dom_range_create_dom_document(this.m_document)
# 5. If node's root is not the same as the this's range's root, set the start newRange's start and end to newFocus.
if tree_node_root(node) != tree_node_root(dom_abstract_range_start_container(range.dom_abstract_range())):
try dom_range_set_start(new_range, new_focus.node, new_focus.offset)
try dom_range_set_end(new_range, new_focus.node, new_focus.offset)
# 6. Otherwise, if oldAnchor is before or equal to newFocus, set the start newRange's start to oldAnchor, then set its end to newFocus.
elif dom_position_of_boundary_point_relative_to_other_boundary_point(old_anchor, new_focus) != DomRelativeBoundaryPointPosition.after:
try dom_range_set_start(new_range, old_anchor.node, old_anchor.offset)
try dom_range_set_end(new_range, new_focus.node, new_focus.offset)
# 7. Otherwise, set the start newRange's start to newFocus, then set its end to oldAnchor.
else:
try dom_range_set_start(new_range, new_focus.node, new_focus.offset)
try dom_range_set_end(new_range, old_anchor.node, old_anchor.offset)
# 8. Set this's range to newRange.
selection_selection_set_range(this, new_range)
# 9. If newFocus is before oldAnchor, set this's direction to backwards. Otherwise, set it to forwards.
if dom_position_of_boundary_point_relative_to_other_boundary_point(new_focus, old_anchor) == DomRelativeBoundaryPointPosition.before:
this.m_direction = SelectionSelectionDirection.backwards
else:
this.m_direction = SelectionSelectionDirection.forwards
## Ported from Selection.cpp:329 Web::Selection::Selection::set_base_and_extent.
## https://w3c.github.io/selection-api/#dom-selection-setbaseandextent
pub func selection_selection_set_base_and_extent(this: SelectionSelection*, anchor_node: DomNode*, anchor_offset: u32, focus_node: DomNode*, focus_offset: u32) -> !:
# 1. If anchorOffset is longer than anchorNode's length or if focusOffset is longer than focusNode's length, throw an IndexSizeError exception and abort these steps.
if (usize)anchor_offset > dom_node_length(anchor_node):
try idl_throw(.idl_dom_exception(idl_index_size_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Anchor offset points outside of the anchor node"))))
if (usize)focus_offset > dom_node_length(focus_node):
try idl_throw(.idl_dom_exception(idl_index_size_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Focus offset points outside of the focus node"))))
# 2. If document associated with this is not a shadow-including inclusive ancestor of anchorNode or focusNode, abort these steps.
let document = this.m_document.dom_node()
if not dom_node_is_shadow_including_inclusive_ancestor_of(document, anchor_node) or not dom_node_is_shadow_including_inclusive_ancestor_of(document, focus_node):
return
# 3. Let anchor be the boundary point (anchorNode, anchorOffset) and let focus be the boundary point (focusNode, focusOffset).
let anchor = DomBoundaryPoint(node = anchor_node, offset = anchor_offset)
let focus = DomBoundaryPoint(node = focus_node, offset = focus_offset)
# 4. Let newRange be a new range.
let new_range = dom_range_create_dom_document(this.m_document)
# 5. If anchor is before focus, set the start the newRange's start to anchor and its end to focus. Otherwise, set the start them to focus and anchor respectively.
let position_of_anchor_relative_to_focus = dom_position_of_boundary_point_relative_to_other_boundary_point(anchor, focus)
if position_of_anchor_relative_to_focus == DomRelativeBoundaryPointPosition.before:
try dom_range_set_start(new_range, anchor_node, anchor_offset)
try dom_range_set_end(new_range, focus_node, focus_offset)
else:
try dom_range_set_start(new_range, focus_node, focus_offset)
try dom_range_set_end(new_range, anchor_node, anchor_offset)
# 6. Set this's range to newRange.
selection_selection_set_range(this, new_range)
# 7. If focus is before anchor, set this's direction to backwards. Otherwise, set it to forwards
# NOTE: "Otherwise" can be seen as "focus is equal to or after anchor".
if position_of_anchor_relative_to_focus == DomRelativeBoundaryPointPosition.after:
this.m_direction = SelectionSelectionDirection.backwards
else:
this.m_direction = SelectionSelectionDirection.forwards
## Ported from Selection.cpp:372 Web::Selection::Selection::select_all_children.
## https://w3c.github.io/selection-api/#dom-selection-selectallchildren
pub func selection_selection_select_all_children(this: SelectionSelection*, node: DomNode*) -> !:
# 1. If node is a DocumentType, throw an InvalidNodeTypeError exception and abort these steps.
if dom_node_is_document_type(node):
try idl_throw(.idl_dom_exception(idl_invalid_node_type_error_create(selection_selection_realm(this), ak.utf16_string_from_str("Selection.selectAllChildren() with DocumentType node"))))
# 2. If node's root is not the document associated with this, abort these steps.
if tree_node_root(node) != this.m_document.dom_node():
return
# 3. Let newRange be a new range and childCount be the number of children of node.
let new_range = dom_range_create_dom_document(this.m_document)
let child_count = tree_node_child_count(node)
# 4. Set newRange's start to (node, 0).
try dom_range_set_start(new_range, node, 0)
# 5. Set newRange's end to (node, childCount).
try dom_range_set_end(new_range, node, (u32)child_count)
# 6. Set this's range to newRange.
selection_selection_set_range(this, new_range)
# 7. Set this's direction to forwards.
this.m_direction = SelectionSelectionDirection.forwards