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
This commit is contained in:
Karl Seguin
2026-02-09 17:38:11 +08:00
parent 2e5d04389b
commit 36d267ca40
2 changed files with 13 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -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, .{});
};