Skip to content

Commit 40c9355

Browse files
Merge pull request #355 from lightpanda-io/history
dom: history placeholder
2 parents 73bb14e + 8f15572 commit 40c9355

File tree

5 files changed

+81
-48
lines changed

5 files changed

+81
-48
lines changed

src/html/history.zig

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,70 @@ const checkCases = jsruntime.test_utils.checkCases;
2828
pub const History = struct {
2929
pub const mem_guarantied = true;
3030

31-
const ScrollRestaurationMode = enum {
31+
const ScrollRestorationMode = enum {
3232
auto,
3333
manual,
3434
};
3535

36-
scrollRestauration: ScrollRestaurationMode = .audio,
36+
scrollRestoration: ScrollRestorationMode = .auto,
37+
state: std.json.Value = .null,
3738

38-
pub fn get_length(_: *History) u64 {
39-
return 0;
40-
}
39+
// count tracks the history length until we implement correctly pushstate.
40+
count: u32 = 0,
4141

42-
pub fn get_appCodeName(_: *Navigator) []const u8 {
43-
return "Mozilla";
44-
}
45-
pub fn get_appName(_: *Navigator) []const u8 {
46-
return "Netscape";
47-
}
48-
pub fn get_appVersion(self: *Navigator) []const u8 {
49-
return self.version;
50-
}
51-
pub fn get_platform(self: *Navigator) []const u8 {
52-
return self.platform;
53-
}
54-
pub fn get_product(_: *Navigator) []const u8 {
55-
return "Gecko";
56-
}
57-
pub fn get_productSub(_: *Navigator) []const u8 {
58-
return "20030107";
42+
pub fn get_length(self: *History) u32 {
43+
// TODO return the real history length value.
44+
return self.count;
5945
}
60-
pub fn get_vendor(self: *Navigator) []const u8 {
61-
return self.vendor;
62-
}
63-
pub fn get_vendorSub(_: *Navigator) []const u8 {
64-
return "";
46+
47+
pub fn get_scrollRestoration(self: *History) []const u8 {
48+
return switch (self.scrollRestoration) {
49+
.auto => "auto",
50+
.manual => "manual",
51+
};
6552
}
66-
pub fn get_language(self: *Navigator) []const u8 {
67-
return self.language;
53+
54+
pub fn set_scrollRestoration(self: *History, mode: []const u8) void {
55+
if (std.mem.eql(u8, "manual", mode)) self.scrollRestoration = .manual;
56+
if (std.mem.eql(u8, "auto", mode)) self.scrollRestoration = .auto;
6857
}
69-
// TODO wait for arrays.
70-
//pub fn get_languages(self: *Navigator) [][]const u8 {
71-
// return .{self.language};
72-
//}
73-
pub fn get_online(_: *Navigator) bool {
74-
return true;
58+
59+
pub fn get_state(self: *History) std.json.Value {
60+
return self.state;
7561
}
76-
pub fn _registerProtocolHandler(_: *Navigator, scheme: []const u8, url: []const u8) void {
77-
_ = scheme;
62+
63+
// TODO implement the function
64+
// data must handle any argument. We could expect a std.json.Value but
65+
// https://github.com/lightpanda-io/zig-js-runtime/issues/267 is missing.
66+
pub fn _pushState(self: *History, data: []const u8, _: ?[]const u8, url: ?[]const u8) void {
67+
self.count += 1;
7868
_ = url;
69+
_ = data;
7970
}
80-
pub fn _unregisterProtocolHandler(_: *Navigator, scheme: []const u8, url: []const u8) void {
81-
_ = scheme;
71+
72+
// TODO implement the function
73+
// data must handle any argument. We could expect a std.json.Value but
74+
// https://github.com/lightpanda-io/zig-js-runtime/issues/267 is missing.
75+
pub fn _replaceState(self: *History, data: []const u8, _: ?[]const u8, url: ?[]const u8) void {
76+
_ = self;
8277
_ = url;
78+
_ = data;
8379
}
8480

85-
pub fn get_cookieEnabled(_: *Navigator) bool {
86-
return true;
81+
// TODO implement the function
82+
pub fn _go(self: *History, delta: ?i32) void {
83+
_ = self;
84+
_ = delta;
85+
}
86+
87+
// TODO implement the function
88+
pub fn _back(self: *History) void {
89+
_ = self;
90+
}
91+
92+
// TODO implement the function
93+
pub fn _forward(self: *History) void {
94+
_ = self;
8795
}
8896
};
8997

@@ -94,11 +102,27 @@ pub fn testExecFn(
94102
_: std.mem.Allocator,
95103
js_env: *jsruntime.Env,
96104
) anyerror!void {
97-
var navigator = [_]Case{
98-
.{ .src = "navigator.userAgent", .ex = "Lightpanda/1.0" },
99-
.{ .src = "navigator.appVersion", .ex = "1.0" },
100-
.{ .src = "navigator.language", .ex = "en-US" },
105+
var history = [_]Case{
106+
.{ .src = "history.scrollRestoration", .ex = "auto" },
107+
.{ .src = "history.scrollRestoration = 'manual'", .ex = "manual" },
108+
.{ .src = "history.scrollRestoration = 'foo'", .ex = "foo" },
109+
.{ .src = "history.scrollRestoration", .ex = "manual" },
110+
.{ .src = "history.scrollRestoration = 'auto'", .ex = "auto" },
111+
.{ .src = "history.scrollRestoration", .ex = "auto" },
112+
113+
.{ .src = "history.state", .ex = "null" },
114+
115+
.{ .src = "history.pushState({}, null, '')", .ex = "undefined" },
116+
117+
.{ .src = "history.replaceState({}, null, '')", .ex = "undefined" },
118+
119+
.{ .src = "history.go()", .ex = "undefined" },
120+
.{ .src = "history.go(1)", .ex = "undefined" },
121+
.{ .src = "history.go(-1)", .ex = "undefined" },
122+
123+
.{ .src = "history.forward()", .ex = "undefined" },
124+
125+
.{ .src = "history.back()", .ex = "undefined" },
101126
};
102-
try checkCases(js_env, &navigator);
127+
try checkCases(js_env, &history);
103128
}
104-

src/html/html.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const HTMLDocument = @import("document.zig").HTMLDocument;
2222
const HTMLElem = @import("elements.zig");
2323
const Window = @import("window.zig").Window;
2424
const Navigator = @import("navigator.zig").Navigator;
25+
const History = @import("history.zig").History;
2526

2627
pub const Interfaces = generate.Tuple(.{
2728
HTMLDocument,
@@ -30,4 +31,5 @@ pub const Interfaces = generate.Tuple(.{
3031
HTMLElem.Interfaces,
3132
Window,
3233
Navigator,
34+
History,
3335
});

src/html/window.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const Loop = jsruntime.Loop;
2626

2727
const EventTarget = @import("../dom/event_target.zig").EventTarget;
2828
const Navigator = @import("navigator.zig").Navigator;
29+
const History = @import("history.zig").History;
2930

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

@@ -41,6 +42,7 @@ pub const Window = struct {
4142

4243
document: ?*parser.DocumentHTML = null,
4344
target: []const u8,
45+
history: History = .{},
4446

4547
storageShelf: ?*storage.Shelf = null,
4648

@@ -86,6 +88,10 @@ pub const Window = struct {
8688
return self.document;
8789
}
8890

91+
pub fn get_history(self: *Window) *History {
92+
return &self.history;
93+
}
94+
8995
pub fn get_name(self: *Window) []const u8 {
9096
return self.target;
9197
}

src/run_tests.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ fn testsAllExecFn(
138138
MutationObserverTestExecFn,
139139
@import("polyfill/fetch.zig").testExecFn,
140140
@import("html/navigator.zig").testExecFn,
141+
@import("html/history.zig").testExecFn,
141142
};
142143

143144
inline for (testFns) |testFn| {

src/storage/storage.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub const Bottle = struct {
173173
// > context of another document.
174174
// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
175175
//
176-
// So for now, we won't impement the feature.
176+
// So for now, we won't implement the feature.
177177
}
178178

179179
pub fn _removeItem(self: *Bottle, k: []const u8) !void {

0 commit comments

Comments
 (0)