From 36d267ca40b5c5a54ef165ee1b8233e5f0ac85c7 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 9 Feb 2026 17:38:11 +0800 Subject: [PATCH] Improve Selection compatibility collapse-30.html WPT test from from 2753/5133 to 5133/5133. -Return dom_exception from setPosition -Verify node containment in document --- src/browser/webapi/Node.zig | 7 +++++++ src/browser/webapi/Selection.zig | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig index a21fa568..bcf0ac1b 100644 --- a/src/browser/webapi/Node.zig +++ b/src/browser/webapi/Node.zig @@ -457,6 +457,13 @@ pub fn ownerDocument(self: *const Node, page: *const Page) ?*Document { return page.document; } +pub fn isSameDocumentAs(self: *const Node, other: *const Node, page: *const Page) bool { + // Get the root document for each node + const self_doc = if (self._type == .document) self._type.document else self.ownerDocument(page); + const other_doc = if (other._type == .document) other._type.document else other.ownerDocument(page); + return self_doc == other_doc; +} + pub fn hasChildNodes(self: *const Node) bool { return self.firstChild() != null; } diff --git a/src/browser/webapi/Selection.zig b/src/browser/webapi/Selection.zig index 69054b17..a1613138 100644 --- a/src/browser/webapi/Selection.zig +++ b/src/browser/webapi/Selection.zig @@ -370,6 +370,11 @@ pub fn collapse(self: *Selection, _node: ?*Node, _offset: ?u32, page: *Page) !vo return error.IndexSizeError; } + // If the node is not contained in the document, do not change the selection + if (!page.document.asNode().contains(node)) { + return; + } + const range = try Range.init(page); try range.setStart(node, offset); try range.setEnd(node, offset); @@ -416,7 +421,7 @@ pub const JsApi = struct { pub const removeRange = bridge.function(Selection.removeRange, .{ .dom_exception = true }); pub const selectAllChildren = bridge.function(Selection.selectAllChildren, .{}); pub const setBaseAndExtent = bridge.function(Selection.setBaseAndExtent, .{ .dom_exception = true }); - pub const setPosition = bridge.function(Selection.collapse, .{}); + pub const setPosition = bridge.function(Selection.collapse, .{ .dom_exception = true }); pub const toString = bridge.function(Selection.toString, .{}); };