Skip to content

Commit 318e2bd

Browse files
committed
dom: expose document.location
1 parent 09ba4bc commit 318e2bd

File tree

8 files changed

+28
-12
lines changed

8 files changed

+28
-12
lines changed

src/browser/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub const Page = struct {
391391

392392
// TODO set the referrer to the document.
393393

394-
self.session.window.replaceDocument(html_doc);
394+
try self.session.window.replaceDocument(html_doc);
395395
self.session.window.setStorageShelf(
396396
try self.session.storageShed.getOrPut(self.origin orelse "null"),
397397
);

src/html/document.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const Node = @import("../dom/node.zig").Node;
2828
const Document = @import("../dom/document.zig").Document;
2929
const NodeList = @import("../dom/nodelist.zig").NodeList;
3030
const HTMLElem = @import("elements.zig");
31+
const Location = @import("location.zig").Location;
3132

3233
const collection = @import("../dom/html_collection.zig");
3334
const Walker = @import("../dom/walker.zig").WalkerDepthFirst;
@@ -157,6 +158,10 @@ pub const HTMLDocument = struct {
157158
return try parser.documentHTMLGetCurrentScript(self);
158159
}
159160

161+
pub fn get_location(self: *parser.DocumentHTML) !?*Location {
162+
return try parser.documentHTMLGetLocation(Location, self);
163+
}
164+
160165
pub fn get_designMode(_: *parser.DocumentHTML) []const u8 {
161166
return "off";
162167
}

src/html/location.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn testExecFn(
115115
) anyerror!void {
116116
var location = [_]Case{
117117
.{ .src = "location.href", .ex = "" },
118+
.{ .src = "document.location.href", .ex = "" },
118119
};
119120
try checkCases(js_env, &location);
120121
}

src/html/window.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ pub const Window = struct {
6262
};
6363
}
6464

65-
pub fn replaceLocation(self: *Window, loc: Location) void {
65+
pub fn replaceLocation(self: *Window, loc: Location) !void {
6666
self.location = loc;
67+
68+
if (self.doc != null) {
69+
try parser.documentHTMLSetLocation(Location, self.doc.?, &self.location);
70+
}
6771
}
6872

69-
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void {
73+
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
7074
self.document = doc;
75+
try parser.documentHTMLSetLocation(Location, doc, &self.location);
7176
}
7277

7378
pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void {

src/main_shell.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn execJS(
5555

5656
// alias global as self and window
5757
var window = Window.create(null, null);
58-
window.replaceDocument(doc);
58+
try window.replaceDocument(doc);
5959
window.setStorageShelf(&storageShelf);
6060
try js_env.bindGlobal(window);
6161

src/netsurf/netsurf.zig

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,14 +2265,19 @@ pub fn documentHTMLGetCurrentScript(doc: *DocumentHTML) !?*Script {
22652265
return @ptrCast(elem.?);
22662266
}
22672267

2268-
pub fn documentHTMLSetLocation(doc: *DocumentHTML, location: ?*anyopaque) !void {
2269-
const err = documentHTMLVtable(doc).set_location.?(doc, location);
2268+
pub fn documentHTMLSetLocation(T: type, doc: *DocumentHTML, location: *T) !void {
2269+
const l = @as(*anyopaque, @ptrCast(location));
2270+
const err = documentHTMLVtable(doc).set_location.?(doc, l);
22702271
try DOMErr(err);
22712272
}
22722273

2273-
pub fn documentHTMLGetLocation(doc: *DocumentHTML) !?*anyopaque {
2274-
var loc: ?*anyopaque = undefined;
2275-
const err = documentHTMLVtable(doc).get_location.?(doc, &loc);
2274+
pub fn documentHTMLGetLocation(T: type, doc: *DocumentHTML) !?*T {
2275+
var l: ?*anyopaque = undefined;
2276+
const err = documentHTMLVtable(doc).get_location.?(doc, &l);
22762277
try DOMErr(err);
2277-
return loc;
2278+
2279+
if (l == null) return null;
2280+
2281+
const ptr: *align(@alignOf(*T)) anyopaque = @alignCast(l.?);
2282+
return @as(*T, @ptrCast(ptr));
22782283
}

src/run_tests.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn testExecFn(
9898
// alias global as self and window
9999
var window = Window.create(null, null);
100100

101-
window.replaceDocument(doc);
101+
try window.replaceDocument(doc);
102102
window.setStorageShelf(&storageShelf);
103103

104104
try js_env.bindGlobal(window);

src/wpt/run.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
9191

9292
// setup global env vars.
9393
var window = Window.create(null, null);
94-
window.replaceDocument(html_doc);
94+
try window.replaceDocument(html_doc);
9595
window.setStorageShelf(&storageShelf);
9696
try js_env.bindGlobal(&window);
9797

0 commit comments

Comments
 (0)