Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add document named index

Karl Seguinaf94f0c27b19
diff --git a/src/browser/tests/document/named_properties.html b/src/browser/tests/document/named_properties.html
new file mode 100644
index 000000000..b808e925e
--- /dev/null
+++ b/src/browser/tests/document/named_properties.html
@@ -0,0 +1,80 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<form name="form1" id="f1">
+ <input name="field" value="hello">
+</form>
+<form name="dup" id="dup1"></form>
+<img name="dup" id="dup2">
+<img name="pic" id="img_by_id">
+<img id="img_no_name">
+<object id="obj_by_id"></object>
+<div name="not_named"></div>
+<form name="getElementById"></form>
+<iframe name="frame1" srcdoc="<p>hi</p>"></iframe>
+
+<script id=single>
+{
+ const form = document.forms.namedItem('form1');
+ testing.expectEqual(form, document.form1);
+ testing.expectEqual(form, document['form1']);
+ testing.expectEqual('hello', document.form1.field.value);
+ testing.expectTrue('form1' in document);
+ testing.expectFalse(Object.keys(document).includes('form1'));
+}
+</script>
+
+<script id=filter>
+{
+ testing.expectEqual($('#img_by_id'), document.pic);
+ // an img is also named by its id, but only when it has a name
+ testing.expectEqual($('#img_by_id'), document.img_by_id);
+ testing.expectEqual(undefined, document.img_no_name);
+ testing.expectEqual($('#obj_by_id'), document.obj_by_id);
+ testing.expectEqual(undefined, document.not_named);
+ testing.expectEqual(undefined, document.nope);
+ testing.expectFalse('nope' in document);
+ testing.expectFalse('not_named' in document);
+}
+</script>
+
+<script id=builtins>
+ testing.expectEqual('function', typeof document.getElementById);
+</script>
+
+<script id=multiple>
+{
+ const dup = document.dup;
+ testing.expectTrue(dup instanceof HTMLCollection);
+ testing.expectEqual(2, dup.length);
+ testing.expectEqual($('#dup1'), dup[0]);
+ testing.expectEqual($('#dup2'), dup[1]);
+
+ // live
+ const extra = document.createElement('embed');
+ extra.name = 'dup';
+ document.body.appendChild(extra);
+ testing.expectEqual(3, dup.length);
+ testing.expectEqual(extra, dup[2]);
+ extra.remove();
+ testing.expectEqual(2, dup.length);
+}
+</script>
+
+<script id=dynamic>
+{
+ testing.expectEqual(undefined, document.later);
+ const form = document.createElement('form');
+ form.name = 'later';
+ document.body.appendChild(form);
+ testing.expectEqual(form, document.later);
+ form.remove();
+ testing.expectEqual(undefined, document.later);
+}
+</script>
+
+<script id=iframe>
+ testing.onload(() => {
+ testing.expectEqual(document.querySelector('iframe').contentWindow, document.frame1);
+ });
+</script>
diff --git a/src/browser/webapi/HTMLDocument.zig b/src/browser/webapi/HTMLDocument.zig
index 910ca4aa7..c98c579b1 100644
--- a/src/browser/webapi/HTMLDocument.zig
+++ b/src/browser/webapi/HTMLDocument.zig
@@ -23,9 +23,13 @@ const Frame = @import("../Frame.zig");
const Node = @import("Node.zig");
const Document = @import("Document.zig");
const Element = @import("Element.zig");
+const Window = @import("Window.zig");
const DocumentType = @import("DocumentType.zig");
+const TreeWalker = @import("TreeWalker.zig");
const collections = @import("collections.zig");
+const NodeLive = collections.NodeLive;
+
const HTMLDocument = @This();
pub const Proto = Document;
@@ -99,7 +103,7 @@ pub fn getTitle(self: *HTMLDocument, frame: *Frame) ![]const u8 {
// Search the entire document for the first <title> element
const root = self._proto.getDocumentElement() orelse return "";
const title_element = blk: {
- var walker = @import("TreeWalker.zig").Full.init(root.asNode(), .{});
+ var walker = TreeWalker.Full.init(root.asNode(), .{});
while (walker.next()) |node| {
if (node.is(Element.Html.Title)) |title| {
break :blk title;
@@ -234,7 +238,7 @@ fn getDocType(self: *HTMLDocument, frame: *Frame) !*DocumentType {
return dt;
}
- var tw = @import("TreeWalker.zig").Full.init(self.asNode(), .{});
+ var tw = TreeWalker.Full.init(self.asNode(), .{});
while (tw.next()) |node| {
if (node._type == .document_type) {
self._document_type = node.as(DocumentType);
@@ -251,6 +255,70 @@ fn getDocType(self: *HTMLDocument, frame: *Frame) !*DocumentType {
return self._document_type.?;
}
+const NamedItemResult = union(enum) {
+ window: Window.Access,
+ element: *Element,
+ collection: NodeLive(.document_named),
+};
+fn namedItem(self: *HTMLDocument, name: []const u8, frame: *Frame) !?NamedItemResult {
+ if (name.len == 0) {
+ return null;
+ }
+
+ var tw = TreeWalker.FullExcludeSelf.Elements.init(self.asNode(), .{});
+ const first = findNamed(&tw, name) orelse return null;
+ if (findNamed(&tw, name) != null) {
+ // we found more than one, we return an HTMLCollection
+ return .{ .collection = NodeLive(.document_named).init(self.asNode(), try frame.dupeString(name), frame) };
+ }
+
+ if (first.is(Element.Html.IFrame)) |iframe| {
+ const iframe_window = iframe._window orelse return null;
+ return .{ .window = Window.Access.init(frame.window, iframe_window) };
+ }
+ return .{ .element = first };
+}
+
+fn findNamed(tw: *TreeWalker.FullExcludeSelf.Elements, name: []const u8) ?*Element {
+ while (tw.next()) |el| {
+ if (isNamed(el, name)) {
+ return el;
+ }
+ }
+ return null;
+}
+
+pub fn isNamed(el: *Element, name: []const u8) bool {
+ const html = el.is(Element.Html) orelse return false;
+ switch (html._type) {
+ .embed, .form, .iframe => {},
+ .img => {
+ const name_attr = el.getName() orelse return false;
+ if (name_attr.len == 0) {
+ return false;
+ }
+
+ if (std.mem.eql(u8, name_attr, name)) {
+ return true;
+ }
+
+ // img is weird, it can only match by id when it has a non-empty name
+ const id = el.getId() orelse return false;
+ return std.mem.eql(u8, id, name);
+ },
+ .object => {
+ if (el.getId()) |id| {
+ if (std.mem.eql(u8, id, name)) {
+ return true;
+ }
+ }
+ },
+ else => return false,
+ }
+ const name_attr = el.getName() orelse return false;
+ return std.mem.eql(u8, name_attr, name);
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(HTMLDocument);
@@ -305,4 +373,18 @@ pub const JsApi = struct {
pub const currentScript = bridge.accessor(HTMLDocument.getCurrentScript, null, .{});
pub const all = bridge.accessor(HTMLDocument.getAll, null, .{});
pub const doctype = bridge.accessor(HTMLDocument.getDocType, null, .{});
+
+ pub const @"[str]" = bridge.namedIndexed(HTMLDocument.namedItem, null, null, null, struct {
+ fn query(self: *HTMLDocument, name: []const u8) !u32 {
+ if (name.len == 0) {
+ return error.NotHandled;
+ }
+
+ var tw = TreeWalker.FullExcludeSelf.Elements.init(self.asNode(), .{});
+ if (findNamed(&tw, name) == null) {
+ return error.NotHandled;
+ }
+ return js.v8.None;
+ }
+ }.query, .{ .null_as_undefined = true });
};
diff --git a/src/browser/webapi/collections/HTMLCollection.zig b/src/browser/webapi/collections/HTMLCollection.zig
index e6faf9847..36bcf8f94 100644
--- a/src/browser/webapi/collections/HTMLCollection.zig
+++ b/src/browser/webapi/collections/HTMLCollection.zig
@@ -42,6 +42,7 @@ const Mode = enum {
links,
anchors,
form,
+ document_named,
empty,
};
@@ -63,6 +64,7 @@ _data: union(Mode) {
links: NodeLive(.links),
anchors: NodeLive(.anchors),
form: NodeLive(.form),
+ document_named: NodeLive(.document_named),
empty: void,
},
_rc: lp.RC = .{},
@@ -134,6 +136,7 @@ pub fn iterator(self: *HTMLCollection, exec: *const Execution) !*Iterator {
.links => |*impl| .{ .links = impl._tw.clone() },
.anchors => |*impl| .{ .anchors = impl._tw.clone() },
.form => |*impl| .{ .form = impl._tw.clone() },
+ .document_named => |*impl| .{ .document_named = impl._tw.clone() },
.empty => .empty,
},
}, exec);
@@ -156,6 +159,7 @@ pub const Iterator = GenericIterator(struct {
links: TreeWalker.FullExcludeSelf,
anchors: TreeWalker.FullExcludeSelf,
form: TreeWalker.FullExcludeSelf,
+ document_named: TreeWalker.FullExcludeSelf,
empty: void,
},
@@ -182,6 +186,7 @@ pub const Iterator = GenericIterator(struct {
.links => |*impl| impl.nextTw(&self.tw.links),
.anchors => |*impl| impl.nextTw(&self.tw.anchors),
.form => |*impl| impl.nextTw(&self.tw.form),
+ .document_named => |*impl| impl.nextTw(&self.tw.document_named),
.empty => return null,
};
}
diff --git a/src/browser/webapi/collections/node_live.zig b/src/browser/webapi/collections/node_live.zig
index f76249933..d7b739ce4 100644
--- a/src/browser/webapi/collections/node_live.zig
+++ b/src/browser/webapi/collections/node_live.zig
@@ -27,6 +27,7 @@ const Element = @import("../Element.zig");
const TreeWalker = @import("../TreeWalker.zig");
const Selector = @import("../selector/Selector.zig");
const Form = @import("../element/html/Form.zig");
+const HTMLDocument = @import("../HTMLDocument.zig");
const String = lp.String;
@@ -45,6 +46,7 @@ const Mode = enum {
links,
anchors,
form,
+ document_named,
};
const ClassNameFilter = struct {
@@ -74,6 +76,7 @@ const Filters = union(Mode) {
links,
anchors,
form: struct { form: *Form, form_id: ?[]const u8, image_buttons: bool },
+ document_named: []const u8,
fn TypeOf(comptime mode: Mode) type {
@setEvalBranchQuota(10_000);
@@ -103,7 +106,7 @@ const Filters = union(Mode) {
pub fn NodeLive(comptime mode: Mode) type {
const Filter = Filters.TypeOf(mode);
const TW = switch (mode) {
- .tag, .tag_name, .tag_name_ns, .class_name, .name, .all_elements, .links, .anchors, .form => TreeWalker.FullExcludeSelf,
+ .tag, .tag_name, .tag_name_ns, .class_name, .name, .all_elements, .links, .anchors, .form, .document_named => TreeWalker.FullExcludeSelf,
.child_elements, .child_tag, .cells => TreeWalker.Children,
// A select's options can sit one level down, inside an <optgroup>, so
// these two walk the subtree and filter on the parent instead.
@@ -396,6 +399,10 @@ pub fn NodeLive(comptime mode: Mode) type {
// controls, this could be significantly faster.
return self._filter.form.asNode().contains(node);
},
+ .document_named => {
+ const el = node.is(Element) orelse return false;
+ return HTMLDocument.isNamed(el, self._filter);
+ },
}
}
@@ -447,6 +454,7 @@ pub fn NodeLive(comptime mode: Mode) type {
.links => .{ ._data = .{ .links = self } },
.anchors => .{ ._data = .{ .anchors = self } },
.form => .{ ._data = .{ .form = self } },
+ .document_named => .{ ._data = .{ .document_named = self } },
};
}
};