diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 1e74a8a9..d8178cbb 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -46,12 +46,15 @@ const polyfill = @import("../polyfill/polyfill.zig"); const log = std.log.scoped(.browser); +const user_agent = "Lightpanda.io/1.0"; + // Browser is an instance of the browser. // You can create multiple browser instances. // A browser contains only one session. // TODO allow multiple sessions per browser. pub const Browser = struct { session: Session = undefined, + agent: []const u8 = user_agent, const uri = "about:blank"; @@ -111,7 +114,7 @@ pub const Session = struct { .uri = uri, .alloc = alloc, .arena = std.heap.ArenaAllocator.init(alloc), - .window = Window.create(null), + .window = Window.create(null, .{ .agent = user_agent }), .loader = Loader.init(alloc), .storageShed = storage.Shed.init(alloc), .httpClient = undefined, diff --git a/src/html/html.zig b/src/html/html.zig index 3161e694..2bb439d1 100644 --- a/src/html/html.zig +++ b/src/html/html.zig @@ -21,6 +21,7 @@ const generate = @import("../generate.zig"); const HTMLDocument = @import("document.zig").HTMLDocument; const HTMLElem = @import("elements.zig"); const Window = @import("window.zig").Window; +const Navigator = @import("navigator.zig").Navigator; pub const Interfaces = generate.Tuple(.{ HTMLDocument, @@ -28,4 +29,5 @@ pub const Interfaces = generate.Tuple(.{ HTMLElem.HTMLMediaElement, HTMLElem.Interfaces, Window, + Navigator, }); diff --git a/src/html/navigator.zig b/src/html/navigator.zig new file mode 100644 index 00000000..15253593 --- /dev/null +++ b/src/html/navigator.zig @@ -0,0 +1,56 @@ +// 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 parser = @import("netsurf"); +const jsruntime = @import("jsruntime"); +const Callback = jsruntime.Callback; +const CallbackArg = jsruntime.CallbackArg; +const Loop = jsruntime.Loop; + +const Case = jsruntime.test_utils.Case; +const checkCases = jsruntime.test_utils.checkCases; + +const EventTarget = @import("../dom/event_target.zig").EventTarget; + +const storage = @import("../storage/storage.zig"); + +// https://html.spec.whatwg.org/multipage/system-state.html#navigator +pub const Navigator = struct { + pub const mem_guarantied = true; + + agent: []const u8 = "", + + pub fn get_userAgent(self: *Navigator) []const u8 { + return self.agent; + } +}; + +// Tests +// ----- + +pub fn testExecFn( + _: std.mem.Allocator, + js_env: *jsruntime.Env, +) anyerror!void { + var navigator = [_]Case{ + .{ .src = "navigator.userAgent", .ex = "" }, + }; + try checkCases(js_env, &navigator); +} diff --git a/src/html/window.zig b/src/html/window.zig index b81c91ba..414b660d 100644 --- a/src/html/window.zig +++ b/src/html/window.zig @@ -25,6 +25,7 @@ const CallbackArg = jsruntime.CallbackArg; const Loop = jsruntime.Loop; const EventTarget = @import("../dom/event_target.zig").EventTarget; +const Navigator = @import("navigator.zig").Navigator; const storage = @import("../storage/storage.zig"); @@ -48,9 +49,12 @@ pub const Window = struct { timeoutid: u32 = 0, timeoutids: [512]u64 = undefined, - pub fn create(target: ?[]const u8) Window { + navigator: Navigator, + + pub fn create(target: ?[]const u8, navigator: ?Navigator) Window { return Window{ .target = target orelse "", + .navigator = navigator orelse .{}, }; } @@ -66,6 +70,10 @@ pub const Window = struct { return self; } + pub fn get_navigator(self: *Window) *Navigator { + return &self.navigator; + } + pub fn get_self(self: *Window) *Window { return self; } diff --git a/src/run_tests.zig b/src/run_tests.zig index b506e5aa..077e56e2 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -96,7 +96,7 @@ fn testExecFn( }); // alias global as self and window - var window = Window.create(null); + var window = Window.create(null, null); window.replaceDocument(doc); window.setStorageShelf(&storageShelf); @@ -137,6 +137,7 @@ fn testsAllExecFn( HTMLElementTestExecFn, MutationObserverTestExecFn, @import("polyfill/fetch.zig").testExecFn, + @import("html/navigator.zig").testExecFn, }; inline for (testFns) |testFn| { @@ -359,7 +360,7 @@ test "bug document html parsing #4" { } test "Window is a libdom event target" { - var window = Window.create(null); + var window = Window.create(null, null); const event = try parser.eventCreate(); try parser.eventInit(event, "foo", .{}); diff --git a/src/wpt/run.zig b/src/wpt/run.zig index 07c3f740..208078b9 100644 --- a/src/wpt/run.zig +++ b/src/wpt/run.zig @@ -90,7 +90,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const } // setup global env vars. - var window = Window.create(null); + var window = Window.create(null, null); window.replaceDocument(html_doc); window.setStorageShelf(&storageShelf); try js_env.bindGlobal(&window);