Skip to content

Commit edf125b

Browse files
authored
Merge pull request #705 from lightpanda-io/page_as_state
Replace SessionState directly with the Page.
2 parents 1d0876a + 676e6ec commit edf125b

33 files changed

+541
-582
lines changed

src/browser/console/console.zig

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const std = @import("std");
2020
const builtin = @import("builtin");
2121

2222
const JsObject = @import("../env.zig").Env.JsObject;
23-
const SessionState = @import("../env.zig").SessionState;
23+
const Page = @import("../page.zig").Page;
2424

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

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

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

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

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

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

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

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

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

7070
var current: u32 = 0;
7171
if (gop.found_existing) {
7272
current = gop.value_ptr.*;
7373
} else {
74-
gop.key_ptr.* = try state.arena.dupe(u8, label);
74+
gop.key_ptr.* = try page.arena.dupe(u8, label);
7575
}
7676

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

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

9696
if (gop.found_existing) {
9797
log.info(.console, "duplicate timer", .{ .label = label });
9898
return;
9999
}
100-
gop.key_ptr.* = try state.arena.dupe(u8, label);
100+
gop.key_ptr.* = try page.arena.dupe(u8, label);
101101
gop.value_ptr.* = timestamp();
102102
}
103103

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

125-
pub fn _assert(_: *Console, assertion: JsObject, values: []JsObject, state: *SessionState) !void {
125+
pub fn _assert(_: *Console, assertion: JsObject, values: []JsObject, page: *Page) !void {
126126
if (assertion.isTruthy()) {
127127
return;
128128
}
129129
var serialized_values: []const u8 = "";
130130
if (values.len > 0) {
131-
serialized_values = try serializeValues(values, state);
131+
serialized_values = try serializeValues(values, page);
132132
}
133133
log.info(.console, "assertion failed", .{ .values = serialized_values });
134134
}
135135

136-
fn serializeValues(values: []JsObject, state: *SessionState) ![]const u8 {
137-
const arena = state.call_arena;
136+
fn serializeValues(values: []JsObject, page: *Page) ![]const u8 {
137+
const arena = page.call_arena;
138138
var arr: std.ArrayListUnmanaged(u8) = .{};
139139
try arr.appendSlice(arena, try values[0].toString());
140140
for (values[1..]) |value| {

src/browser/cssom/css_style_declaration.zig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const std = @import("std");
2020

2121
const CSSParser = @import("./css_parser.zig").CSSParser;
2222
const CSSValueAnalyzer = @import("./css_value_analyzer.zig").CSSValueAnalyzer;
23-
const SessionState = @import("../env.zig").SessionState;
23+
const Page = @import("../page.zig").Page;
2424

2525
pub const Interfaces = .{
2626
CSSStyleDeclaration,
@@ -47,17 +47,17 @@ pub const CSSStyleDeclaration = struct {
4747
return self._getPropertyValue("float");
4848
}
4949

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

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

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

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

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

@@ -119,19 +119,19 @@ pub const CSSStyleDeclaration = struct {
119119
break;
120120
}
121121
}
122-
// safe to return, since it's in our state.arena
122+
// safe to return, since it's in our page.arena
123123
return prop.value.value;
124124
}
125125

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

130-
const gop = try self.store.getOrPut(state.arena, name);
130+
const gop = try self.store.getOrPut(page.arena, name);
131131
if (!gop.found_existing) {
132-
const owned_name = try state.arena.dupe(u8, name);
132+
const owned_name = try page.arena.dupe(u8, name);
133133
gop.key_ptr.* = owned_name;
134-
try self.order.append(state.arena, owned_name);
134+
try self.order.append(page.arena, owned_name);
135135
}
136136

137137
gop.value_ptr.* = .{ .value = owned_value, .priority = is_important };
@@ -177,7 +177,7 @@ test "CSSOM.CSSStyleDeclaration" {
177177
.{ "style.setProperty('color', 'green')", "undefined" },
178178
.{ "style.getPropertyValue('color')", "green" },
179179
.{ "style.length", "4" },
180-
.{ "style.color", "green"},
180+
.{ "style.color", "green" },
181181

182182
.{ "style.setProperty('padding', '10px', 'important')", "undefined" },
183183
.{ "style.getPropertyValue('padding')", "10px" },

src/browser/dom/comment.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ const parser = @import("../netsurf.zig");
2020

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

23-
const SessionState = @import("../env.zig").SessionState;
23+
const Page = @import("../page.zig").Page;
2424

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

31-
pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Comment {
31+
pub fn constructor(data: ?[]const u8, page: *const Page) !*parser.Comment {
3232
return parser.documentCreateComment(
33-
parser.documentHTMLToDocument(state.window.document),
33+
parser.documentHTMLToDocument(page.window.document),
3434
data orelse "",
3535
);
3636
}

src/browser/dom/document.zig

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
const std = @import("std");
2020

2121
const parser = @import("../netsurf.zig");
22-
const SessionState = @import("../env.zig").SessionState;
22+
const Page = @import("../page.zig").Page;
2323

2424
const Node = @import("node.zig").Node;
2525
const NodeList = @import("nodelist.zig").NodeList;
@@ -42,14 +42,14 @@ pub const Document = struct {
4242
pub const prototype = *Node;
4343
pub const subtype = .node;
4444

45-
pub fn constructor(state: *const SessionState) !*parser.DocumentHTML {
45+
pub fn constructor(page: *const Page) !*parser.DocumentHTML {
4646
const doc = try parser.documentCreateDocument(
47-
try parser.documentHTMLGetTitle(state.window.document),
47+
try parser.documentHTMLGetTitle(page.window.document),
4848
);
4949

5050
// we have to work w/ document instead of html document.
5151
const ddoc = parser.documentHTMLToDocument(doc);
52-
const ccur = parser.documentHTMLToDocument(state.window.document);
52+
const ccur = parser.documentHTMLToDocument(page.window.document);
5353
try parser.documentSetDocumentURI(ddoc, try parser.documentGetDocumentURI(ccur));
5454
try parser.documentSetInputEncoding(ddoc, try parser.documentGetInputEncoding(ccur));
5555

@@ -141,18 +141,17 @@ pub const Document = struct {
141141
pub fn _getElementsByTagName(
142142
self: *parser.Document,
143143
tag_name: []const u8,
144-
state: *SessionState,
144+
page: *Page,
145145
) !collection.HTMLCollection {
146-
return try collection.HTMLCollectionByTagName(state.arena, parser.documentToNode(self), tag_name, true);
146+
return try collection.HTMLCollectionByTagName(page.arena, parser.documentToNode(self), tag_name, true);
147147
}
148148

149149
pub fn _getElementsByClassName(
150150
self: *parser.Document,
151151
classNames: []const u8,
152-
state: *SessionState,
152+
page: *Page,
153153
) !collection.HTMLCollection {
154-
const allocator = state.arena;
155-
return try collection.HTMLCollectionByClassName(allocator, parser.documentToNode(self), classNames, true);
154+
return try collection.HTMLCollectionByClassName(page.arena, parser.documentToNode(self), classNames, true);
156155
}
157156

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

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

220-
const allocator = state.arena;
221-
const n = try css.querySelector(allocator, parser.documentToNode(self), selector);
219+
const n = try css.querySelector(page.arena, parser.documentToNode(self), selector);
222220

223221
if (n == null) return null;
224222

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

228-
pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, state: *SessionState) !NodeList {
229-
const allocator = state.arena;
230-
return css.querySelectorAll(allocator, parser.documentToNode(self), selector);
226+
pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, page: *Page) !NodeList {
227+
return css.querySelectorAll(page.arena, parser.documentToNode(self), selector);
231228
}
232229

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

250247
const testing = @import("../../testing.zig");
251248
test "Browser.DOM.Document" {
252-
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
249+
var runner = try testing.jsRunner(testing.tracking_allocator, .{
250+
.url = "about:blank",
251+
});
253252
defer runner.deinit();
254253

255254
try runner.testCases(&.{

src/browser/dom/document_fragment.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const parser = @import("../netsurf.zig");
20-
const SessionState = @import("../env.zig").SessionState;
20+
const Page = @import("../page.zig").Page;
2121

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

@@ -27,9 +27,9 @@ pub const DocumentFragment = struct {
2727
pub const prototype = *Node;
2828
pub const subtype = .node;
2929

30-
pub fn constructor(state: *const SessionState) !*parser.DocumentFragment {
30+
pub fn constructor(page: *const Page) !*parser.DocumentFragment {
3131
return parser.documentCreateDocumentFragment(
32-
parser.documentHTMLToDocument(state.window.document),
32+
parser.documentHTMLToDocument(page.window.document),
3333
);
3434
}
3535

0 commit comments

Comments
 (0)