From ea80e5e4a120e0909c695c31f2dc8c0e3d96587c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 3 Jan 2024 16:23:26 +0100 Subject: [PATCH 01/20] dom: DocumentHTML getters --- src/html/document.zig | 32 ++++++++++++++++++++++++++++++++ src/netsurf.zig | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 4a65cd24..55016ccc 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -18,9 +18,35 @@ pub const HTMLDocument = struct { // JS funcs // -------- + pub fn get_domain(self: *parser.DocumentHTML) ![]const u8 { + return try parser.documentHTMLGetDomain(self); + } + + pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { + return parser.DOMError.NotSupported; + } + + pub fn get_referrer(self: *parser.DocumentHTML) ![]const u8 { + return try parser.documentHTMLGetReferrer(self); + } + + pub fn set_referrer(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { + return parser.DOMError.NotSupported; + } + pub fn get_body(self: *parser.DocumentHTML) !?*parser.Body { return try parser.documentHTMLBody(self); } + + // TODO: not implemented by libdom + pub fn get_cookie(_: *parser.DocumentHTML) ![]const u8 { + return error.NotImplemented; + } + + // TODO: not implemented by libdom + pub fn set_cookie(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { + return parser.DOMError.NotSupported; + } }; // Tests @@ -37,4 +63,10 @@ pub fn testExecFn( .{ .src = "document.body.localName == 'body'", .ex = "true" }, }; try checkCases(js_env, &constructor); + + var getters = [_]Case{ + .{ .src = "document.domain", .ex = "" }, + .{ .src = "document.referrer", .ex = "" }, + }; + try checkCases(js_env, &getters); } diff --git a/src/netsurf.zig b/src/netsurf.zig index 88565df7..ed58cda1 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1450,3 +1450,19 @@ pub inline fn documentHTMLBody(doc_html: *DocumentHTML) !?*Body { if (body == null) return null; return @as(*Body, @ptrCast(body.?)); } + +pub fn documentHTMLGetDomain(doc: *DocumentHTML) ![]const u8 { + var s: ?*String = undefined; + const err = documentHTMLVtable(doc).get_domain.?(doc, &s); + try DOMErr(err); + if (s == null) return ""; + return strToData(s.?); +} + +pub fn documentHTMLGetReferrer(doc: *DocumentHTML) ![]const u8 { + var s: ?*String = undefined; + const err = documentHTMLVtable(doc).get_referrer.?(doc, &s); + try DOMErr(err); + if (s == null) return ""; + return strToData(s.?); +} From 970d51e4b099311e4e098434c73da9acc82ddee0 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 3 Jan 2024 16:32:59 +0100 Subject: [PATCH 02/20] dom: DocumentHTML title --- src/html/document.zig | 17 +++++++++++++++++ src/netsurf.zig | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 55016ccc..16158969 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -47,6 +47,15 @@ pub const HTMLDocument = struct { pub fn set_cookie(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { return parser.DOMError.NotSupported; } + + pub fn get_title(self: *parser.DocumentHTML) ![]const u8 { + return try parser.documentHTMLGetTitle(self); + } + + pub fn set_title(self: *parser.DocumentHTML, v: []const u8) ![]const u8 { + try parser.documentHTMLSetTitle(self, v); + return v; + } }; // Tests @@ -67,6 +76,14 @@ pub fn testExecFn( var getters = [_]Case{ .{ .src = "document.domain", .ex = "" }, .{ .src = "document.referrer", .ex = "" }, + .{ .src = "document.title", .ex = "" }, }; try checkCases(js_env, &getters); + + var titles = [_]Case{ + .{ .src = "document.title = 'foo'", .ex = "foo" }, + .{ .src = "document.title", .ex = "foo" }, + .{ .src = "document.title = ''", .ex = "" }, + }; + try checkCases(js_env, &titles); } diff --git a/src/netsurf.zig b/src/netsurf.zig index ed58cda1..84e0991c 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1466,3 +1466,16 @@ pub fn documentHTMLGetReferrer(doc: *DocumentHTML) ![]const u8 { if (s == null) return ""; return strToData(s.?); } + +pub fn documentHTMLGetTitle(doc: *DocumentHTML) ![]const u8 { + var s: ?*String = undefined; + const err = documentHTMLVtable(doc).get_title.?(doc, &s); + try DOMErr(err); + if (s == null) return ""; + return strToData(s.?); +} + +pub fn documentHTMLSetTitle(doc: *DocumentHTML, v: []const u8) !void { + const err = documentHTMLVtable(doc).set_title.?(doc, try strFromData(v)); + try DOMErr(err); +} From a4d44081e516c58997753d22ff8d185f46ca3a22 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 11:50:14 +0100 Subject: [PATCH 03/20] netsurf: add missing inline --- src/netsurf.zig | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/netsurf.zig b/src/netsurf.zig index 84e0991c..2e021e24 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1451,7 +1451,12 @@ pub inline fn documentHTMLBody(doc_html: *DocumentHTML) !?*Body { return @as(*Body, @ptrCast(body.?)); } -pub fn documentHTMLGetDomain(doc: *DocumentHTML) ![]const u8 { +pub inline fn documentHTMLSetBody(doc_html: *DocumentHTML, elt: ?*ElementHTML) !void { + const err = documentHTMLVtable(doc_html).set_body.?(doc_html, elt); + try DOMErr(err); +} + +pub inline fn documentHTMLGetDomain(doc: *DocumentHTML) ![]const u8 { var s: ?*String = undefined; const err = documentHTMLVtable(doc).get_domain.?(doc, &s); try DOMErr(err); @@ -1459,7 +1464,7 @@ pub fn documentHTMLGetDomain(doc: *DocumentHTML) ![]const u8 { return strToData(s.?); } -pub fn documentHTMLGetReferrer(doc: *DocumentHTML) ![]const u8 { +pub inline fn documentHTMLGetReferrer(doc: *DocumentHTML) ![]const u8 { var s: ?*String = undefined; const err = documentHTMLVtable(doc).get_referrer.?(doc, &s); try DOMErr(err); @@ -1467,7 +1472,7 @@ pub fn documentHTMLGetReferrer(doc: *DocumentHTML) ![]const u8 { return strToData(s.?); } -pub fn documentHTMLGetTitle(doc: *DocumentHTML) ![]const u8 { +pub inline fn documentHTMLGetTitle(doc: *DocumentHTML) ![]const u8 { var s: ?*String = undefined; const err = documentHTMLVtable(doc).get_title.?(doc, &s); try DOMErr(err); @@ -1475,7 +1480,7 @@ pub fn documentHTMLGetTitle(doc: *DocumentHTML) ![]const u8 { return strToData(s.?); } -pub fn documentHTMLSetTitle(doc: *DocumentHTML, v: []const u8) !void { +pub inline fn documentHTMLSetTitle(doc: *DocumentHTML, v: []const u8) !void { const err = documentHTMLVtable(doc).set_title.?(doc, try strFromData(v)); try DOMErr(err); } From 0b952f5295f1d5ea3d91e21fd542a5815268aa4c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 11:50:47 +0100 Subject: [PATCH 04/20] HTMLCollection: add a matcher by name --- src/dom/html_collection.zig | 52 ++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index da4bf2f6..cb455659 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -12,6 +12,7 @@ const Element = @import("element.zig").Element; const Union = @import("element.zig").Union; const Matcher = union(enum) { + matchByName: MatchByName, matchByTagName: MatchByTagName, matchByClassName: MatchByClassName, matchTrue: struct {}, @@ -21,6 +22,7 @@ const Matcher = union(enum) { inline .matchTrue => return true, inline .matchByTagName => |case| return case.match(node), inline .matchByClassName => |case| return case.match(node), + inline .matchByName => |case| return case.match(node), } } @@ -29,6 +31,7 @@ const Matcher = union(enum) { inline .matchTrue => return, inline .matchByTagName => |case| return case.deinit(alloc), inline .matchByClassName => |case| return case.deinit(alloc), + inline .matchByName => |case| return case.deinit(alloc), } } }; @@ -117,6 +120,44 @@ pub fn HTMLCollectionByClassName( }; } +pub const MatchByName = struct { + name: []const u8, + + fn init(alloc: std.mem.Allocator, name: []const u8) !MatchByName { + const names_alloc = try alloc.alloc(u8, name.len); + @memcpy(names_alloc, name); + return MatchByName{ + .name = names_alloc, + }; + } + + pub fn match(self: MatchByName, node: *parser.Node) !bool { + const e = parser.nodeToElement(node); + const nname = try parser.elementGetAttribute(e, "name") orelse return false; + return std.mem.eql(u8, self.name, nname); + } + + fn deinit(self: MatchByName, alloc: std.mem.Allocator) void { + alloc.free(self.name); + } +}; + +pub fn HTMLCollectionByName( + alloc: std.mem.Allocator, + root: ?*parser.Node, + name: []const u8, + include_root: bool, +) !HTMLCollection { + return HTMLCollection{ + .root = root, + .walker = Walker{ .walkerDepthFirst = .{} }, + .matcher = Matcher{ + .matchByName = try MatchByName.init(alloc, name), + }, + .include_root = include_root, + }; +} + pub fn HTMLCollectionChildren( root: ?*parser.Node, include_root: bool, @@ -259,7 +300,7 @@ pub const HTMLCollection = struct { return len; } - pub fn _item(self: *HTMLCollection, index: u32) !?Union { + pub fn item(self: *HTMLCollection, index: u32) !?*parser.Node { if (self.root == null) return null; var i: u32 = 0; @@ -282,8 +323,7 @@ pub const HTMLCollection = struct { self.cur_node = node; self.cur_idx = i; - const e = @as(*parser.Element, @ptrCast(node)); - return try Element.toInterface(e); + return node; } i += 1; @@ -296,6 +336,12 @@ pub const HTMLCollection = struct { return null; } + pub fn _item(self: *HTMLCollection, index: u32) !?Union { + const node = try self.item(index) orelse return null; + const e = @as(*parser.Element, @ptrCast(node)); + return try Element.toInterface(e); + } + pub fn _namedItem(self: *HTMLCollection, name: []const u8) !?Union { if (self.root == null) return null; if (name.len == 0) return null; From 2b681c8353e01329c7f6cd376d7dc93282f7ed1c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 11:51:06 +0100 Subject: [PATCH 05/20] nodelist: add comment about liveness --- src/dom/nodelist.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dom/nodelist.zig b/src/dom/nodelist.zig index fd3419df..d70df85c 100644 --- a/src/dom/nodelist.zig +++ b/src/dom/nodelist.zig @@ -14,6 +14,10 @@ const DOMException = @import("exceptions.zig").DOMException; // Nodelist is implemented in pure Zig b/c libdom's NodeList doesn't allow to // append nodes. // WEB IDL https://dom.spec.whatwg.org/#nodelist +// +// TODO: a Nodelist can be either static or live. But the current +// implementation allows only static nodelist. +// see https://dom.spec.whatwg.org/#old-style-collections pub const NodeList = struct { pub const mem_guarantied = true; pub const Exception = DOMException; From 6bc44cbe75c53daee5733eaf4f594fc59726118f Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 11:51:45 +0100 Subject: [PATCH 06/20] dom: add DocumentHTML getElementsByName --- src/html/document.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 16158969..d0241851 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -7,7 +7,9 @@ const Case = jsruntime.test_utils.Case; const checkCases = jsruntime.test_utils.checkCases; const Document = @import("../dom/document.zig").Document; +const NodeList = @import("../dom/nodelist.zig").NodeList; const HTMLElem = @import("elements.zig"); +const collection = @import("../dom/html_collection.zig"); // WEB IDL https://html.spec.whatwg.org/#the-document-object pub const HTMLDocument = struct { @@ -56,6 +58,35 @@ pub const HTMLDocument = struct { try parser.documentHTMLSetTitle(self, v); return v; } + + pub fn _getElementsByName(self: *parser.DocumentHTML, alloc: std.mem.Allocator, name: []const u8) !NodeList { + var list = try NodeList.init(); + errdefer list.deinit(alloc); + + if (name.len == 0) return list; + + const root = try rootNode(self) orelse return list; + + var c = try collection.HTMLCollectionByName(alloc, root, name, false); + + const ln = try c.get_length(); + var i: u32 = 0; + while (i < ln) { + const n = try c.item(i) orelse break; + try list.append(alloc, n); + i += 1; + } + + return list; + } + + inline fn rootNode(self: *parser.DocumentHTML) !?*parser.Node { + const doc = parser.documentHTMLToDocument(self); + const elt = try parser.documentGetDocumentElement(doc) orelse return null; + return parser.elementToNode(elt); + } + + pub fn deinit(_: *parser.DocumentHTML, _: std.mem.Allocator) void {} }; // Tests @@ -86,4 +117,11 @@ pub fn testExecFn( .{ .src = "document.title = ''", .ex = "" }, }; try checkCases(js_env, &titles); + + var getElementsByName = [_]Case{ + .{ .src = "document.getElementById('link').setAttribute('name', 'foo')", .ex = "undefined" }, + .{ .src = "let list = document.getElementsByName('foo')", .ex = "undefined" }, + .{ .src = "list.length", .ex = "1" }, + }; + try checkCases(js_env, &getElementsByName); } From db8031f965cb11fe9480ae0d8da82ccbbb6ad7ac Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 12:15:08 +0100 Subject: [PATCH 07/20] HTMLCollection: add match by links --- src/dom/html_collection.zig | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index cb455659..6421282a 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -15,6 +15,7 @@ const Matcher = union(enum) { matchByName: MatchByName, matchByTagName: MatchByTagName, matchByClassName: MatchByClassName, + matchByLinks: MatchByLinks, matchTrue: struct {}, pub fn match(self: Matcher, node: *parser.Node) !bool { @@ -23,6 +24,7 @@ const Matcher = union(enum) { inline .matchByTagName => |case| return case.match(node), inline .matchByClassName => |case| return case.match(node), inline .matchByName => |case| return case.match(node), + inline .matchByLinks => return MatchByLinks.match(node), } } @@ -32,6 +34,7 @@ const Matcher = union(enum) { inline .matchByTagName => |case| return case.deinit(alloc), inline .matchByClassName => |case| return case.deinit(alloc), inline .matchByName => |case| return case.deinit(alloc), + inline .matchByLinks => return, } } }; @@ -170,6 +173,34 @@ pub fn HTMLCollectionChildren( }; } +// MatchByLinks matches the a and area elements in the Document that have href +// attributes. +// https://html.spec.whatwg.org/#dom-document-links +pub const MatchByLinks = struct { + pub fn match(node: *parser.Node) !bool { + const tag = try parser.nodeName(node); + if (!std.ascii.eqlIgnoreCase(tag, "a") and !std.ascii.eqlIgnoreCase(tag, "area")) { + return false; + } + const elem = @as(*parser.Element, @ptrCast(node)); + return parser.elementHasAttribute(elem, "href"); + } +}; + +pub fn HTMLCollectionByLinks( + root: ?*parser.Node, + include_root: bool, +) !HTMLCollection { + return HTMLCollection{ + .root = root, + .walker = Walker{ .walkerDepthFirst = .{} }, + .matcher = Matcher{ + .matchByLinks = MatchByLinks{}, + }, + .include_root = include_root, + }; +} + const Walker = union(enum) { walkerDepthFirst: WalkerDepthFirst, walkerChildren: WalkerChildren, From 7a4de724e3d1149c744f601393a92580978176ad Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 12:15:35 +0100 Subject: [PATCH 08/20] HTMLDocument: add images/scripts/forms/links/embed getters --- src/html/document.zig | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index d0241851..07dea582 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -86,6 +86,30 @@ pub const HTMLDocument = struct { return parser.elementToNode(elt); } + pub fn get_images(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { + return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "img", false); + } + + pub fn get_embeds(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { + return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "embed", false); + } + + pub fn get_plugins(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { + return get_embeds(self, alloc); + } + + pub fn get_forms(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { + return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "form", false); + } + + pub fn get_scripts(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { + return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "script", false); + } + + pub fn get_links(self: *parser.DocumentHTML) !collection.HTMLCollection { + return try collection.HTMLCollectionByLinks(try rootNode(self), false); + } + pub fn deinit(_: *parser.DocumentHTML, _: std.mem.Allocator) void {} }; @@ -108,6 +132,13 @@ pub fn testExecFn( .{ .src = "document.domain", .ex = "" }, .{ .src = "document.referrer", .ex = "" }, .{ .src = "document.title", .ex = "" }, + .{ .src = "document.body.tagName", .ex = "BODY" }, + .{ .src = "document.images.length", .ex = "0" }, + .{ .src = "document.embeds.length", .ex = "0" }, + .{ .src = "document.plugins.length", .ex = "0" }, + .{ .src = "document.scripts.length", .ex = "0" }, + .{ .src = "document.forms.length", .ex = "0" }, + .{ .src = "document.links.length", .ex = "1" }, }; try checkCases(js_env, &getters); From 9de23d76d2607e2351d3ea03628a3d96bdaf4b9e Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 12:16:34 +0100 Subject: [PATCH 09/20] HTMLDocument: add body setter --- src/html/document.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 07dea582..341c1701 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -40,6 +40,11 @@ pub const HTMLDocument = struct { return try parser.documentHTMLBody(self); } + pub fn set_body(self: *parser.DocumentHTML, elt: ?*parser.ElementHTML) !?*parser.Body { + try parser.documentHTMLSetBody(self, elt); + return try get_body(self); + } + // TODO: not implemented by libdom pub fn get_cookie(_: *parser.DocumentHTML) ![]const u8 { return error.NotImplemented; From 8c121ce7b8981d45d25582fc40593628d86e7001 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 12:25:12 +0100 Subject: [PATCH 10/20] upgrade tests/wpt --- tests/wpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wpt b/tests/wpt index 4f7ac437..28814be2 160000 --- a/tests/wpt +++ b/tests/wpt @@ -1 +1 @@ -Subproject commit 4f7ac437ea6fb1d4e3b207078c0fb52f8f2d3b94 +Subproject commit 28814be2ea9b17bb1b73946d245fba50f746da0b From 8f18fda54fb4bd71bd0686e68f12b9fbfe8b1313 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 14:41:45 +0100 Subject: [PATCH 11/20] HTMLDocument: add head getter --- src/html/document.zig | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/html/document.zig b/src/html/document.zig index 341c1701..6e58d0e1 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -6,10 +6,13 @@ const jsruntime = @import("jsruntime"); const Case = jsruntime.test_utils.Case; const checkCases = jsruntime.test_utils.checkCases; +const Node = @import("../dom/node.zig").Node; const Document = @import("../dom/document.zig").Document; const NodeList = @import("../dom/nodelist.zig").NodeList; const HTMLElem = @import("elements.zig"); + const collection = @import("../dom/html_collection.zig"); +const Walker = collection.WalkerDepthFirst; // WEB IDL https://html.spec.whatwg.org/#the-document-object pub const HTMLDocument = struct { @@ -45,6 +48,18 @@ pub const HTMLDocument = struct { return try get_body(self); } + pub fn get_head(self: *parser.DocumentHTML) !?*parser.Head { + const root = try rootNode(self) orelse return null; + const walker = Walker{}; + var next: ?*parser.Node = null; + while (true) { + next = try walker.get_next(root, next) orelse return null; + if (std.ascii.eqlIgnoreCase("head", try parser.nodeName(next.?))) { + return @as(*parser.Head, @ptrCast(next.?)); + } + } + } + // TODO: not implemented by libdom pub fn get_cookie(_: *parser.DocumentHTML) ![]const u8 { return error.NotImplemented; @@ -137,7 +152,8 @@ pub fn testExecFn( .{ .src = "document.domain", .ex = "" }, .{ .src = "document.referrer", .ex = "" }, .{ .src = "document.title", .ex = "" }, - .{ .src = "document.body.tagName", .ex = "BODY" }, + .{ .src = "document.body.localName", .ex = "body" }, + .{ .src = "document.head.localName", .ex = "head" }, .{ .src = "document.images.length", .ex = "0" }, .{ .src = "document.embeds.length", .ex = "0" }, .{ .src = "document.plugins.length", .ex = "0" }, From d07717cd10768385f2f5698d30e1633d48767ef2 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 14:51:50 +0100 Subject: [PATCH 12/20] HTMLDocument: add currentScript getter --- src/html/document.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 6e58d0e1..4e933ed5 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -130,6 +130,10 @@ pub const HTMLDocument = struct { return try collection.HTMLCollectionByLinks(try rootNode(self), false); } + pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element { + return null; + } + pub fn deinit(_: *parser.DocumentHTML, _: std.mem.Allocator) void {} }; @@ -160,6 +164,7 @@ pub fn testExecFn( .{ .src = "document.scripts.length", .ex = "0" }, .{ .src = "document.forms.length", .ex = "0" }, .{ .src = "document.links.length", .ex = "1" }, + .{ .src = "document.currentScript", .ex = "null" }, }; try checkCases(js_env, &getters); From 584b254c084d7a6a0c5c72850df99f26798bbf5e Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 15:11:20 +0100 Subject: [PATCH 13/20] HTMLDocument: applets return empty collection --- src/dom/html_collection.zig | 19 +++++++++++++++++++ src/html/document.zig | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index 6421282a..1f04ae84 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -17,10 +17,12 @@ const Matcher = union(enum) { matchByClassName: MatchByClassName, matchByLinks: MatchByLinks, matchTrue: struct {}, + matchFalse: struct {}, pub fn match(self: Matcher, node: *parser.Node) !bool { switch (self) { inline .matchTrue => return true, + inline .matchFalse => return false, inline .matchByTagName => |case| return case.match(node), inline .matchByClassName => |case| return case.match(node), inline .matchByName => |case| return case.match(node), @@ -31,6 +33,7 @@ const Matcher = union(enum) { pub fn deinit(self: Matcher, alloc: std.mem.Allocator) void { switch (self) { inline .matchTrue => return, + inline .matchFalse => return, inline .matchByTagName => |case| return case.deinit(alloc), inline .matchByClassName => |case| return case.deinit(alloc), inline .matchByName => |case| return case.deinit(alloc), @@ -173,6 +176,15 @@ pub fn HTMLCollectionChildren( }; } +pub fn HTMLCollectionEmpty() !HTMLCollection { + return HTMLCollection{ + .root = null, + .walker = Walker{ .walkerNone = .{} }, + .matcher = Matcher{ .matchFalse = .{} }, + .include_root = false, + }; +} + // MatchByLinks matches the a and area elements in the Document that have href // attributes. // https://html.spec.whatwg.org/#dom-document-links @@ -204,6 +216,7 @@ pub fn HTMLCollectionByLinks( const Walker = union(enum) { walkerDepthFirst: WalkerDepthFirst, walkerChildren: WalkerChildren, + walkerNone: WalkerNone, pub fn get_next(self: Walker, root: *parser.Node, cur: ?*parser.Node) !?*parser.Node { switch (self) { @@ -277,6 +290,12 @@ pub const WalkerChildren = struct { } }; +pub const WalkerNone = struct { + pub fn get_next(_: WalkerNone, _: *parser.Node, _: ?*parser.Node) !?*parser.Node { + return null; + } +}; + // WEB IDL https://dom.spec.whatwg.org/#htmlcollection // HTMLCollection is re implemented in zig here because libdom // dom_html_collection expects a comparison function callback as arguement. diff --git a/src/html/document.zig b/src/html/document.zig index 4e933ed5..2e1e52b1 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -126,6 +126,10 @@ pub const HTMLDocument = struct { return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "script", false); } + pub fn get_applets(_: *parser.DocumentHTML) !collection.HTMLCollection { + return try collection.HTMLCollectionEmpty(); + } + pub fn get_links(self: *parser.DocumentHTML) !collection.HTMLCollection { return try collection.HTMLCollectionByLinks(try rootNode(self), false); } @@ -164,6 +168,7 @@ pub fn testExecFn( .{ .src = "document.scripts.length", .ex = "0" }, .{ .src = "document.forms.length", .ex = "0" }, .{ .src = "document.links.length", .ex = "1" }, + .{ .src = "document.applets.length", .ex = "0" }, .{ .src = "document.currentScript", .ex = "null" }, }; try checkCases(js_env, &getters); From 552bf4224c965093dea16314e171f8123403bc27 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 15:11:46 +0100 Subject: [PATCH 14/20] HTMLDocument: pseudo designMode --- src/html/document.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 2e1e52b1..bc182af4 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -138,6 +138,14 @@ pub const HTMLDocument = struct { return null; } + pub fn get_designMode(_: *parser.DocumentHTML) []const u8 { + return "off"; + } + + pub fn set_designMode(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return "off"; + } + pub fn deinit(_: *parser.DocumentHTML, _: std.mem.Allocator) void {} }; From 91d1f539d4963b6422d5dfa73c0720b994352c7e Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Jan 2024 15:16:48 +0100 Subject: [PATCH 15/20] HTMLDocument: implements anchors --- src/dom/html_collection.zig | 30 ++++++++++++++++++++++++++++++ src/html/document.zig | 5 +++++ 2 files changed, 35 insertions(+) diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index 1f04ae84..41e88299 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -16,6 +16,7 @@ const Matcher = union(enum) { matchByTagName: MatchByTagName, matchByClassName: MatchByClassName, matchByLinks: MatchByLinks, + matchByAnchors: MatchByAnchors, matchTrue: struct {}, matchFalse: struct {}, @@ -27,6 +28,7 @@ const Matcher = union(enum) { inline .matchByClassName => |case| return case.match(node), inline .matchByName => |case| return case.match(node), inline .matchByLinks => return MatchByLinks.match(node), + inline .matchByAnchors => return MatchByAnchors.match(node), } } @@ -38,6 +40,7 @@ const Matcher = union(enum) { inline .matchByClassName => |case| return case.deinit(alloc), inline .matchByName => |case| return case.deinit(alloc), inline .matchByLinks => return, + inline .matchByAnchors => return, } } }; @@ -213,6 +216,33 @@ pub fn HTMLCollectionByLinks( }; } +// MatchByAnchors matches the a elements in the Document that have name +// attributes. +// https://html.spec.whatwg.org/#dom-document-anchors +pub const MatchByAnchors = struct { + pub fn match(node: *parser.Node) !bool { + const tag = try parser.nodeName(node); + if (!std.ascii.eqlIgnoreCase(tag, "a")) return false; + + const elem = @as(*parser.Element, @ptrCast(node)); + return parser.elementHasAttribute(elem, "name"); + } +}; + +pub fn HTMLCollectionByAnchors( + root: ?*parser.Node, + include_root: bool, +) !HTMLCollection { + return HTMLCollection{ + .root = root, + .walker = Walker{ .walkerDepthFirst = .{} }, + .matcher = Matcher{ + .matchByAnchors = MatchByAnchors{}, + }, + .include_root = include_root, + }; +} + const Walker = union(enum) { walkerDepthFirst: WalkerDepthFirst, walkerChildren: WalkerChildren, diff --git a/src/html/document.zig b/src/html/document.zig index bc182af4..4fb05692 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -134,6 +134,10 @@ pub const HTMLDocument = struct { return try collection.HTMLCollectionByLinks(try rootNode(self), false); } + pub fn get_anchors(self: *parser.DocumentHTML) !collection.HTMLCollection { + return try collection.HTMLCollectionByAnchors(try rootNode(self), false); + } + pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element { return null; } @@ -177,6 +181,7 @@ pub fn testExecFn( .{ .src = "document.forms.length", .ex = "0" }, .{ .src = "document.links.length", .ex = "1" }, .{ .src = "document.applets.length", .ex = "0" }, + .{ .src = "document.anchors.length", .ex = "0" }, .{ .src = "document.currentScript", .ex = "null" }, }; try checkCases(js_env, &getters); From fce9e444079c0cf17307760842f21f2baa5a0f4b Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 5 Jan 2024 10:08:40 +0100 Subject: [PATCH 16/20] HTMLDocument: add noop legacy functions --- src/html/document.zig | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/html/document.zig b/src/html/document.zig index 4fb05692..b9344427 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -150,6 +150,43 @@ pub const HTMLDocument = struct { return "off"; } + // noop legacy functions + // https://html.spec.whatwg.org/#Document-partial + pub fn _clear(_: *parser.DocumentHTML) void {} + pub fn _captureEvents(_: *parser.DocumentHTML) void {} + pub fn _releaseEvents(_: *parser.DocumentHTML) void {} + + pub fn get_fgColor(_: *parser.DocumentHTML) []const u8 { + return ""; + } + pub fn set_fgColor(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return ""; + } + pub fn get_linkColor(_: *parser.DocumentHTML) []const u8 { + return ""; + } + pub fn set_linkColor(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return ""; + } + pub fn get_vlinkColor(_: *parser.DocumentHTML) []const u8 { + return ""; + } + pub fn set_vlinkColor(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return ""; + } + pub fn get_alinkColor(_: *parser.DocumentHTML) []const u8 { + return ""; + } + pub fn set_alinkColor(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return ""; + } + pub fn get_bgColor(_: *parser.DocumentHTML) []const u8 { + return ""; + } + pub fn set_bgColor(_: *parser.DocumentHTML, _: []const u8) []const u8 { + return ""; + } + pub fn deinit(_: *parser.DocumentHTML, _: std.mem.Allocator) void {} }; From c73ecb5e0db57eae10bf3ce64d20946717af649c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 5 Jan 2024 10:28:09 +0100 Subject: [PATCH 17/20] HTMLDocument: implement all getter --- src/dom/html_collection.zig | 12 ++++++++++++ src/html/document.zig | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index 41e88299..4e00b7a9 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -167,6 +167,18 @@ pub fn HTMLCollectionByName( }; } +pub fn HTMLCollectionAll( + root: ?*parser.Node, + include_root: bool, +) !HTMLCollection { + return HTMLCollection{ + .root = root, + .walker = Walker{ .walkerDepthFirst = .{} }, + .matcher = Matcher{ .matchTrue = .{} }, + .include_root = include_root, + }; +} + pub fn HTMLCollectionChildren( root: ?*parser.Node, include_root: bool, diff --git a/src/html/document.zig b/src/html/document.zig index b9344427..ec40ffe1 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -138,6 +138,10 @@ pub const HTMLDocument = struct { return try collection.HTMLCollectionByAnchors(try rootNode(self), false); } + pub fn get_all(self: *parser.DocumentHTML) !collection.HTMLCollection { + return try collection.HTMLCollectionAll(try rootNode(self), true); + } + pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element { return null; } @@ -219,6 +223,7 @@ pub fn testExecFn( .{ .src = "document.links.length", .ex = "1" }, .{ .src = "document.applets.length", .ex = "0" }, .{ .src = "document.anchors.length", .ex = "0" }, + .{ .src = "document.all.length", .ex = "8" }, .{ .src = "document.currentScript", .ex = "null" }, }; try checkCases(js_env, &getters); From a25169c9e82780b1d9cac5f652208c861b855f31 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 9 Jan 2024 13:56:17 +0100 Subject: [PATCH 18/20] documentHTML: replace NotSupported with NotImplemented --- src/html/document.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/html/document.zig b/src/html/document.zig index ec40ffe1..f6f608ff 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -28,7 +28,7 @@ pub const HTMLDocument = struct { } pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { - return parser.DOMError.NotSupported; + return error.NotImplemented; } pub fn get_referrer(self: *parser.DocumentHTML) ![]const u8 { @@ -36,7 +36,7 @@ pub const HTMLDocument = struct { } pub fn set_referrer(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { - return parser.DOMError.NotSupported; + return error.NotImplemented; } pub fn get_body(self: *parser.DocumentHTML) !?*parser.Body { @@ -67,7 +67,7 @@ pub const HTMLDocument = struct { // TODO: not implemented by libdom pub fn set_cookie(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { - return parser.DOMError.NotSupported; + return error.NotImplemented; } pub fn get_title(self: *parser.DocumentHTML) ![]const u8 { From be1e55272a7f49fe1a0d9a9f53535d9698289f36 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 9 Jan 2024 14:11:45 +0100 Subject: [PATCH 19/20] DocumentHTML: cast directly document to node --- src/html/document.zig | 25 +++++++++---------------- src/netsurf.zig | 5 +++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/html/document.zig b/src/html/document.zig index f6f608ff..36be1b3b 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -49,7 +49,7 @@ pub const HTMLDocument = struct { } pub fn get_head(self: *parser.DocumentHTML) !?*parser.Head { - const root = try rootNode(self) orelse return null; + const root = parser.documentHTMLToNode(self); const walker = Walker{}; var next: ?*parser.Node = null; while (true) { @@ -85,8 +85,7 @@ pub const HTMLDocument = struct { if (name.len == 0) return list; - const root = try rootNode(self) orelse return list; - + const root = parser.documentHTMLToNode(self); var c = try collection.HTMLCollectionByName(alloc, root, name, false); const ln = try c.get_length(); @@ -100,18 +99,12 @@ pub const HTMLDocument = struct { return list; } - inline fn rootNode(self: *parser.DocumentHTML) !?*parser.Node { - const doc = parser.documentHTMLToDocument(self); - const elt = try parser.documentGetDocumentElement(doc) orelse return null; - return parser.elementToNode(elt); - } - pub fn get_images(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { - return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "img", false); + return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "img", false); } pub fn get_embeds(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { - return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "embed", false); + return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "embed", false); } pub fn get_plugins(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { @@ -119,11 +112,11 @@ pub const HTMLDocument = struct { } pub fn get_forms(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { - return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "form", false); + return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "form", false); } pub fn get_scripts(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection { - return try collection.HTMLCollectionByTagName(alloc, try rootNode(self), "script", false); + return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "script", false); } pub fn get_applets(_: *parser.DocumentHTML) !collection.HTMLCollection { @@ -131,15 +124,15 @@ pub const HTMLDocument = struct { } pub fn get_links(self: *parser.DocumentHTML) !collection.HTMLCollection { - return try collection.HTMLCollectionByLinks(try rootNode(self), false); + return try collection.HTMLCollectionByLinks(parser.documentHTMLToNode(self), false); } pub fn get_anchors(self: *parser.DocumentHTML) !collection.HTMLCollection { - return try collection.HTMLCollectionByAnchors(try rootNode(self), false); + return try collection.HTMLCollectionByAnchors(parser.documentHTMLToNode(self), false); } pub fn get_all(self: *parser.DocumentHTML) !collection.HTMLCollection { - return try collection.HTMLCollectionAll(try rootNode(self), true); + return try collection.HTMLCollectionAll(parser.documentHTMLToNode(self), true); } pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element { diff --git a/src/netsurf.zig b/src/netsurf.zig index 2e021e24..a8af4bbb 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1357,6 +1357,11 @@ pub inline fn documentCreateAttributeNS(doc: *Document, ns: []const u8, qname: [ // DocumentHTML pub const DocumentHTML = c.dom_html_document; +// documentHTMLToNode is an helper to convert a documentHTML to an node. +pub inline fn documentHTMLToNode(doc: *DocumentHTML) *Node { + return @as(*Node, @ptrCast(doc)); +} + fn documentHTMLVtable(doc_html: *DocumentHTML) c.dom_html_document_vtable { return getVtable(c.dom_html_document_vtable, DocumentHTML, doc_html); } From ca6bb577c67514ffd6caf26c84abfa9f7176bc07 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 9 Jan 2024 14:24:55 +0100 Subject: [PATCH 20/20] Document: use document cast to node --- src/dom/document.zig | 22 ++++------------------ src/netsurf.zig | 4 ++++ 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index 5157e86b..be7581f0 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -108,12 +108,7 @@ pub const Document = struct { alloc: std.mem.Allocator, tag_name: []const u8, ) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - - return try collection.HTMLCollectionByTagName(alloc, elt, tag_name, true); + return try collection.HTMLCollectionByTagName(alloc, parser.documentToNode(self), tag_name, true); } pub fn _getElementsByClassName( @@ -121,12 +116,7 @@ pub const Document = struct { alloc: std.mem.Allocator, classNames: []const u8, ) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - - return try collection.HTMLCollectionByClassName(alloc, elt, classNames, true); + return try collection.HTMLCollectionByClassName(alloc, parser.documentToNode(self), classNames, true); } pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment { @@ -170,11 +160,7 @@ pub const Document = struct { // ParentNode // https://dom.spec.whatwg.org/#parentnode pub fn get_children(self: *parser.Document) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - return try collection.HTMLCollectionChildren(elt, true); + return try collection.HTMLCollectionChildren(parser.documentToNode(self), false); } pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion { @@ -219,7 +205,7 @@ pub const Document = struct { // catch-all, return all elements if (selectors[0] == '*') { // walk over the node tree fo find the node by id. - const root = parser.elementToNode(try parser.documentGetDocumentElement(self) orelse return list); + const root = parser.documentToNode(self); const walker = Walker{}; var next: ?*parser.Node = null; while (true) { diff --git a/src/netsurf.zig b/src/netsurf.zig index a8af4bbb..4dca3fb6 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1216,6 +1216,10 @@ fn documentVtable(doc: *Document) c.dom_document_vtable { return getVtable(c.dom_document_vtable, Document, doc); } +pub inline fn documentToNode(doc: *Document) *Node { + return @as(*Node, @ptrCast(doc)); +} + pub inline fn documentGetElementById(doc: *Document, id: []const u8) !?*Element { var elem: ?*Element = undefined; const err = documentVtable(doc).dom_document_get_element_by_id.?(doc, try strFromData(id), &elem);