From 13bd52249d3fe674b8c11049eb66a305e058ee6f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 16 Nov 2024 10:04:27 +0100 Subject: [PATCH] LibWeb: Make Selection APIs throw on DocumentType node inputs This matches the behavior of all major engines, and is covered by hundreds of subtests in WPT. Spec PR: https://github.com/w3c/selection-api/pull/342 --- Libraries/LibWeb/Selection/Selection.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Libraries/LibWeb/Selection/Selection.cpp b/Libraries/LibWeb/Selection/Selection.cpp index b29f6e5e7c8a..3ff7b6edaf07 100644 --- a/Libraries/LibWeb/Selection/Selection.cpp +++ b/Libraries/LibWeb/Selection/Selection.cpp @@ -196,6 +196,12 @@ WebIDL::ExceptionOr Selection::collapse(GC::Ptr 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()) { + 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"_string); @@ -361,6 +367,12 @@ WebIDL::ExceptionOr Selection::set_base_and_extent(GC::Ref anch // https://w3c.github.io/selection-api/#dom-selection-selectallchildren WebIDL::ExceptionOr Selection::select_all_children(GC::Ref 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 {};