Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: insert-adjacent spec semantics; document pre-insert validity

Francis Bouviera603721f5f1e
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index dc26368a8..aeee38879 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -771,9 +771,14 @@ pub fn insertAdjacentElement(
position: []const u8,
element: *Element,
frame: *Frame,
-) !void {
- const target_node, const prev_node = try self.asNode().findAdjacentNodes(position);
+) !?*Element {
+ const target_node, const prev_node = self.asNode().findAdjacentNodes(position, .node) catch |err| switch (err) {
+ // beforebegin/afterend with no parent is a no-op returning null.
+ error.AdjacentNoParent => return null,
+ else => return err,
+ };
_ = try target_node.insertBefore(element.asNode(), prev_node, frame);
+ return element;
}
pub fn insertAdjacentText(
@@ -782,8 +787,12 @@ pub fn insertAdjacentText(
data: []const u8,
frame: *Frame,
) !void {
+ const target_node, const prev_node = self.asNode().findAdjacentNodes(where, .node) catch |err| switch (err) {
+ // beforebegin/afterend with no parent is a no-op.
+ error.AdjacentNoParent => return,
+ else => return err,
+ };
const text_node = try Frame.node_factory.createTextNode(frame, data);
- const target_node, const prev_node = try self.asNode().findAdjacentNodes(where);
_ = try target_node.insertBefore(text_node, prev_node, frame);
}
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 397a18102..302708076 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -123,11 +123,17 @@ pub fn is(self: *Node, comptime T: type) ?*T {
return null;
}
+/// Which "insert adjacent" flavor is asking. insertAdjacentHTML throws
+/// NoModificationAllowedError for a null or document parent, while
+/// insertAdjacentElement/Text return null for a null parent and otherwise
+/// rely on the pre-insert validity checks (HierarchyRequestError).
+pub const AdjacentVariant = enum { html, node };
+
/// Given a position, returns target and previous nodes required for
/// insertAdjacentHTML, insertAdjacentElement and insertAdjacentText.
/// * `target_node` is `*Node` (where we actually insert),
/// * `previous_node` is `?*Node`.
-pub fn findAdjacentNodes(self: *Node, position: []const u8) !struct { *Node, ?*Node } {
+pub fn findAdjacentNodes(self: *Node, position: []const u8, variant: AdjacentVariant) !struct { *Node, ?*Node } {
// Case-insensitive match per HTML spec.
// "beforeend" was the most common case in my tests; we might adjust the order
// depending on which ones websites prefer most.
@@ -142,11 +148,16 @@ pub fn findAdjacentNodes(self: *Node, position: []const u8) !struct { *Node, ?*N
if (std.ascii.eqlIgnoreCase(position, "beforebegin")) {
// The node must have a parent node in order to use this variant.
- const parent_node = self.parentNode() orelse return error.NoModificationAllowed;
- // Parent cannot be Document.
- switch (parent_node._type) {
- .document, .document_fragment => return error.NoModificationAllowed,
- else => {},
+ const parent_node = self.parentNode() orelse switch (variant) {
+ .html => return error.NoModificationAllowed,
+ .node => return error.AdjacentNoParent,
+ };
+ if (variant == .html) {
+ // Parent cannot be Document.
+ switch (parent_node._type) {
+ .document, .document_fragment => return error.NoModificationAllowed,
+ else => {},
+ }
}
return .{ parent_node, self };
@@ -154,11 +165,16 @@ pub fn findAdjacentNodes(self: *Node, position: []const u8) !struct { *Node, ?*N
if (std.ascii.eqlIgnoreCase(position, "afterend")) {
// The node must have a parent node in order to use this variant.
- const parent_node = self.parentNode() orelse return error.NoModificationAllowed;
- // Parent cannot be Document.
- switch (parent_node._type) {
- .document, .document_fragment => return error.NoModificationAllowed,
- else => {},
+ const parent_node = self.parentNode() orelse switch (variant) {
+ .html => return error.NoModificationAllowed,
+ .node => return error.AdjacentNoParent,
+ };
+ if (variant == .html) {
+ // Parent cannot be Document.
+ switch (parent_node._type) {
+ .document, .document_fragment => return error.NoModificationAllowed,
+ else => {},
+ }
}
// Get the next sibling or null; null indicates our node is the only one.
@@ -220,6 +236,36 @@ fn validateNodeInsertion(parent: *Node, node: *Node) !void {
}
}
+// DOM "ensure pre-insert validity" rules specific to document parents: a
+// document can't contain text nodes and has at most one element child.
+// `replaced_child` is the node about to be replaced (replaceChild), which
+// doesn't count against the single-element rule. Not part of
+// validateNodeInsertion because replaceChildren replaces every existing
+// child and must skip the single-element rule entirely.
+fn validateDocumentInsertion(parent: *Node, node: *const Node, replaced_child: ?*const Node) !void {
+ if (parent._type != .document) {
+ return;
+ }
+ switch (node._type) {
+ .cdata => |cd| {
+ if (cd._type == .text) {
+ // A Text node cannot be a child of a document.
+ return error.HierarchyError;
+ }
+ },
+ .element => {
+ var it = parent.childrenIterator();
+ while (it.next()) |existing| {
+ if (existing._type == .element and existing != node and existing != replaced_child) {
+ // A document can have at most one element child.
+ return error.HierarchyError;
+ }
+ }
+ },
+ else => {},
+ }
+}
+
pub fn appendChild(self: *Node, child: *Node, frame: *Frame) !*Node {
if (child.is(DocumentFragment)) |_| {
try frame.appendAllChildren(child, self);
@@ -227,6 +273,7 @@ pub fn appendChild(self: *Node, child: *Node, frame: *Frame) !*Node {
}
try validateNodeInsertion(self, child);
+ try validateDocumentInsertion(self, child, null);
frame.domChanged();
@@ -628,6 +675,12 @@ pub fn removeChild(self: *Node, child: *Node, frame: *Frame) !*Node {
}
pub fn insertBefore(self: *Node, new_node: *Node, ref_node_: ?*Node, frame: *Frame) !*Node {
+ return self.insertBeforeReplacing(new_node, ref_node_, null, frame);
+}
+
+// `replacing` is the child replaceChild is about to remove; it doesn't count
+// against the document single-element rule.
+fn insertBeforeReplacing(self: *Node, new_node: *Node, ref_node_: ?*Node, replacing: ?*const Node, frame: *Frame) !*Node {
const ref_node = ref_node_ orelse {
return self.appendChild(new_node, frame);
};
@@ -657,6 +710,7 @@ pub fn insertBefore(self: *Node, new_node: *Node, ref_node_: ?*Node, frame: *Fra
}
try validateNodeInsertion(self, new_node);
+ try validateDocumentInsertion(self, new_node, replacing);
const child_already_connected = new_node.isConnected();
@@ -695,8 +749,9 @@ pub fn replaceChild(self: *Node, new_child: *Node, old_child: *Node, frame: *Fra
}
try validateNodeInsertion(self, new_child);
+ try validateDocumentInsertion(self, new_child, old_child);
- _ = try self.insertBefore(new_child, old_child, frame);
+ _ = try self.insertBeforeReplacing(new_child, old_child, old_child, frame);
// Special case: if we replace a node by itself, insertBefore was a noop.
if (new_child != old_child) {
diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zig
index 2caf21a2c..3e62aa99b 100644
--- a/src/browser/webapi/element/Html.zig
+++ b/src/browser/webapi/element/Html.zig
@@ -287,7 +287,7 @@ pub fn insertAdjacentHTML(
const fragment = (try DocumentFragment.init(frame)).asNode();
try frame.parseHtmlAsChildren(fragment, html);
- const target_node, const prev_node = try self.asElement().asNode().findAdjacentNodes(position);
+ const target_node, const prev_node = try self.asElement().asNode().findAdjacentNodes(position, .html);
var iter = fragment.childrenIterator();
while (iter.next()) |child_node| {