Skip to content

Commit

Permalink
LibWeb: Make Selection APIs throw on DocumentType node inputs
Browse files Browse the repository at this point in the history
This matches the behavior of all major engines, and is covered by
hundreds of subtests in WPT.

Spec PR: w3c/selection-api#342

(cherry picked from commit 13bd52249d3fe674b8c11049eb66a305e058ee6f)
  • Loading branch information
awesomekling authored and nico committed Nov 17, 2024
1 parent 5b8629f commit 246dc6f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Userland/Libraries/LibWeb/Selection/Selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ WebIDL::ExceptionOr<void> Selection::collapse(JS::GCPtr<DOM::Node> node, unsigne
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()) {
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()) {
return WebIDL::IndexSizeError::create(realm(), "Selection.collapse() with offset longer than node's length"_fly_string);
Expand Down Expand Up @@ -355,6 +361,12 @@ WebIDL::ExceptionOr<void> Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::N
// https://w3c.github.io/selection-api/#dom-selection-selectallchildren
WebIDL::ExceptionOr<void> Selection::select_all_children(JS::NonnullGCPtr<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()) {
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.
if (&node->root() != m_document.ptr())
return {};
Expand Down

0 comments on commit 246dc6f

Please sign in to comment.