Skip to content

Lightpanda: the headless browser designed for AI and automation

Extract common logic

Karl Seguin978636f7be9c
diff --git a/src/browser/webapi/CData.zig b/src/browser/webapi/CData.zig
index 13e86604e..7e58e2948 100644
--- a/src/browser/webapi/CData.zig
+++ b/src/browser/webapi/CData.zig
@@ -40,6 +40,13 @@ _data: String = .empty,
/// 4-byte UTF-8 sequences (codepoints >= U+10000) produce 2 UTF-16 code units (surrogate pair),
/// everything else produces 1.
pub fn utf16Len(data: []const u8) usize {
+ // All-ASCII data (the common case) is one code unit per byte.
+ for (data) |byte| {
+ if (byte >= 0x80) {
+ break;
+ }
+ } else return data.len;
+
var count: usize = 0;
var i: usize = 0;
while (i < data.len) {
@@ -96,6 +103,41 @@ pub fn utf16OffsetToUtf8(data: []const u8, utf16_offset: usize) error{IndexSizeE
return error.IndexSizeError;
}
+/// Convert a UTF-16 code unit offset to a UTF-8 byte offset, rounding an
+/// offset that lands inside a surrogate pair down to the code point's start
+/// and clamping an offset past the end to data.len. Unlike the exact-or-error
+/// variant above, this is total and monotonic, so converting a start/end pair
+/// always yields ordered byte offsets.
+pub fn utf16OffsetToUtf8Floor(data: []const u8, utf16_offset: usize) usize {
+ var utf16_pos: usize = 0;
+ var i: usize = 0;
+ while (i < data.len) {
+ if (utf16_pos == utf16_offset) return i;
+ const byte = data[i];
+ const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch {
+ i += 1;
+ utf16_pos += 1;
+ continue;
+ };
+ if (i + seq_len > data.len) {
+ utf16_pos += 1;
+ i += 1;
+ continue;
+ }
+ if (seq_len == 4) {
+ if (utf16_offset == utf16_pos + 1) {
+ // Between the two code units of a surrogate pair.
+ return i;
+ }
+ utf16_pos += 2;
+ } else {
+ utf16_pos += 1;
+ }
+ i += seq_len;
+ }
+ return i;
+}
+
/// Convert a UTF-16 code unit range to UTF-8 byte offsets in a single pass.
/// Returns IndexSizeError if utf16_start > utf16 length of data.
/// Clamps utf16_end to the actual string length if it exceeds it.
@@ -527,6 +569,31 @@ test "utf16OffsetToUtf8" {
try std.testing.expectError(error.IndexSizeError, utf16OffsetToUtf8("", 1));
}
+test "utf16OffsetToUtf8Floor" {
+ // ASCII: identical to the exact variant, clamps past the end
+ try std.testing.expectEqual(@as(usize, 0), utf16OffsetToUtf8Floor("hello", 0));
+ try std.testing.expectEqual(@as(usize, 3), utf16OffsetToUtf8Floor("hello", 3));
+ try std.testing.expectEqual(@as(usize, 5), utf16OffsetToUtf8Floor("hello", 5));
+ try std.testing.expectEqual(@as(usize, 5), utf16OffsetToUtf8Floor("hello", 6)); // clamped
+
+ // Emoji "🌠AB" (4+1+1 = 6 bytes; 2+1+1 = 4 UTF-16 code units).
+ // Offset 1 lands inside the surrogate pair and rounds down to its start.
+ try std.testing.expectEqual(@as(usize, 0), utf16OffsetToUtf8Floor("🌠AB", 0));
+ try std.testing.expectEqual(@as(usize, 0), utf16OffsetToUtf8Floor("🌠AB", 1)); // mid-surrogate
+ try std.testing.expectEqual(@as(usize, 4), utf16OffsetToUtf8Floor("🌠AB", 2));
+ try std.testing.expectEqual(@as(usize, 5), utf16OffsetToUtf8Floor("🌠AB", 3));
+ try std.testing.expectEqual(@as(usize, 6), utf16OffsetToUtf8Floor("🌠AB", 4));
+ try std.testing.expectEqual(@as(usize, 6), utf16OffsetToUtf8Floor("🌠AB", 5)); // clamped
+
+ // Trailing surrogate pair: mid-surrogate at the very end still floors
+ try std.testing.expectEqual(@as(usize, 1), utf16OffsetToUtf8Floor("A🌠", 2)); // mid-surrogate
+ try std.testing.expectEqual(@as(usize, 5), utf16OffsetToUtf8Floor("A🌠", 3));
+
+ // Empty string
+ try std.testing.expectEqual(@as(usize, 0), utf16OffsetToUtf8Floor("", 0));
+ try std.testing.expectEqual(@as(usize, 0), utf16OffsetToUtf8Floor("", 1)); // clamped
+}
+
test "utf16RangeToUtf8" {
// ASCII: basic range
{
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 830810e0d..b1193459f 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -476,24 +476,44 @@ pub fn setOuterHTML(self: *Element, html: []const u8, frame: *Frame) !void {
// Observers of the parent must see a single mutation record replacing
// this node with the parsed nodes.
const notify = Frame.observers.hasMutationObservers(frame);
- const previous_sibling = node.previousSibling();
- const next_sibling = node.nextSibling();
var added: std.ArrayList(*Node) = .empty;
+ var fragment: ?*Node = null;
if (html.len > 0) {
- const fragment = (try Node.DocumentFragment.init(frame)).asNode();
- try frame.parseHtmlAsChildren(fragment, html);
+ const frag = (try Node.DocumentFragment.init(frame)).asNode();
+ try frame.parseHtmlAsChildren(frag, html);
+ fragment = frag;
+ }
+
+ // Parsing (and each insertion below) can synchronously run a custom
+ // element constructor that mutates the live tree; per the spec's replace
+ // step, a node that is no longer our parent's child cannot be replaced.
+ if (node._parent != parent) {
+ return error.NotFound;
+ }
+
+ // Captured after the parse: a constructor may have reshuffled siblings.
+ const previous_sibling = node.previousSibling();
+ const next_sibling = node.nextSibling();
+
+ if (fragment) |frag| {
const dest_connected = parent.isConnected();
- var it = fragment.childrenIterator();
+ var it = frag.childrenIterator();
while (it.next()) |child| {
+ if (node._parent != parent) {
+ return error.NotFound;
+ }
if (notify) {
- try added.append(frame.local_arena, child);
+ try added.append(frame.call_arena, child);
}
- frame.removeNode(fragment, child, .{ .will_be_reconnected = dest_connected, .notify_observers = false });
+ frame.removeNode(frag, child, .{ .will_be_reconnected = dest_connected, .notify_observers = false });
try frame.insertNodeRelative(parent, child, .{ .before = node }, .{ .notify_observers = false });
}
}
+ if (node._parent != parent) {
+ return error.NotFound;
+ }
frame.removeNode(parent, node, .{ .will_be_reconnected = false, .notify_observers = false });
if (notify) {
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 4e7907139..0f9e680a7 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -147,38 +147,12 @@ pub fn findAdjacentNodes(self: *Node, position: []const u8, variant: AdjacentVar
}
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 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 };
+ return .{ try self.adjacentParent(variant), self };
}
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 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.
- return .{ parent_node, self.nextSibling() };
+ return .{ try self.adjacentParent(variant), self.nextSibling() };
}
// Returned if:
@@ -187,6 +161,22 @@ pub fn findAdjacentNodes(self: *Node, position: []const u8, variant: AdjacentVar
return error.SyntaxError;
}
+// beforebegin/afterend insert into the parent, which must exist and, for the
+// html variant, cannot be a document or fragment.
+fn adjacentParent(self: *Node, variant: AdjacentVariant) !*Node {
+ const parent_node = self.parentNode() orelse switch (variant) {
+ .html => return error.NoModificationAllowed,
+ .node => return error.AdjacentNoParent,
+ };
+ if (variant == .html) {
+ switch (parent_node._type) {
+ .document, .document_fragment => return error.NoModificationAllowed,
+ else => {},
+ }
+ }
+ return parent_node;
+}
+
pub fn firstChild(self: *const Node) ?*Node {
const children = self._children orelse return null;
return linkToNodeOrNull(children.first);
@@ -248,8 +238,9 @@ fn validateDocumentInsertion(parent: *Node, node: *const Node, replaced_child: ?
}
switch (node._type) {
.cdata => |cd| {
- if (cd._type == .text) {
- // A Text node cannot be a child of a document.
+ if (cd._type == .text or cd._type == .cdata_section) {
+ // A Text node (CDATASection included) cannot be a child of a
+ // document.
return error.HierarchyError;
}
},
@@ -262,6 +253,15 @@ fn validateDocumentInsertion(parent: *Node, node: *const Node, replaced_child: ?
}
}
},
+ .document_type => {
+ var it = parent.childrenIterator();
+ while (it.next()) |existing| {
+ if (existing._type == .document_type and existing != node and existing != replaced_child) {
+ // A document can have at most one doctype child.
+ return error.HierarchyError;
+ }
+ }
+ },
else => {},
}
}
@@ -1275,11 +1275,11 @@ pub fn replaceChildren(self: *Node, nodes: []const NodeOrText, frame: *Frame) !v
var frag_it = frag.asNode().childrenIterator();
while (frag_it.next()) |frag_child| {
try validateNodeInsertion(self, frag_child);
- try children_to_add.append(frame.local_arena, frag_child);
+ try children_to_add.append(frame.call_arena, frag_child);
}
} else {
try validateNodeInsertion(self, child);
- try children_to_add.append(frame.local_arena, child);
+ try children_to_add.append(frame.call_arena, child);
}
}
@@ -1289,16 +1289,7 @@ pub fn replaceChildren(self: *Node, nodes: []const NodeOrText, frame: *Frame) !v
// record with all removed and added nodes, so per-node notification is
// suppressed here.
const notify = Frame.observers.hasMutationObservers(frame);
- var removed: std.ArrayList(*Node) = .empty;
-
- // Remove all existing children
- var it = self.childrenIterator();
- while (it.next()) |child| {
- if (notify) {
- try removed.append(frame.local_arena, child);
- }
- frame.removeNode(self, child, .{ .will_be_reconnected = false, .notify_observers = false });
- }
+ const removed = try self.removeAllChildrenCollecting(notify, frame);
// Append new children
const parent_is_connected = self.isConnected();
@@ -1316,6 +1307,21 @@ pub fn replaceChildren(self: *Node, nodes: []const NodeOrText, frame: *Frame) !v
}
}
+// Removes every child with per-node notification suppressed, returning the
+// removed nodes when `notify` is set so the caller can queue one combined
+// "replace all" mutation record.
+fn removeAllChildrenCollecting(self: *Node, notify: bool, frame: *Frame) !std.ArrayList(*Node) {
+ var removed: std.ArrayList(*Node) = .empty;
+ var it = self.childrenIterator();
+ while (it.next()) |child| {
+ if (notify) {
+ try removed.append(frame.call_arena, child);
+ }
+ frame.removeNode(self, child, .{ .will_be_reconnected = false, .notify_observers = false });
+ }
+ return removed;
+}
+
/// Shared implementation in Element and DocumentFragment
pub fn setHTML(self: *Node, html: []const u8, allow_declarative_shadow: bool, frame: *Frame) !void {
frame.domChanged();
@@ -1324,15 +1330,7 @@ pub fn setHTML(self: *Node, html: []const u8, allow_declarative_shadow: bool, fr
// record; per-node notification is suppressed for the removals here and
// for the parser insertions (fragment parsing never notifies).
const notify = Frame.observers.hasMutationObservers(frame);
- var removed: std.ArrayList(*Node) = .empty;
-
- var it = self.childrenIterator();
- while (it.next()) |child| {
- if (notify) {
- try removed.append(frame.local_arena, child);
- }
- frame.removeNode(self, child, .{ .will_be_reconnected = false, .notify_observers = false });
- }
+ const removed = try self.removeAllChildrenCollecting(notify, frame);
if (html.len > 0) {
if (allow_declarative_shadow) {
diff --git a/src/browser/webapi/Range.zig b/src/browser/webapi/Range.zig
index 100e2a3a5..9c0b9263f 100644
--- a/src/browser/webapi/Range.zig
+++ b/src/browser/webapi/Range.zig
@@ -376,9 +376,13 @@ pub fn insertNode(self: *Range, node: *Node, frame: *Frame) !void {
}
// Range offsets in CharacterData are UTF-16 code units; convert one to a
-// byte index into the UTF-8 data, clamping out-of-range offsets to the end.
+// byte index into the UTF-8 data. An offset inside a surrogate pair rounds
+// down to the code point and out-of-range offsets clamp to the end — the
+// conversion is monotonic, so a (start, end) offset pair always converts to
+// ordered byte offsets (an error-clamp here turned a mid-surrogate start
+// into data.len, producing start > end and a slice panic).
fn byteOffset(data: []const u8, utf16_offset: u32) usize {
- return Node.CData.utf16OffsetToUtf8(data, utf16_offset) catch data.len;
+ return Node.CData.utf16OffsetToUtf8Floor(data, utf16_offset);
}
pub fn deleteContents(self: *Range, frame: *Frame) !void {
diff --git a/src/browser/webapi/Selection.zig b/src/browser/webapi/Selection.zig
index e5f5979b2..77fedfd97 100644
--- a/src/browser/webapi/Selection.zig
+++ b/src/browser/webapi/Selection.zig
@@ -534,20 +534,20 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
if (isTextNode(focus_node)) {
if (forward) {
- const i = nextWordEnd(new_node.getData().str(), new_offset);
+ const i = wordEndOffset(new_node, new_offset);
if (i > new_offset) {
new_offset = i;
} else if (nextTextNode(focus_node)) |next| {
new_node = next;
- new_offset = nextWordEnd(next.getData().str(), 0);
+ new_offset = wordEndOffset(next, 0);
}
} else {
- const i = prevWordStart(new_node.getData().str(), new_offset);
+ const i = wordStartOffset(new_node, new_offset);
if (i < new_offset) {
new_offset = i;
} else if (prevTextNode(focus_node)) |prev| {
new_node = prev;
- new_offset = prevWordStart(prev.getData().str(), @intCast(prev.getData().len));
+ new_offset = wordStartOffset(prev, prev.getLength());
}
}
} else {
@@ -558,7 +558,7 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
const child = focus_node.getChildAt(focus_offset) orelse {
if (nextTextNodeAfter(focus_node)) |next| {
new_node = next;
- new_offset = nextWordEnd(next.getData().str(), 0);
+ new_offset = wordEndOffset(next, 0);
}
return self.applyModify(alter, new_node, new_offset, frame);
};
@@ -568,7 +568,7 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
};
new_node = t;
- new_offset = nextWordEnd(t.getData().str(), 0);
+ new_offset = wordEndOffset(t, 0);
} else {
var idx = focus_offset;
while (idx > 0) {
@@ -578,7 +578,7 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
while (bottom.lastChild()) |c| bottom = c;
if (isTextNode(bottom)) {
new_node = bottom;
- new_offset = prevWordStart(bottom.getData().str(), bottom.getLength());
+ new_offset = wordStartOffset(bottom, bottom.getLength());
break;
}
}
@@ -588,6 +588,22 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
try self.applyModify(alter, new_node, new_offset, frame);
}
+// nextWordEnd/prevWordStart scan UTF-8 bytes, but selection/range offsets
+// are UTF-16 code units; these wrappers convert on the way in and out.
+fn wordEndOffset(node: *Node, utf16_offset: u32) u32 {
+ const data = node.getData().str();
+ const byte_offset = Node.CData.utf16OffsetToUtf8Floor(data, utf16_offset);
+ const byte_end = nextWordEnd(data, @intCast(byte_offset));
+ return @intCast(Node.CData.utf16Len(data[0..byte_end]));
+}
+
+fn wordStartOffset(node: *Node, utf16_offset: u32) u32 {
+ const data = node.getData().str();
+ const byte_offset = Node.CData.utf16OffsetToUtf8Floor(data, utf16_offset);
+ const byte_start = prevWordStart(data, @intCast(byte_offset));
+ return @intCast(Node.CData.utf16Len(data[0..byte_start]));
+}
+
fn applyModify(self: *Selection, alter: ModifyAlter, new_node: *Node, new_offset: u32, frame: *Frame) !void {
switch (alter) {
.move => {
diff --git a/src/browser/webapi/cdata/Text.zig b/src/browser/webapi/cdata/Text.zig
index 063473ae5..1682d916e 100644
--- a/src/browser/webapi/cdata/Text.zig
+++ b/src/browser/webapi/cdata/Text.zig
@@ -61,11 +61,8 @@ pub fn getWholeText(self: *Text, frame: *Frame) ![]const u8 {
var buf: std.ArrayList(u8) = .empty;
var current: ?*Node = first;
while (current) |cur| : (current = cur.nextSibling()) {
- if (!isExclusiveTextNode(cur)) {
- break;
- }
-
- try buf.appendSlice(frame.local_arena, cur._type.cdata._data.str());
+ const text = cur.is(Text) orelse break;
+ try buf.appendSlice(frame.local_arena, text.ownData());
}
return buf.items;
}