1717// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
1919const std = @import ("std" );
20+
2021const log = @import ("../../log.zig" );
22+ const testing = @import ("../../testing.zig" );
2123const URL = @import ("../../url.zig" ).URL ;
22-
23- const js = @import ("../js/js.zig" );
24- const Page = @import ("../page.zig" ).Page ;
25-
2624const EventTarget = @import ("../dom/event_target.zig" ).EventTarget ;
2725const EventHandler = @import ("../events/event.zig" ).EventHandler ;
28-
26+ const js = @import ( "../js/js.zig" );
2927const parser = @import ("../netsurf.zig" );
28+ const Page = @import ("../page.zig" ).Page ;
3029
3130// https://developer.mozilla.org/en-US/docs/Web/API/Navigation
3231const Navigation = @This ();
@@ -38,12 +37,20 @@ pub const Interfaces = .{
3837 NavigationHistoryEntry ,
3938};
4039
41- pub const NavigationKind = union (enum ) {
42- initial ,
40+ pub const NavigationType = enum {
41+ pub const ENUM_JS_USE_TAG = true ;
42+
43+ push ,
44+ replace ,
45+ reload ,
46+ traverse ,
47+ };
48+
49+ pub const NavigationKind = union (NavigationType ) {
4350 push : ? []const u8 ,
4451 replace ,
45- traverse : usize ,
4652 reload ,
53+ traverse : usize ,
4754};
4855
4956pub const prototype = * EventTarget ;
@@ -103,18 +110,9 @@ const NavigationHistoryEntry = struct {
103110
104111// https://developer.mozilla.org/en-US/docs/Web/API/NavigationActivation
105112const NavigationActivation = struct {
106- const NavigationActivationType = enum {
107- pub const ENUM_JS_USE_TAG = true ;
108-
109- push ,
110- reload ,
111- replace ,
112- traverse ,
113- };
114-
115113 entry : NavigationHistoryEntry ,
116114 from : ? NavigationHistoryEntry = null ,
117- type : NavigationActivationType ,
115+ type : NavigationType ,
118116
119117 pub fn get_entry (self : * const NavigationActivation ) NavigationHistoryEntry {
120118 return self .entry ;
@@ -124,7 +122,7 @@ const NavigationActivation = struct {
124122 return self .from ;
125123 }
126124
127- pub fn get_navigationType (self : * const NavigationActivation ) NavigationActivationType {
125+ pub fn get_navigationType (self : * const NavigationActivation ) NavigationType {
128126 return self .type ;
129127 }
130128};
@@ -133,7 +131,7 @@ const NavigationActivation = struct {
133131const NavigationTransition = struct {
134132 finished : js.Promise ,
135133 from : NavigationHistoryEntry ,
136- navigation_type : NavigationActivation.NavigationActivationType ,
134+ navigation_type : NavigationType ,
137135};
138136
139137pub fn get_canGoBack (self : * const Navigation ) bool {
@@ -191,21 +189,22 @@ pub fn _forward(self: *Navigation, page: *Page) !NavigationReturn {
191189}
192190
193191// This is for after true navigation processing, where we need to ensure that our entries are up to date.
194- pub fn processNavigation (self : * Navigation , url : []const u8 , kind : NavigationKind , page : * Page ) ! void {
195- switch (kind ) {
196- .initial = > {
197- _ = try self .pushEntry (url , null , page );
198- },
199- .replace = > {
200- // When replacing, we just update the URL but the state is nullified.
201- const entry = self .currentEntry ();
202- entry .url = url ;
203- entry .state = null ;
204- },
205- .push = > | state | {
206- _ = try self .pushEntry (url , state , page );
207- },
208- .traverse , .reload = > {},
192+ pub fn processNavigation (self : * Navigation , url : []const u8 , kind : ? NavigationKind , page : * Page ) ! void {
193+ if (kind ) | k | {
194+ switch (k ) {
195+ .replace = > {
196+ // When replacing, we just update the URL but the state is nullified.
197+ const entry = self .currentEntry ();
198+ entry .url = url ;
199+ entry .state = null ;
200+ },
201+ .push = > | state | {
202+ _ = try self .pushEntry (url , state , page );
203+ },
204+ .traverse , .reload = > {},
205+ }
206+ } else {
207+ _ = try self .pushEntry (url , null , page );
209208 }
210209}
211210
@@ -300,7 +299,7 @@ pub fn navigate(
300299 .reload = > {
301300 try page .navigateFromWebAPI (url , .{ .reason = .navigation }, kind );
302301 },
303- else = > unreachable ,
302+ .replace = > {} ,
304303 }
305304
306305 return .{
@@ -312,7 +311,17 @@ pub fn navigate(
312311pub fn _navigate (self : * Navigation , _url : []const u8 , _opts : ? NavigateOptions , page : * Page ) ! NavigationReturn {
313312 const opts = _opts orelse NavigateOptions {};
314313 const json = if (opts .state ) | state | state .toJson (page .session .arena ) catch return error .DataClone else null ;
315- return try self .navigate (_url , .{ .push = json }, page );
314+
315+ switch (opts .history ) {
316+ .auto , .push = > {
317+ return try self .navigate (_url , .{ .push = json }, page );
318+ },
319+ .replace = > {
320+ const entry = self .currentEntry ();
321+ entry .state = json ;
322+ return try self .navigate (_url , .replace , page );
323+ },
324+ }
316325}
317326
318327pub const ReloadOptions = struct {
@@ -357,7 +366,44 @@ pub fn _updateCurrentEntry(self: *Navigation, options: UpdateCurrentEntryOptions
357366 self .currentEntry ().state = options .state .toJson (arena ) catch return error .DataClone ;
358367}
359368
360- const testing = @import ("../../testing.zig" );
369+ const Event = @import ("../events/event.zig" ).Event ;
370+
371+ pub const NavigationCurrentEntryChangeEvent = struct {
372+ pub const prototype = * Event ;
373+ pub const union_make_copy = true ;
374+
375+ pub const EventInit = struct {
376+ from : NavigationHistoryEntry ,
377+ navigation_type : ? NavigationType = null ,
378+ };
379+
380+ proto : parser.Event ,
381+ form : NavigationHistoryEntry ,
382+ navigation_type : ? NavigationType ,
383+
384+ pub fn constructor (event_type : []const u8 , opts : ? EventInit ) ! NavigationCurrentEntryChangeEvent {
385+ const event = try parser .eventCreate ();
386+ defer parser .eventDestroy (event );
387+ try parser .eventInit (event , event_type , .{});
388+ parser .eventSetInternalType (event , .navigation_current_entry_change );
389+
390+ const o = opts orelse EventInit {};
391+
392+ return .{
393+ .proto = event .* ,
394+ .state = o .state ,
395+ };
396+ }
397+
398+ pub fn get_from (self : * const NavigationCurrentEntryChangeEvent ) NavigationHistoryEntry {
399+ return self .from ;
400+ }
401+
402+ pub fn get_navigationType (self : * const NavigationCurrentEntryChangeEvent ) NavigationType {
403+ return self .navigation_type .? ;
404+ }
405+ };
406+
361407test "Browser: Navigation" {
362408 try testing .htmlRunner ("html/navigation.html" );
363409}
0 commit comments