|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <francis@lightpanda.io> |
| 4 | +// Pierre Tachoire <pierre@lightpanda.io> |
| 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 | +const log = @import("../../log.zig"); |
| 21 | + |
| 22 | +const Env = @import("../env.zig").Env; |
| 23 | +const Page = @import("../page.zig").Page; |
| 24 | + |
| 25 | +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-history-interface |
| 26 | +const History = @This(); |
| 27 | + |
| 28 | +const HistoryEntry = struct { |
| 29 | + url: []const u8, |
| 30 | + // This is serialized as JSON because |
| 31 | + // History must survive a JsContext. |
| 32 | + state: ?[]u8, |
| 33 | +}; |
| 34 | + |
| 35 | +const ScrollRestorationMode = enum { |
| 36 | + auto, |
| 37 | + manual, |
| 38 | + |
| 39 | + pub fn fromString(str: []const u8) ?ScrollRestorationMode { |
| 40 | + for (std.enums.values(ScrollRestorationMode)) |mode| { |
| 41 | + if (std.ascii.eqlIgnoreCase(str, @tagName(mode))) { |
| 42 | + return mode; |
| 43 | + } |
| 44 | + } else { |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn toString(self: ScrollRestorationMode) []const u8 { |
| 50 | + return @tagName(self); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +scroll_restoration: ScrollRestorationMode = .auto, |
| 55 | +stack: std.ArrayListUnmanaged(HistoryEntry) = .empty, |
| 56 | +current: ?usize = null, |
| 57 | + |
| 58 | +pub fn get_length(self: *History) u32 { |
| 59 | + return @intCast(self.stack.items.len); |
| 60 | +} |
| 61 | + |
| 62 | +pub fn get_scrollRestoration(self: *History) ScrollRestorationMode { |
| 63 | + return self.scroll_restoration; |
| 64 | +} |
| 65 | + |
| 66 | +pub fn set_scrollRestoration(self: *History, mode: []const u8) void { |
| 67 | + self.scroll_restoration = ScrollRestorationMode.fromString(mode) orelse self.scroll_restoration; |
| 68 | +} |
| 69 | + |
| 70 | +pub fn get_state(self: *History, page: *Page) !?Env.Value { |
| 71 | + if (self.current) |curr| { |
| 72 | + const entry = self.stack.items[curr]; |
| 73 | + if (entry.state) |state| { |
| 74 | + const value = try Env.Value.fromJson(page.main_context, state); |
| 75 | + return value; |
| 76 | + } else { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } else { |
| 80 | + return null; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub fn pushNavigation(self: *History, _url: []const u8, page: *Page) !void { |
| 85 | + const arena = page.session.arena; |
| 86 | + const url = try arena.dupe(u8, _url); |
| 87 | + |
| 88 | + const entry = HistoryEntry{ .state = null, .url = url }; |
| 89 | + try self.stack.append(arena, entry); |
| 90 | + self.current = self.stack.items.len - 1; |
| 91 | +} |
| 92 | + |
| 93 | +pub fn dispatchPopStateEvent(state: ?[]const u8, page: *Page) void { |
| 94 | + log.debug(.script_event, "dispatch popstate event", .{ |
| 95 | + .type = "popstate", |
| 96 | + .source = "history", |
| 97 | + }); |
| 98 | + History._dispatchPopStateEvent(state, page) catch |err| { |
| 99 | + log.err(.app, "dispatch popstate event error", .{ |
| 100 | + .err = err, |
| 101 | + .type = "popstate", |
| 102 | + .source = "history", |
| 103 | + }); |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +fn _dispatchPopStateEvent(state: ?[]const u8, page: *Page) !void { |
| 108 | + var evt = try PopStateEvent.constructor("popstate", .{ .state = state }); |
| 109 | + |
| 110 | + _ = try parser.eventTargetDispatchEvent( |
| 111 | + @as(*parser.EventTarget, @ptrCast(&page.window)), |
| 112 | + &evt.proto, |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +pub fn _pushState(self: *History, state: Env.JsObject, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void { |
| 117 | + const arena = page.session.arena; |
| 118 | + |
| 119 | + const json = try state.toJson(arena); |
| 120 | + const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw); |
| 121 | + const entry = HistoryEntry{ .state = json, .url = url }; |
| 122 | + try self.stack.append(arena, entry); |
| 123 | + self.current = self.stack.items.len - 1; |
| 124 | +} |
| 125 | + |
| 126 | +pub fn _replaceState(self: *History, state: Env.JsObject, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void { |
| 127 | + const arena = page.session.arena; |
| 128 | + |
| 129 | + if (self.current) |curr| { |
| 130 | + const entry = &self.stack.items[curr]; |
| 131 | + const json = try state.toJson(arena); |
| 132 | + const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw); |
| 133 | + entry.* = HistoryEntry{ .state = json, .url = url }; |
| 134 | + } else { |
| 135 | + try self._pushState(state, "", _url, page); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +pub fn go(self: *History, delta: i32, page: *Page) !void { |
| 140 | + // 0 behaves the same as no argument, both reloading the page. |
| 141 | + // If this is getting called, there SHOULD be an entry, atleast from pushNavigation. |
| 142 | + const current = self.current.?; |
| 143 | + |
| 144 | + const index_s: i64 = @intCast(@as(i64, @intCast(current)) + @as(i64, @intCast(delta))); |
| 145 | + if (index_s < 0 or index_s > self.stack.items.len - 1) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + const index = @as(usize, @intCast(index_s)); |
| 150 | + const entry = self.stack.items[index]; |
| 151 | + self.current = index; |
| 152 | + |
| 153 | + if (try page.isSameOrigin(entry.url)) { |
| 154 | + History.dispatchPopStateEvent(entry.state, page); |
| 155 | + } |
| 156 | + |
| 157 | + try page.navigateFromWebAPI(entry.url, .{ .reason = .history }); |
| 158 | +} |
| 159 | + |
| 160 | +pub fn _go(self: *History, _delta: ?i32, page: *Page) !void { |
| 161 | + try self.go(_delta orelse 0, page); |
| 162 | +} |
| 163 | + |
| 164 | +pub fn _back(self: *History, page: *Page) !void { |
| 165 | + try self.go(-1, page); |
| 166 | +} |
| 167 | + |
| 168 | +pub fn _forward(self: *History, page: *Page) !void { |
| 169 | + try self.go(1, page); |
| 170 | +} |
| 171 | + |
| 172 | +const parser = @import("../netsurf.zig"); |
| 173 | +const Event = @import("../events/event.zig").Event; |
| 174 | + |
| 175 | +pub const PopStateEvent = struct { |
| 176 | + pub const prototype = *Event; |
| 177 | + pub const union_make_copy = true; |
| 178 | + |
| 179 | + pub const EventInit = struct { |
| 180 | + state: ?[]const u8 = null, |
| 181 | + }; |
| 182 | + |
| 183 | + proto: parser.Event, |
| 184 | + state: ?[]const u8, |
| 185 | + |
| 186 | + pub fn constructor(event_type: []const u8, opts: ?EventInit) !PopStateEvent { |
| 187 | + const event = try parser.eventCreate(); |
| 188 | + defer parser.eventDestroy(event); |
| 189 | + try parser.eventInit(event, event_type, .{}); |
| 190 | + parser.eventSetInternalType(event, .pop_state); |
| 191 | + |
| 192 | + const o = opts orelse EventInit{}; |
| 193 | + |
| 194 | + return .{ |
| 195 | + .proto = event.*, |
| 196 | + .state = o.state, |
| 197 | + }; |
| 198 | + } |
| 199 | + |
| 200 | + // `hasUAVisualTransition` is not implemented. It isn't baseline so this is okay. |
| 201 | + |
| 202 | + pub fn get_state(self: *const PopStateEvent, page: *Page) !?Env.Value { |
| 203 | + if (self.state) |state| { |
| 204 | + const value = try Env.Value.fromJson(page.main_context, state); |
| 205 | + return value; |
| 206 | + } else { |
| 207 | + return null; |
| 208 | + } |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +const testing = @import("../../testing.zig"); |
| 213 | +test "Browser: HTML.History" { |
| 214 | + try testing.htmlRunner("html/history.html"); |
| 215 | +} |
0 commit comments