Lightpanda: the headless browser designed for AI and automation
Use node childrenIterator
Karl Seguine5257f9bc352
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zigindex bd0157a6f..397a18102 100644--- a/src/browser/webapi/Node.zig+++ b/src/browser/webapi/Node.zig@@ -558,6 +558,13 @@ pub fn ownerDocument(self: *const Node, frame: *const Frame) ?*Document {return frame.document;}+fn ownerDocumentIncludingSelf(self: *const Node, frame: *const Frame) ?*Document {+ if (self._type == .document) {+ return self._type.document;+ }+ return self.ownerDocument(frame);+}+// Returns the Frame that owns this node's tree. Used to tie cached state of// "live" collections (NodeList, HTMLCollection, etc.) to the right frame's DOM// version: cross-realm callers must invalidate based on mutations through the@@ -566,10 +573,7 @@ pub fn ownerDocument(self: *const Node, frame: *const Frame) ?*Document {// Falls back to `default` when the node has no associated document yet (e.g.,// freshly created and detached) or its document has no frame.pub fn ownerFrame(self: *const Node, default: *Frame) *Frame {- if (self._type == .document) {- return self._type.document._frame orelse default;- }- const doc = self.ownerDocument(default) orelse return default;+ const doc = self.ownerDocumentIncludingSelf(default) orelse return default;return doc._frame orelse default;}@@ -582,9 +586,7 @@ pub const ResolveURLOpts = struct {pub fn resolveURL(self: *const Node, url: anytype, frame: *Frame, opts: ResolveURLOpts) ![:0]const u8 {const owner_frame = self.ownerFrame(frame);const allocator = opts.allocator orelse frame.call_arena;- // The owning document's encoding, not the frame's: script-created- // documents (e.g. createHTMLDocument) are always UTF-8.- const doc: ?*const Document = if (self._type == .document) self._type.document else self.ownerDocument(frame);+ const doc: ?*const Document = self.ownerDocumentIncludingSelf(frame);const encoding = if (doc) |d| d.getCharset() else owner_frame.charset;return URL.resolve(allocator, owner_frame.base(), url, .{ .encoding = encoding });}@@ -600,8 +602,8 @@ pub fn resolveURLReflect(self: *const Node, url: []const u8, frame: *Frame, optspub fn isSameDocumentAs(self: *const Node, other: *const Node, frame: *const Frame) bool {// Get the root document for each node- const self_doc = if (self._type == .document) self._type.document else self.ownerDocument(frame);- const other_doc = if (other._type == .document) other._type.document else other.ownerDocument(frame);+ const self_doc = self.ownerDocumentIncludingSelf(frame);+ const other_doc = other.ownerDocumentIncludingSelf(frame);return self_doc == other_doc;}@@ -1377,11 +1379,7 @@ pub const JsApi = struct {pub const baseURI = bridge.accessor(_baseURI, null, .{});fn _baseURI(self: *Node, frame: *const Frame) []const u8 {- const doc = if (self._type == .document)- self._type.document- else- self.ownerDocument(frame) orelse return frame.base();-+ const doc = self.ownerDocumentIncludingSelf(frame) orelse return frame.base();if (doc._frame) |doc_frame| {return doc_frame.base();}diff --git a/src/browser/webapi/collections/HTMLCollection.zig b/src/browser/webapi/collections/HTMLCollection.zigindex 36d2b5443..c332b32cb 100644--- a/src/browser/webapi/collections/HTMLCollection.zig+++ b/src/browser/webapi/collections/HTMLCollection.zig@@ -185,12 +185,6 @@ pub const JsApi = struct {// Named properties are [LegacyUnenumerableNamedProperties]: the query// reports them non-enumerable, so for-in skips them while// Object.getOwnPropertyNames still lists them via the enumerator.- // Deliberately NOT ReadOnly: v8 consults the query when a [[Set]] walks- // the prototype chain, and a read-only property on the prototype would- // block the expando that the spec's ignore-named-props rule requires- // (HTMLCollection-as-prototype.html). Writability as seen by- // getOwnPropertyDescriptor comes from describeByName, and direct- // assignments still fail through defineByName.fn queryByName(self: *HTMLCollection, name: []const u8, frame: *Frame) !u32 {if (name.len > 0 and self.getByName(name, frame) != null) {return js.v8.DontEnum;diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zigindex 8bea494f5..81ef82c3f 100644--- a/src/browser/webapi/selector/List.zig+++ b/src/browser/webapi/selector/List.zig@@ -642,9 +642,9 @@ fn matchesPseudoClass(el: *Node.Element, pseudo: Selector.PseudoClass, scope: *N.empty => {// Only element and content (non-empty text/cdata) children affect// emptiness; comments and processing instructions are ignored.- var child = node.firstChild();- while (child) |c| : (child = c.nextSibling()) {- switch (c._type) {+ var it = node.childrenIterator();+ while (it.next()) |child| {+ switch (child._type) {.cdata => |cdata| switch (cdata._type) {.comment, .processing_instruction => {},else => if (cdata.getLength() > 0) return false,@@ -680,29 +680,32 @@ fn matchesPseudoClass(el: *Node.Element, pseudo: Selector.PseudoClass, scope: *N// attribute. Elements in a document with no declared language// fall back to the UA default (en); detached subtrees have no// language at all.- var lang: ?[]const u8 = null;- var current: ?*Node = node;- while (current) |cur| : (current = cur.parentNode()) {- switch (cur._type) {- .element => |ancestor| {- if (ancestor.getAttributeSafe(comptime .wrap("lang"))) |value| {- lang = value;- break;- }- },- .document => {- lang = "en";- break;- },- else => {},+ const lang = blk: {+ var current: ?*Node = node;+ while (current) |cur| : (current = cur.parentNode()) {+ switch (cur._type) {+ .element => |ancestor| {+ if (ancestor.getAttributeSafe(comptime .wrap("lang"))) |value| {+ break :blk value;+ }+ },+ .document => {+ break :blk "en";+ },+ else => {},+ }}+ return false;+ };+ if (lang.len < expected.len) {+ return false;}- const value = lang orelse return false;// Match the exact language or a `-` separated sub-tag prefix// (:lang(en) matches lang="en-AU"), ASCII case-insensitively.- if (value.len < expected.len) return false;- if (!std.ascii.eqlIgnoreCase(value[0..expected.len], expected)) return false;- return value.len == expected.len or value[expected.len] == '-';+ if (!std.ascii.eqlIgnoreCase(lang[0..expected.len], expected)) {+ return false;+ }+ return lang.len == expected.len or lang[expected.len] == '-';},.not => |selectors| {for (selectors) |selector| {