diff --git a/src/html/html.zig b/src/html/html.zig
index 29b467cb..43535e17 100644
--- a/src/html/html.zig
+++ b/src/html/html.zig
@@ -23,6 +23,7 @@ const HTMLElem = @import("elements.zig");
const Window = @import("window.zig").Window;
const Navigator = @import("navigator.zig").Navigator;
const History = @import("history.zig").History;
+const Location = @import("location.zig").Location;
pub const Interfaces = generate.Tuple(.{
HTMLDocument,
@@ -32,4 +33,5 @@ pub const Interfaces = generate.Tuple(.{
Window,
Navigator,
History,
+ Location,
});
diff --git a/src/html/location.zig b/src/html/location.zig
new file mode 100644
index 00000000..ac72d1ce
--- /dev/null
+++ b/src/html/location.zig
@@ -0,0 +1,120 @@
+// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier
+// Pierre Tachoire
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see .
+
+const std = @import("std");
+
+const builtin = @import("builtin");
+const jsruntime = @import("jsruntime");
+
+const URL = @import("../url/url.zig").URL;
+
+const Case = jsruntime.test_utils.Case;
+const checkCases = jsruntime.test_utils.checkCases;
+
+// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface
+pub const Location = struct {
+ pub const mem_guarantied = true;
+
+ url: ?*URL = null,
+
+ pub fn deinit(_: *Location, _: std.mem.Allocator) void {}
+
+ pub fn get_href(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_href(alloc);
+
+ return "";
+ }
+
+ pub fn get_protocol(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_protocol(alloc);
+
+ return "";
+ }
+
+ pub fn get_host(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_host(alloc);
+
+ return "";
+ }
+
+ pub fn get_hostname(self: *Location) []const u8 {
+ if (self.url) |u| return u.get_hostname();
+
+ return "";
+ }
+
+ pub fn get_port(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_port(alloc);
+
+ return "";
+ }
+
+ pub fn get_pathname(self: *Location) []const u8 {
+ if (self.url) |u| return u.get_pathname();
+
+ return "";
+ }
+
+ pub fn get_search(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_search(alloc);
+
+ return "";
+ }
+
+ pub fn get_hash(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_hash(alloc);
+
+ return "";
+ }
+
+ pub fn get_origin(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ if (self.url) |u| return u.get_origin(alloc);
+
+ return "";
+ }
+
+ // TODO
+ pub fn _assign(_: *Location, url: []const u8) !void {
+ _ = url;
+ }
+
+ // TODO
+ pub fn _replace(_: *Location, url: []const u8) !void {
+ _ = url;
+ }
+
+ // TODO
+ pub fn _reload(_: *Location) !void {}
+
+ pub fn _toString(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
+ return try self.get_href(alloc);
+ }
+};
+
+// Tests
+// -----
+
+pub fn testExecFn(
+ _: std.mem.Allocator,
+ js_env: *jsruntime.Env,
+) anyerror!void {
+ var location = [_]Case{
+ .{ .src = "location.href", .ex = "" },
+ };
+ try checkCases(js_env, &location);
+}
diff --git a/src/html/window.zig b/src/html/window.zig
index 65a8af67..0a73adc0 100644
--- a/src/html/window.zig
+++ b/src/html/window.zig
@@ -27,6 +27,7 @@ const Loop = jsruntime.Loop;
const EventTarget = @import("../dom/event_target.zig").EventTarget;
const Navigator = @import("navigator.zig").Navigator;
const History = @import("history.zig").History;
+const Location = @import("location.zig").Location;
const storage = @import("../storage/storage.zig");
@@ -43,6 +44,7 @@ pub const Window = struct {
document: ?*parser.DocumentHTML = null,
target: []const u8,
history: History = .{},
+ location: Location = .{},
storageShelf: ?*storage.Shelf = null,
@@ -60,6 +62,10 @@ pub const Window = struct {
};
}
+ pub fn replaceLocation(self: *Window, loc: Location) void {
+ self.location = loc;
+ }
+
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void {
self.document = doc;
}
@@ -76,6 +82,10 @@ pub const Window = struct {
return &self.navigator;
}
+ pub fn get_location(self: *Window) *Location {
+ return &self.location;
+ }
+
pub fn get_self(self: *Window) *Window {
return self;
}
diff --git a/src/run_tests.zig b/src/run_tests.zig
index 838a6047..c0ca2e2a 100644
--- a/src/run_tests.zig
+++ b/src/run_tests.zig
@@ -139,6 +139,7 @@ fn testsAllExecFn(
@import("polyfill/fetch.zig").testExecFn,
@import("html/navigator.zig").testExecFn,
@import("html/history.zig").testExecFn,
+ @import("html/location.zig").testExecFn,
};
inline for (testFns) |testFn| {