From 318e2bd1c6c7848f11905af2a199066dadb1eeea Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 23 Jan 2025 17:08:02 +0100 Subject: [PATCH] dom: expose document.location --- src/browser/browser.zig | 2 +- src/html/document.zig | 5 +++++ src/html/location.zig | 1 + src/html/window.zig | 9 +++++++-- src/main_shell.zig | 2 +- src/netsurf/netsurf.zig | 17 +++++++++++------ src/run_tests.zig | 2 +- src/wpt/run.zig | 2 +- 8 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/browser/browser.zig b/src/browser/browser.zig index c421498c..65aafa83 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -391,7 +391,7 @@ pub const Page = struct { // TODO set the referrer to the document. - self.session.window.replaceDocument(html_doc); + try self.session.window.replaceDocument(html_doc); self.session.window.setStorageShelf( try self.session.storageShed.getOrPut(self.origin orelse "null"), ); diff --git a/src/html/document.zig b/src/html/document.zig index 78ffa864..0f278f0d 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -28,6 +28,7 @@ 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 Location = @import("location.zig").Location; const collection = @import("../dom/html_collection.zig"); const Walker = @import("../dom/walker.zig").WalkerDepthFirst; @@ -157,6 +158,10 @@ pub const HTMLDocument = struct { return try parser.documentHTMLGetCurrentScript(self); } + pub fn get_location(self: *parser.DocumentHTML) !?*Location { + return try parser.documentHTMLGetLocation(Location, self); + } + pub fn get_designMode(_: *parser.DocumentHTML) []const u8 { return "off"; } diff --git a/src/html/location.zig b/src/html/location.zig index ac72d1ce..b1ec2913 100644 --- a/src/html/location.zig +++ b/src/html/location.zig @@ -115,6 +115,7 @@ pub fn testExecFn( ) anyerror!void { var location = [_]Case{ .{ .src = "location.href", .ex = "" }, + .{ .src = "document.location.href", .ex = "" }, }; try checkCases(js_env, &location); } diff --git a/src/html/window.zig b/src/html/window.zig index 0a73adc0..b5530857 100644 --- a/src/html/window.zig +++ b/src/html/window.zig @@ -62,12 +62,17 @@ pub const Window = struct { }; } - pub fn replaceLocation(self: *Window, loc: Location) void { + pub fn replaceLocation(self: *Window, loc: Location) !void { self.location = loc; + + if (self.doc != null) { + try parser.documentHTMLSetLocation(Location, self.doc.?, &self.location); + } } - pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void { + pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void { self.document = doc; + try parser.documentHTMLSetLocation(Location, doc, &self.location); } pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void { diff --git a/src/main_shell.zig b/src/main_shell.zig index 362f68e1..dff00963 100644 --- a/src/main_shell.zig +++ b/src/main_shell.zig @@ -55,7 +55,7 @@ fn execJS( // alias global as self and window var window = Window.create(null, null); - window.replaceDocument(doc); + try window.replaceDocument(doc); window.setStorageShelf(&storageShelf); try js_env.bindGlobal(window); diff --git a/src/netsurf/netsurf.zig b/src/netsurf/netsurf.zig index 1019b153..97716683 100644 --- a/src/netsurf/netsurf.zig +++ b/src/netsurf/netsurf.zig @@ -2265,14 +2265,19 @@ pub fn documentHTMLGetCurrentScript(doc: *DocumentHTML) !?*Script { return @ptrCast(elem.?); } -pub fn documentHTMLSetLocation(doc: *DocumentHTML, location: ?*anyopaque) !void { - const err = documentHTMLVtable(doc).set_location.?(doc, location); +pub fn documentHTMLSetLocation(T: type, doc: *DocumentHTML, location: *T) !void { + const l = @as(*anyopaque, @ptrCast(location)); + const err = documentHTMLVtable(doc).set_location.?(doc, l); try DOMErr(err); } -pub fn documentHTMLGetLocation(doc: *DocumentHTML) !?*anyopaque { - var loc: ?*anyopaque = undefined; - const err = documentHTMLVtable(doc).get_location.?(doc, &loc); +pub fn documentHTMLGetLocation(T: type, doc: *DocumentHTML) !?*T { + var l: ?*anyopaque = undefined; + const err = documentHTMLVtable(doc).get_location.?(doc, &l); try DOMErr(err); - return loc; + + if (l == null) return null; + + const ptr: *align(@alignOf(*T)) anyopaque = @alignCast(l.?); + return @as(*T, @ptrCast(ptr)); } diff --git a/src/run_tests.zig b/src/run_tests.zig index c0ca2e2a..7481d266 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -98,7 +98,7 @@ fn testExecFn( // alias global as self and window var window = Window.create(null, null); - window.replaceDocument(doc); + try window.replaceDocument(doc); window.setStorageShelf(&storageShelf); try js_env.bindGlobal(window); diff --git a/src/wpt/run.zig b/src/wpt/run.zig index 208078b9..3a00db6b 100644 --- a/src/wpt/run.zig +++ b/src/wpt/run.zig @@ -91,7 +91,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const // setup global env vars. var window = Window.create(null, null); - window.replaceDocument(html_doc); + try window.replaceDocument(html_doc); window.setStorageShelf(&storageShelf); try js_env.bindGlobal(&window);