Skip to content

Commit 7b35bb4

Browse files
committed
dom: improve location impl
1 parent 318e2bd commit 7b35bb4

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/browser/browser.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const apiweb = @import("../apiweb.zig");
3636
const Window = @import("../html/window.zig").Window;
3737
const Walker = @import("../dom/walker.zig").WalkerDepthFirst;
3838

39+
const URL = @import("../url/url.zig").URL;
40+
const Location = @import("../html/location.zig").Location;
41+
3942
const storage = @import("../storage/storage.zig");
4043

4144
const FetchResult = @import("../http/Client.zig").Client.FetchResult;
@@ -102,7 +105,9 @@ pub const Session = struct {
102105
loader: Loader,
103106
env: Env = undefined,
104107
inspector: ?jsruntime.Inspector = null,
108+
105109
window: Window,
110+
106111
// TODO move the shed to the browser?
107112
storageShed: storage.Shed,
108113
page: ?Page = null,
@@ -201,6 +206,10 @@ pub const Page = struct {
201206
uri: std.Uri = undefined,
202207
origin: ?[]const u8 = null,
203208

209+
// html url and location
210+
url: ?URL = null,
211+
location: Location = .{},
212+
204213
raw_data: ?[]const u8 = null,
205214

206215
fn init(
@@ -244,6 +253,13 @@ pub const Page = struct {
244253
self.session.env.stop();
245254
// TODO unload document: https://html.spec.whatwg.org/#unloading-documents
246255

256+
if (self.url) |*u| u.deinit(self.arena.allocator());
257+
self.url = null;
258+
self.location.url = null;
259+
self.session.window.replaceLocation(&self.location) catch |e| {
260+
log.err("reset window location: {any}", .{e});
261+
};
262+
247263
// clear netsurf memory arena.
248264
parser.deinit();
249265

@@ -308,6 +324,11 @@ pub const Page = struct {
308324
self.rawuri = try alloc.dupe(u8, uri);
309325
self.uri = std.Uri.parse(self.rawuri.?) catch try std.Uri.parseAfterScheme("", self.rawuri.?);
310326

327+
if (self.url) |*prev| prev.deinit(alloc);
328+
self.url = try URL.constructor(alloc, self.rawuri.?, null);
329+
self.location.url = &self.url.?;
330+
try self.session.window.replaceLocation(&self.location);
331+
311332
// prepare origin value.
312333
var buf = std.ArrayList(u8).init(alloc);
313334
defer buf.deinit();

src/html/location.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,16 @@ pub fn testExecFn(
114114
js_env: *jsruntime.Env,
115115
) anyerror!void {
116116
var location = [_]Case{
117-
.{ .src = "location.href", .ex = "" },
118-
.{ .src = "document.location.href", .ex = "" },
117+
.{ .src = "location.href", .ex = "https://lightpanda.io/opensource-browser/" },
118+
.{ .src = "document.location.href", .ex = "https://lightpanda.io/opensource-browser/" },
119+
120+
.{ .src = "location.host", .ex = "lightpanda.io" },
121+
.{ .src = "location.hostname", .ex = "lightpanda.io" },
122+
.{ .src = "location.origin", .ex = "https://lightpanda.io" },
123+
.{ .src = "location.pathname", .ex = "/opensource-browser/" },
124+
.{ .src = "location.hash", .ex = "" },
125+
.{ .src = "location.port", .ex = "" },
126+
.{ .src = "location.search", .ex = "" },
119127
};
120128
try checkCases(js_env, &location);
121129
}

src/html/window.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const Location = @import("location.zig").Location;
3131

3232
const storage = @import("../storage/storage.zig");
3333

34+
var emptyLocation = Location{};
35+
3436
// https://dom.spec.whatwg.org/#interface-window-extensions
3537
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
3638
pub const Window = struct {
@@ -44,7 +46,7 @@ pub const Window = struct {
4446
document: ?*parser.DocumentHTML = null,
4547
target: []const u8,
4648
history: History = .{},
47-
location: Location = .{},
49+
location: *Location = &emptyLocation,
4850

4951
storageShelf: ?*storage.Shelf = null,
5052

@@ -62,17 +64,17 @@ pub const Window = struct {
6264
};
6365
}
6466

65-
pub fn replaceLocation(self: *Window, loc: Location) !void {
67+
pub fn replaceLocation(self: *Window, loc: *Location) !void {
6668
self.location = loc;
6769

68-
if (self.doc != null) {
69-
try parser.documentHTMLSetLocation(Location, self.doc.?, &self.location);
70+
if (self.document != null) {
71+
try parser.documentHTMLSetLocation(Location, self.document.?, self.location);
7072
}
7173
}
7274

7375
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
7476
self.document = doc;
75-
try parser.documentHTMLSetLocation(Location, doc, &self.location);
77+
try parser.documentHTMLSetLocation(Location, doc, self.location);
7678
}
7779

7880
pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void {
@@ -88,7 +90,7 @@ pub const Window = struct {
8890
}
8991

9092
pub fn get_location(self: *Window) *Location {
91-
return &self.location;
93+
return self.location;
9294
}
9395

9496
pub fn get_self(self: *Window) *Window {

src/run_tests.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ const Window = @import("html/window.zig").Window;
2929
const xhr = @import("xhr/xhr.zig");
3030
const storage = @import("storage/storage.zig");
3131
const url = @import("url/url.zig");
32+
const URL = url.URL;
3233
const urlquery = @import("url/query.zig");
3334
const Client = @import("asyncio").Client;
35+
const Location = @import("html/location.zig").Location;
3436

3537
const documentTestExecFn = @import("dom/document.zig").testExecFn;
3638
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
@@ -98,6 +100,11 @@ fn testExecFn(
98100
// alias global as self and window
99101
var window = Window.create(null, null);
100102

103+
var u = try URL.constructor(alloc, "https://lightpanda.io/opensource-browser/", null);
104+
defer u.deinit(alloc);
105+
var location = Location{ .url = &u };
106+
try window.replaceLocation(&location);
107+
101108
try window.replaceDocument(doc);
102109
window.setStorageShelf(&storageShelf);
103110

0 commit comments

Comments
 (0)