Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ const polyfill = @import("../polyfill/polyfill.zig");

const log = std.log.scoped(.browser);

pub const user_agent = "Lightpanda/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";

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/browser/loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const std = @import("std");
const Client = @import("../http/Client.zig");

const user_agent = "Lightpanda.io/1.0";
const user_agent = @import("browser.zig").user_agent;

pub const Loader = struct {
client: Client,
Expand Down
2 changes: 2 additions & 0 deletions src/html/html.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ 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,
HTMLElem.HTMLElement,
HTMLElem.HTMLMediaElement,
HTMLElem.Interfaces,
Window,
Navigator,
});
110 changes: 110 additions & 0 deletions src/html/navigator.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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 <https://www.gnu.org/licenses/>.

const std = @import("std");

const builtin = @import("builtin");
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 = "Lightpanda/1.0",
version: []const u8 = "1.0",
vendor: []const u8 = "",
platform: []const u8 = std.fmt.comptimePrint("{any} {any}", .{ builtin.os.tag, builtin.cpu.arch }),

language: []const u8 = "en-US",

pub fn get_userAgent(self: *Navigator) []const u8 {
return self.agent;
}
pub fn get_appCodeName(_: *Navigator) []const u8 {
return "Mozilla";
}
pub fn get_appName(_: *Navigator) []const u8 {
return "Netscape";
}
pub fn get_appVersion(self: *Navigator) []const u8 {
return self.version;
}
pub fn get_platform(self: *Navigator) []const u8 {
return self.platform;
}
pub fn get_product(_: *Navigator) []const u8 {
return "Gecko";
}
pub fn get_productSub(_: *Navigator) []const u8 {
return "20030107";
}
pub fn get_vendor(self: *Navigator) []const u8 {
return self.vendor;
}
pub fn get_vendorSub(_: *Navigator) []const u8 {
return "";
}
pub fn get_language(self: *Navigator) []const u8 {
return self.language;
}
// TODO wait for arrays.
//pub fn get_languages(self: *Navigator) [][]const u8 {
// return .{self.language};
//}
pub fn get_online(_: *Navigator) bool {
return true;
}
pub fn _registerProtocolHandler(_: *Navigator, scheme: []const u8, url: []const u8) void {
_ = scheme;
_ = url;
}
pub fn _unregisterProtocolHandler(_: *Navigator, scheme: []const u8, url: []const u8) void {
_ = scheme;
_ = url;
}

pub fn get_cookieEnabled(_: *Navigator) bool {
return true;
}
};

// Tests
// -----

pub fn testExecFn(
_: std.mem.Allocator,
js_env: *jsruntime.Env,
) anyerror!void {
var navigator = [_]Case{
.{ .src = "navigator.userAgent", .ex = "Lightpanda/1.0" },
.{ .src = "navigator.appVersion", .ex = "1.0" },
.{ .src = "navigator.language", .ex = "en-US" },
};
try checkCases(js_env, &navigator);
}
10 changes: 9 additions & 1 deletion src/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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 .{},
};
}

Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/run_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -137,6 +137,7 @@ fn testsAllExecFn(
HTMLElementTestExecFn,
MutationObserverTestExecFn,
@import("polyfill/fetch.zig").testExecFn,
@import("html/navigator.zig").testExecFn,
};

inline for (testFns) |testFn| {
Expand Down Expand Up @@ -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", .{});
Expand Down
2 changes: 1 addition & 1 deletion src/wpt/run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/wpt
Submodule wpt updated 277 files
Loading