dom: improve location impl

This commit is contained in:
Pierre Tachoire
2025-01-24 16:42:48 +01:00
parent 318e2bd1c6
commit 7b35bb4c0f
4 changed files with 46 additions and 8 deletions

View File

@@ -114,8 +114,16 @@ pub fn testExecFn(
js_env: *jsruntime.Env,
) anyerror!void {
var location = [_]Case{
.{ .src = "location.href", .ex = "" },
.{ .src = "document.location.href", .ex = "" },
.{ .src = "location.href", .ex = "https://lightpanda.io/opensource-browser/" },
.{ .src = "document.location.href", .ex = "https://lightpanda.io/opensource-browser/" },
.{ .src = "location.host", .ex = "lightpanda.io" },
.{ .src = "location.hostname", .ex = "lightpanda.io" },
.{ .src = "location.origin", .ex = "https://lightpanda.io" },
.{ .src = "location.pathname", .ex = "/opensource-browser/" },
.{ .src = "location.hash", .ex = "" },
.{ .src = "location.port", .ex = "" },
.{ .src = "location.search", .ex = "" },
};
try checkCases(js_env, &location);
}

View File

@@ -31,6 +31,8 @@ const Location = @import("location.zig").Location;
const storage = @import("../storage/storage.zig");
var emptyLocation = Location{};
// https://dom.spec.whatwg.org/#interface-window-extensions
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
pub const Window = struct {
@@ -44,7 +46,7 @@ pub const Window = struct {
document: ?*parser.DocumentHTML = null,
target: []const u8,
history: History = .{},
location: Location = .{},
location: *Location = &emptyLocation,
storageShelf: ?*storage.Shelf = null,
@@ -62,17 +64,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);
if (self.document != null) {
try parser.documentHTMLSetLocation(Location, self.document.?, self.location);
}
}
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
self.document = doc;
try parser.documentHTMLSetLocation(Location, doc, &self.location);
try parser.documentHTMLSetLocation(Location, doc, self.location);
}
pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void {
@@ -88,7 +90,7 @@ pub const Window = struct {
}
pub fn get_location(self: *Window) *Location {
return &self.location;
return self.location;
}
pub fn get_self(self: *Window) *Window {