1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

LibWeb: Implement Selection.collapse

This commit is contained in:
Rafał Babiarz 2022-12-09 21:57:10 +01:00 committed by Andreas Kling
parent 4cdfe684b8
commit 6d46ebfe8a
2 changed files with 30 additions and 7 deletions

View file

@ -162,17 +162,40 @@ void Selection::empty()
}
// https://w3c.github.io/selection-api/#dom-selection-collapse
void Selection::collapse(JS::GCPtr<DOM::Node>, unsigned offset)
WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigned offset)
{
(void)offset;
TODO();
// 1. If node is null, this method must behave identically as removeAllRanges() and abort these steps.
if (!node) {
remove_all_ranges();
return {};
}
// 2. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
if (offset > node->length()) {
return WebIDL::IndexSizeError::create(realm(), "Selection.collapse() with offset longer than node's length"sv);
}
// 3. If node's root is not the document associated with this, abort these steps.
if (&node->root() != m_document.ptr())
return {};
// 4. Otherwise, let newRange be a new range.
auto new_range = DOM::Range::create(*m_document);
// 5. Set the start the start and the end of newRange to (node, offset).
TRY(new_range->set_start(*node, offset));
// 6. Set this's range to newRange.
m_range = new_range;
return {};
}
// https://w3c.github.io/selection-api/#dom-selection-setposition
void Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
WebIDL::ExceptionOr<void> Selection::set_position(JS::GCPtr<DOM::Node> node, unsigned offset)
{
// The method must be an alias, and behave identically, to collapse().
collapse(node, offset);
return collapse(node, offset);
}
// https://w3c.github.io/selection-api/#dom-selection-collapsetostart