LibWeb: Update spec steps in Selection

Resolves two FIXMEs in `::collapse()` and `::select_all_children()`.
This commit is contained in:
Jelle Raaijmakers 2024-12-11 16:43:01 +01:00 committed by Jelle Raaijmakers
parent 943ec820fc
commit d14fd8a6c8
Notes: github-actions[bot] 2024-12-11 18:12:15 +00:00

View file

@ -196,28 +196,25 @@ WebIDL::ExceptionOr<void> Selection::collapse(GC::Ptr<DOM::Node> node, unsigned
return {};
}
// FIXME: Update this to match the spec once the spec is updated.
// Spec PR: https://github.com/w3c/selection-api/pull/342
if (node->is_document_type()) {
// 2. If node is a DocumentType, throw an InvalidNodeTypeError exception and abort these steps.
if (node->is_document_type())
return WebIDL::InvalidNodeTypeError::create(realm(), "Selection.collapse() with DocumentType node"_string);
}
// 2. The method must throw an IndexSizeError exception if offset is longer than node's length and abort these steps.
if (offset > node->length()) {
// 3. 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"_string);
}
// 3. If document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
// 4. If document associated with this is not a shadow-including inclusive ancestor of node, abort these steps.
if (!m_document->is_shadow_including_inclusive_ancestor_of(*node))
return {};
// 4. Otherwise, let newRange be a new range.
// 5. 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).
// 6. 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.
// 7. Set this's range to newRange.
set_range(new_range);
return {};
@ -367,30 +364,28 @@ WebIDL::ExceptionOr<void> Selection::set_base_and_extent(GC::Ref<DOM::Node> anch
// https://w3c.github.io/selection-api/#dom-selection-selectallchildren
WebIDL::ExceptionOr<void> Selection::select_all_children(GC::Ref<DOM::Node> node)
{
// FIXME: Update this to match the spec once the spec is updated.
// Spec PR: https://github.com/w3c/selection-api/pull/342
if (node->is_document_type()) {
// 1. If node is a DocumentType, throw an InvalidNodeTypeError exception and abort these steps.
if (node->is_document_type())
return WebIDL::InvalidNodeTypeError::create(realm(), "Selection.selectAllChildren() with DocumentType node"_string);
}
// 1. If node's root is not the document associated with this, abort these steps.
// 2. If node's root is not the document associated with this, abort these steps.
if (&node->root() != m_document.ptr())
return {};
// 2. Let newRange be a new range and childCount be the number of children of node.
// 3. Let newRange be a new range and childCount be the number of children of node.
auto new_range = DOM::Range::create(*m_document);
auto child_count = node->child_count();
// 3. Set newRange's start to (node, 0).
// 4. Set newRange's start to (node, 0).
TRY(new_range->set_start(node, 0));
// 4. Set newRange's end to (node, childCount).
// 5. Set newRange's end to (node, childCount).
TRY(new_range->set_end(node, child_count));
// 5. Set this's range to newRange.
// 6. Set this's range to newRange.
set_range(new_range);
// 6. Set this's direction to forwards.
// 7. Set this's direction to forwards.
m_direction = Direction::Forwards;
return {};