Skip to content

Commit d7069df

Browse files
committed
dom: first draft for location
1 parent 579714a commit d7069df

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

src/html/html.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const HTMLElem = @import("elements.zig");
2323
const Window = @import("window.zig").Window;
2424
const Navigator = @import("navigator.zig").Navigator;
2525
const History = @import("history.zig").History;
26+
const Location = @import("location.zig").Location;
2627

2728
pub const Interfaces = generate.Tuple(.{
2829
HTMLDocument,
@@ -32,4 +33,5 @@ pub const Interfaces = generate.Tuple(.{
3233
Window,
3334
Navigator,
3435
History,
36+
Location,
3537
});

src/html/location.zig

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const builtin = @import("builtin");
22+
const jsruntime = @import("jsruntime");
23+
24+
const URL = @import("../url/url.zig").URL;
25+
26+
const Case = jsruntime.test_utils.Case;
27+
const checkCases = jsruntime.test_utils.checkCases;
28+
29+
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface
30+
pub const Location = struct {
31+
pub const mem_guarantied = true;
32+
33+
url: ?*URL = null,
34+
35+
pub fn deinit(_: *Location, _: std.mem.Allocator) void {}
36+
37+
pub fn get_href(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
38+
if (self.url) |u| return u.get_href(alloc);
39+
40+
return "";
41+
}
42+
43+
pub fn get_protocol(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
44+
if (self.url) |u| return u.get_protocol(alloc);
45+
46+
return "";
47+
}
48+
49+
pub fn get_host(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
50+
if (self.url) |u| return u.get_host(alloc);
51+
52+
return "";
53+
}
54+
55+
pub fn get_hostname(self: *Location) []const u8 {
56+
if (self.url) |u| return u.get_hostname();
57+
58+
return "";
59+
}
60+
61+
pub fn get_port(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
62+
if (self.url) |u| return u.get_port(alloc);
63+
64+
return "";
65+
}
66+
67+
pub fn get_pathname(self: *Location) []const u8 {
68+
if (self.url) |u| return u.get_pathname();
69+
70+
return "";
71+
}
72+
73+
pub fn get_search(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
74+
if (self.url) |u| return u.get_search(alloc);
75+
76+
return "";
77+
}
78+
79+
pub fn get_hash(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
80+
if (self.url) |u| return u.get_hash(alloc);
81+
82+
return "";
83+
}
84+
85+
pub fn get_origin(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
86+
if (self.url) |u| return u.get_origin(alloc);
87+
88+
return "";
89+
}
90+
91+
// TODO
92+
pub fn _assign(_: *Location, url: []const u8) !void {
93+
_ = url;
94+
}
95+
96+
// TODO
97+
pub fn _replace(_: *Location, url: []const u8) !void {
98+
_ = url;
99+
}
100+
101+
// TODO
102+
pub fn _reload(_: *Location) !void {}
103+
104+
pub fn _toString(self: *Location, alloc: std.mem.Allocator) ![]const u8 {
105+
return try self.get_href(alloc);
106+
}
107+
};
108+
109+
// Tests
110+
// -----
111+
112+
pub fn testExecFn(
113+
_: std.mem.Allocator,
114+
js_env: *jsruntime.Env,
115+
) anyerror!void {
116+
var location = [_]Case{
117+
.{ .src = "location.href", .ex = "" },
118+
};
119+
try checkCases(js_env, &location);
120+
}

src/html/window.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Loop = jsruntime.Loop;
2727
const EventTarget = @import("../dom/event_target.zig").EventTarget;
2828
const Navigator = @import("navigator.zig").Navigator;
2929
const History = @import("history.zig").History;
30+
const Location = @import("location.zig").Location;
3031

3132
const storage = @import("../storage/storage.zig");
3233

@@ -43,6 +44,7 @@ pub const Window = struct {
4344
document: ?*parser.DocumentHTML = null,
4445
target: []const u8,
4546
history: History = .{},
47+
location: Location = .{},
4648

4749
storageShelf: ?*storage.Shelf = null,
4850

@@ -60,6 +62,10 @@ pub const Window = struct {
6062
};
6163
}
6264

65+
pub fn replaceLocation(self: *Window, loc: Location) void {
66+
self.location = loc;
67+
}
68+
6369
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void {
6470
self.document = doc;
6571
}
@@ -76,6 +82,10 @@ pub const Window = struct {
7682
return &self.navigator;
7783
}
7884

85+
pub fn get_location(self: *Window) *Location {
86+
return &self.location;
87+
}
88+
7989
pub fn get_self(self: *Window) *Window {
8090
return self;
8191
}

src/run_tests.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ fn testsAllExecFn(
139139
@import("polyfill/fetch.zig").testExecFn,
140140
@import("html/navigator.zig").testExecFn,
141141
@import("html/history.zig").testExecFn,
142+
@import("html/location.zig").testExecFn,
142143
};
143144

144145
inline for (testFns) |testFn| {

0 commit comments

Comments
 (0)