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
42 changes: 21 additions & 21 deletions src/browser/console/console.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const std = @import("std");
const builtin = @import("builtin");

const JsObject = @import("../env.zig").Env.JsObject;
const SessionState = @import("../env.zig").SessionState;
const Page = @import("../page.zig").Page;

const log = if (builtin.is_test) &test_capture else @import("../../log.zig");

Expand All @@ -29,49 +29,49 @@ pub const Console = struct {
timers: std.StringHashMapUnmanaged(u32) = .{},
counts: std.StringHashMapUnmanaged(u32) = .{},

pub fn _log(_: *const Console, values: []JsObject, state: *SessionState) !void {
pub fn _log(_: *const Console, values: []JsObject, page: *Page) !void {
if (values.len == 0) {
return;
}
log.info(.console, "info", .{ .args = try serializeValues(values, state) });
log.info(.console, "info", .{ .args = try serializeValues(values, page) });
}

pub fn _info(console: *const Console, values: []JsObject, state: *SessionState) !void {
return console._log(values, state);
pub fn _info(console: *const Console, values: []JsObject, page: *Page) !void {
return console._log(values, page);
}

pub fn _debug(_: *const Console, values: []JsObject, state: *SessionState) !void {
pub fn _debug(_: *const Console, values: []JsObject, page: *Page) !void {
if (values.len == 0) {
return;
}
log.debug(.console, "debug", .{ .args = try serializeValues(values, state) });
log.debug(.console, "debug", .{ .args = try serializeValues(values, page) });
}

pub fn _warn(_: *const Console, values: []JsObject, state: *SessionState) !void {
pub fn _warn(_: *const Console, values: []JsObject, page: *Page) !void {
if (values.len == 0) {
return;
}
log.warn(.console, "warn", .{ .args = try serializeValues(values, state) });
log.warn(.console, "warn", .{ .args = try serializeValues(values, page) });
}

pub fn _error(_: *const Console, values: []JsObject, state: *SessionState) !void {
pub fn _error(_: *const Console, values: []JsObject, page: *Page) !void {
if (values.len == 0) {
return;
}
log.info(.console, "error", .{ .args = try serializeValues(values, state) });
log.info(.console, "error", .{ .args = try serializeValues(values, page) });
}

pub fn _clear(_: *const Console) void {}

pub fn _count(self: *Console, label_: ?[]const u8, state: *SessionState) !void {
pub fn _count(self: *Console, label_: ?[]const u8, page: *Page) !void {
const label = label_ orelse "default";
const gop = try self.counts.getOrPut(state.arena, label);
const gop = try self.counts.getOrPut(page.arena, label);

var current: u32 = 0;
if (gop.found_existing) {
current = gop.value_ptr.*;
} else {
gop.key_ptr.* = try state.arena.dupe(u8, label);
gop.key_ptr.* = try page.arena.dupe(u8, label);
}

const count = current + 1;
Expand All @@ -89,15 +89,15 @@ pub const Console = struct {
log.info(.console, "count reset", .{ .label = label, .count = kv.value });
}

pub fn _time(self: *Console, label_: ?[]const u8, state: *SessionState) !void {
pub fn _time(self: *Console, label_: ?[]const u8, page: *Page) !void {
const label = label_ orelse "default";
const gop = try self.timers.getOrPut(state.arena, label);
const gop = try self.timers.getOrPut(page.arena, label);

if (gop.found_existing) {
log.info(.console, "duplicate timer", .{ .label = label });
return;
}
gop.key_ptr.* = try state.arena.dupe(u8, label);
gop.key_ptr.* = try page.arena.dupe(u8, label);
gop.value_ptr.* = timestamp();
}

Expand All @@ -122,19 +122,19 @@ pub const Console = struct {
log.warn(.console, "timer stop", .{ .label = label, .elapsed = elapsed - kv.value });
}

pub fn _assert(_: *Console, assertion: JsObject, values: []JsObject, state: *SessionState) !void {
pub fn _assert(_: *Console, assertion: JsObject, values: []JsObject, page: *Page) !void {
if (assertion.isTruthy()) {
return;
}
var serialized_values: []const u8 = "";
if (values.len > 0) {
serialized_values = try serializeValues(values, state);
serialized_values = try serializeValues(values, page);
}
log.info(.console, "assertion failed", .{ .values = serialized_values });
}

fn serializeValues(values: []JsObject, state: *SessionState) ![]const u8 {
const arena = state.call_arena;
fn serializeValues(values: []JsObject, page: *Page) ![]const u8 {
const arena = page.call_arena;
var arr: std.ArrayListUnmanaged(u8) = .{};
try arr.appendSlice(arena, try values[0].toString());
for (values[1..]) |value| {
Expand Down
34 changes: 17 additions & 17 deletions src/browser/cssom/css_style_declaration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const std = @import("std");

const CSSParser = @import("./css_parser.zig").CSSParser;
const CSSValueAnalyzer = @import("./css_value_analyzer.zig").CSSValueAnalyzer;
const SessionState = @import("../env.zig").SessionState;
const Page = @import("../page.zig").Page;

pub const Interfaces = .{
CSSStyleDeclaration,
Expand All @@ -47,17 +47,17 @@ pub const CSSStyleDeclaration = struct {
return self._getPropertyValue("float");
}

pub fn set_cssFloat(self: *CSSStyleDeclaration, value: ?[]const u8, state: *SessionState) !void {
pub fn set_cssFloat(self: *CSSStyleDeclaration, value: ?[]const u8, page: *Page) !void {
const final_value = value orelse "";
return self._setProperty("float", final_value, null, state);
return self._setProperty("float", final_value, null, page);
}

pub fn get_cssText(self: *const CSSStyleDeclaration, state: *SessionState) ![]const u8 {
pub fn get_cssText(self: *const CSSStyleDeclaration, page: *Page) ![]const u8 {
var buffer: std.ArrayListUnmanaged(u8) = .empty;
const writer = buffer.writer(state.call_arena);
const writer = buffer.writer(page.call_arena);
for (self.order.items) |name| {
const prop = self.store.get(name).?;
const escaped = try CSSValueAnalyzer.escapeCSSValue(state.call_arena, prop.value);
const escaped = try CSSValueAnalyzer.escapeCSSValue(page.call_arena, prop.value);
try writer.print("{s}: {s}", .{ name, escaped });
if (prop.priority) try writer.writeAll(" !important");
try writer.writeAll("; ");
Expand All @@ -66,18 +66,18 @@ pub const CSSStyleDeclaration = struct {
}

// TODO Propagate also upward to parent node
pub fn set_cssText(self: *CSSStyleDeclaration, text: []const u8, state: *SessionState) !void {
pub fn set_cssText(self: *CSSStyleDeclaration, text: []const u8, page: *Page) !void {
self.store.clearRetainingCapacity();
self.order.clearRetainingCapacity();

// call_arena is safe here, because _setProperty will dupe the name
// using the state's longer-living arena.
const declarations = try CSSParser.parseDeclarations(state.call_arena, text);
// using the page's longer-living arena.
const declarations = try CSSParser.parseDeclarations(page.call_arena, text);

for (declarations) |decl| {
if (!CSSValueAnalyzer.isValidPropertyName(decl.name)) continue;
const priority: ?[]const u8 = if (decl.is_important) "important" else null;
try self._setProperty(decl.name, decl.value, priority, state);
try self._setProperty(decl.name, decl.value, priority, page);
}
}

Expand Down Expand Up @@ -119,19 +119,19 @@ pub const CSSStyleDeclaration = struct {
break;
}
}
// safe to return, since it's in our state.arena
// safe to return, since it's in our page.arena
return prop.value.value;
}

pub fn _setProperty(self: *CSSStyleDeclaration, name: []const u8, value: []const u8, priority: ?[]const u8, state: *SessionState) !void {
const owned_value = try state.arena.dupe(u8, value);
pub fn _setProperty(self: *CSSStyleDeclaration, name: []const u8, value: []const u8, priority: ?[]const u8, page: *Page) !void {
const owned_value = try page.arena.dupe(u8, value);
const is_important = priority != null and std.ascii.eqlIgnoreCase(priority.?, "important");

const gop = try self.store.getOrPut(state.arena, name);
const gop = try self.store.getOrPut(page.arena, name);
if (!gop.found_existing) {
const owned_name = try state.arena.dupe(u8, name);
const owned_name = try page.arena.dupe(u8, name);
gop.key_ptr.* = owned_name;
try self.order.append(state.arena, owned_name);
try self.order.append(page.arena, owned_name);
}

gop.value_ptr.* = .{ .value = owned_value, .priority = is_important };
Expand Down Expand Up @@ -177,7 +177,7 @@ test "CSSOM.CSSStyleDeclaration" {
.{ "style.setProperty('color', 'green')", "undefined" },
.{ "style.getPropertyValue('color')", "green" },
.{ "style.length", "4" },
.{ "style.color", "green"},
.{ "style.color", "green" },

.{ "style.setProperty('padding', '10px', 'important')", "undefined" },
.{ "style.getPropertyValue('padding')", "10px" },
Expand Down
6 changes: 3 additions & 3 deletions src/browser/dom/comment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ const parser = @import("../netsurf.zig");

const CharacterData = @import("character_data.zig").CharacterData;

const SessionState = @import("../env.zig").SessionState;
const Page = @import("../page.zig").Page;

// https://dom.spec.whatwg.org/#interface-comment
pub const Comment = struct {
pub const Self = parser.Comment;
pub const prototype = *CharacterData;
pub const subtype = .node;

pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Comment {
pub fn constructor(data: ?[]const u8, page: *const Page) !*parser.Comment {
return parser.documentCreateComment(
parser.documentHTMLToDocument(state.window.document),
parser.documentHTMLToDocument(page.window.document),
data orelse "",
);
}
Expand Down
31 changes: 15 additions & 16 deletions src/browser/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const std = @import("std");

const parser = @import("../netsurf.zig");
const SessionState = @import("../env.zig").SessionState;
const Page = @import("../page.zig").Page;

const Node = @import("node.zig").Node;
const NodeList = @import("nodelist.zig").NodeList;
Expand All @@ -42,14 +42,14 @@ pub const Document = struct {
pub const prototype = *Node;
pub const subtype = .node;

pub fn constructor(state: *const SessionState) !*parser.DocumentHTML {
pub fn constructor(page: *const Page) !*parser.DocumentHTML {
const doc = try parser.documentCreateDocument(
try parser.documentHTMLGetTitle(state.window.document),
try parser.documentHTMLGetTitle(page.window.document),
);

// we have to work w/ document instead of html document.
const ddoc = parser.documentHTMLToDocument(doc);
const ccur = parser.documentHTMLToDocument(state.window.document);
const ccur = parser.documentHTMLToDocument(page.window.document);
try parser.documentSetDocumentURI(ddoc, try parser.documentGetDocumentURI(ccur));
try parser.documentSetInputEncoding(ddoc, try parser.documentGetInputEncoding(ccur));

Expand Down Expand Up @@ -141,18 +141,17 @@ pub const Document = struct {
pub fn _getElementsByTagName(
self: *parser.Document,
tag_name: []const u8,
state: *SessionState,
page: *Page,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(state.arena, parser.documentToNode(self), tag_name, true);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentToNode(self), tag_name, true);
}

pub fn _getElementsByClassName(
self: *parser.Document,
classNames: []const u8,
state: *SessionState,
page: *Page,
) !collection.HTMLCollection {
const allocator = state.arena;
return try collection.HTMLCollectionByClassName(allocator, parser.documentToNode(self), classNames, true);
return try collection.HTMLCollectionByClassName(page.arena, parser.documentToNode(self), classNames, true);
}

pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment {
Expand Down Expand Up @@ -214,20 +213,18 @@ pub const Document = struct {
return 1;
}

pub fn _querySelector(self: *parser.Document, selector: []const u8, state: *SessionState) !?ElementUnion {
pub fn _querySelector(self: *parser.Document, selector: []const u8, page: *Page) !?ElementUnion {
if (selector.len == 0) return null;

const allocator = state.arena;
const n = try css.querySelector(allocator, parser.documentToNode(self), selector);
const n = try css.querySelector(page.arena, parser.documentToNode(self), selector);

if (n == null) return null;

return try Element.toInterface(parser.nodeToElement(n.?));
}

pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, state: *SessionState) !NodeList {
const allocator = state.arena;
return css.querySelectorAll(allocator, parser.documentToNode(self), selector);
pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, page: *Page) !NodeList {
return css.querySelectorAll(page.arena, parser.documentToNode(self), selector);
}

pub fn _prepend(self: *parser.Document, nodes: []const Node.NodeOrText) !void {
Expand All @@ -249,7 +246,9 @@ pub const Document = struct {

const testing = @import("../../testing.zig");
test "Browser.DOM.Document" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
var runner = try testing.jsRunner(testing.tracking_allocator, .{
.url = "about:blank",
});
defer runner.deinit();

try runner.testCases(&.{
Expand Down
6 changes: 3 additions & 3 deletions src/browser/dom/document_fragment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

const parser = @import("../netsurf.zig");
const SessionState = @import("../env.zig").SessionState;
const Page = @import("../page.zig").Page;

const Node = @import("node.zig").Node;

Expand All @@ -27,9 +27,9 @@ pub const DocumentFragment = struct {
pub const prototype = *Node;
pub const subtype = .node;

pub fn constructor(state: *const SessionState) !*parser.DocumentFragment {
pub fn constructor(page: *const Page) !*parser.DocumentFragment {
return parser.documentCreateDocumentFragment(
parser.documentHTMLToDocument(state.window.document),
parser.documentHTMLToDocument(page.window.document),
);
}

Expand Down
Loading