Skip to content

Lightpanda: the headless browser designed for AI and automation

Minor cleanup

Karl Seguinfa2e21cb5781
diff --git a/src/NodeRegistry.zig b/src/NodeRegistry.zig
index 020b46084..f0f217f81 100644
--- a/src/NodeRegistry.zig
+++ b/src/NodeRegistry.zig
@@ -65,17 +65,17 @@ pub fn reset(self: *NodeRegistry) void {
}
/// Evict only the nodes owned by `frame`'s page, leaving sibling pages' node
-/// IDs valid. Must run before the page's arena is freed — attribution walks
-/// each node's live parent chain.
+/// IDs valid. Must run before the page's arena is freed — attribution reads
+/// each node's document.
pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
const page = frame._page;
var doomed: std.ArrayListUnmanaged(*Node) = .empty;
var it = self.lookup_by_id.valueIterator();
while (it.next()) |node_ptr| {
const node = node_ptr.*;
- // Nodes live in their page's slab. A sibling page's node must not be
- // resolved through this page's frames, so attribute by storage.
- if (page.factory.owns(node.dom)) {
+ // The document table is the browser's, so a sibling page's node
+ // resolves correctly through this page's frame.
+ if (node.dom.getDocument(frame)._page == page) {
doomed.append(arena, node) catch return;
}
}
diff --git a/src/browser/Factory.zig b/src/browser/Factory.zig
index 408d35aa1..56b7015f1 100644
--- a/src/browser/Factory.zig
+++ b/src/browser/Factory.zig
@@ -81,11 +81,6 @@ fn registerDocument(self: *Factory, doc: *Document) !u32 {
return index;
}
-// Whether `ptr` lives in this page's node storage.
-pub fn owns(self: *const Factory, ptr: *const anyopaque) bool {
- return self._slab.owns(ptr);
-}
-
// this is a root object
pub fn eventTarget(self: *Factory, child: anytype) !*@TypeOf(child) {
return self.eventTargetWithAllocator(self._slab.allocator(), child);
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 21a452d83..3f1f0b669 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -480,6 +480,7 @@ pub fn init(self: *Frame, frame_id: u32, page: *Page, opts: InitOpts) !void {
location.acquireRef();
self.window._location = location;
+ lp.assert(document._page == page, "unexpected document page", .{});
document._frame = self;
if (comptime lp.IS_TEST == false) {
diff --git a/src/browser/webapi/cdata/Text.zig b/src/browser/webapi/cdata/Text.zig
index 7b2ae99fd..3f41aa6b6 100644
--- a/src/browser/webapi/cdata/Text.zig
+++ b/src/browser/webapi/cdata/Text.zig
@@ -46,13 +46,13 @@ pub fn init(str: ?js.NullableString, frame: *Frame) !*Text {
// This Text node's own data (getWholeText below spans adjacent Text nodes).
pub fn ownData(self: *const Text) []const u8 {
- return Factory.protoOf(self)._data.str();
+ return self.asCData()._data.str();
}
// The concatenated data of the contiguous exclusive Text nodes (adjacent
// Text siblings on both sides of this one), in tree order.
fn getWholeText(self: *Text, frame: *Frame) ![]const u8 {
- const node = Factory.protoOf(self).asNode();
+ const node = self.asCData().asNode();
var first = node;
while (first.previousSibling()) |prev| {
@@ -86,16 +86,17 @@ fn getAssignedSlot(self: *Text, frame: *Frame) ?*Slot {
}
pub fn splitText(self: *Text, offset: usize, frame: *Frame) !*Text {
- const data = Factory.protoOf(self)._data.str();
+ const cdata = self.asCData();
+ const node = cdata.asNode();
+
+ const data = cdata._data.str();
const byte_offset = CData.utf16OffsetToUtf8(data, offset) catch return error.IndexSizeError;
const new_data = data[byte_offset..];
- const new_node = try Frame.node_factory.createTextNode(Factory.protoOf(self).asNode().getDocument(frame), new_data);
+ const new_node = try Frame.node_factory.createTextNode(node.getDocument(frame), new_data);
const new_text = new_node.as(Text);
- const node = Factory.protoOf(self).asNode();
-
// Per DOM spec splitText: insert first (step 7a), then update ranges (7b-7e),
// then truncate original node (step 8).
if (node.parentNode()) |parent| {
@@ -112,7 +113,7 @@ pub fn splitText(self: *Text, offset: usize, frame: *Frame) !*Text {
// Use replaceData instead of setData so live range updates fire
// (matters for detached text nodes where steps 7b-7e were skipped).
const length = Factory.protoOf(self).getLength();
- try Factory.protoOf(self).replaceData(offset, length - offset, "", frame);
+ try cdata.replaceData(offset, length - offset, "", frame);
return new_text;
}
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index 42d977ffc..6e91876cf 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -204,7 +204,7 @@ pub const List = struct {
return self.getEntryWithInternedName(name) != null;
}
- pub fn getAttribute(self: *const List, name: String, element: ?*Element, frame: *Frame) !?*Attribute {
+ pub fn getAttribute(self: *const List, name: String, element: *Element, frame: *Frame) !?*Attribute {
const entry = (try self.getEntry(name, frame)) orelse return null;
return self.getOrCreateAttribute(entry, element, frame);
}
@@ -212,8 +212,8 @@ pub const List = struct {
// Identity map access: a given (list, name) always yields the same
// *Attribute until the attribute is removed. The map must be the
// element's frame's, not the caller's frame.
- pub fn getOrCreateAttribute(self: *const List, entry: *const Entry, element: ?*Element, frame: *Frame) !*Attribute {
- const owner = if (element) |el| (el.ownerFrame(frame) orelse frame) else frame;
+ pub fn getOrCreateAttribute(self: *const List, entry: *const Entry, element: *Element, frame: *Frame) !*Attribute {
+ const owner = element.ownerFrame(frame) orelse frame;
const gop = try owner._attribute_lookup.getOrPut(owner.arena, .{ .list = self, .name = entry._name_ptr });
if (!gop.found_existing) {
gop.value_ptr.* = try entry.toAttribute(element, owner);
@@ -496,10 +496,8 @@ pub const List = struct {
return formatAttribute(self.name(), self.value(), writer);
}
- fn toAttribute(self: *const Entry, element: ?*Element, frame: *Frame) !*Attribute {
- // Without an element, the attribute is the caller's document's.
- const document = if (element) |el| el.getDocument(frame) else frame.document;
- return frame._factory.node(document, Attribute{
+ fn toAttribute(self: *const Entry, element: *Element, frame: *Frame) !*Attribute {
+ return frame._factory.node(element.getDocument(frame), Attribute{
._element = element,
// The entry's bytes outlive the entry itself, so the
// Attribute can wrap them without duping.
diff --git a/src/slab.zig b/src/slab.zig
index 02d537d70..21e060c7b 100644
--- a/src/slab.zig
+++ b/src/slab.zig
@@ -365,20 +365,6 @@ pub const SlabAllocator = struct {
.resize = Allocator.noResize,
};
- // Whether `ptr` was allocated from one of this allocator's chunks.
- pub fn owns(self: *const Self, ptr: *const anyopaque) bool {
- const addr = @intFromPtr(ptr);
- for (self.slabs.values()) |*slab| {
- for (slab.chunks.items) |chunk| {
- const start = @intFromPtr(chunk.ptr);
- if (addr >= start and addr < start + chunk.len) {
- return true;
- }
- }
- }
- return false;
- }
-
pub fn allocator(self: *Self) Allocator {
return .{
.ptr = self,