Skip to content

Commit 967ab18

Browse files
committed
default:blank as default document
1 parent 0929bd2 commit 967ab18

File tree

12 files changed

+34
-36
lines changed

12 files changed

+34
-36
lines changed

src/browser/dom/comment.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub const Comment = struct {
3030

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

src/browser/dom/document.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ pub const Document = struct {
4141

4242
pub fn constructor(state: *const SessionState) !*parser.DocumentHTML {
4343
const doc = try parser.documentCreateDocument(
44-
try parser.documentHTMLGetTitle(state.window.document.?),
44+
try parser.documentHTMLGetTitle(state.window.document),
4545
);
4646

4747
// we have to work w/ document instead of html document.
4848
const ddoc = parser.documentHTMLToDocument(doc);
49-
const ccur = parser.documentHTMLToDocument(state.window.document.?);
49+
const ccur = parser.documentHTMLToDocument(state.window.document);
5050
try parser.documentSetDocumentURI(ddoc, try parser.documentGetDocumentURI(ccur));
5151
try parser.documentSetInputEncoding(ddoc, try parser.documentGetInputEncoding(ccur));
5252

src/browser/dom/document_fragment.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub const DocumentFragment = struct {
2929

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

src/browser/dom/element.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub const Element = struct {
370370
pub fn _getBoundingClientRect(self: *parser.Element, state: *SessionState) !DOMRect {
371371
// Since we are lazy rendering we need to do this check. We could store the renderer in a viewport such that it could cache these, but it would require tracking changes.
372372
const root = try parser.nodeGetRootNode(parser.elementToNode(self));
373-
if (root != parser.documentToNode(parser.documentHTMLToDocument(state.window.document.?))) {
373+
if (root != parser.documentToNode(parser.documentHTMLToDocument(state.window.document))) {
374374
return DOMRect{ .x = 0, .y = 0, .width = 0, .height = 0 };
375375
}
376376
return state.renderer.getRect(self);
@@ -381,7 +381,7 @@ pub const Element = struct {
381381
// Returns an empty array if the element is eventually detached from the main window
382382
pub fn _getClientRects(self: *parser.Element, state: *SessionState) ![]DOMRect {
383383
const root = try parser.nodeGetRootNode(parser.elementToNode(self));
384-
if (root != parser.documentToNode(parser.documentHTMLToDocument(state.window.document.?))) {
384+
if (root != parser.documentToNode(parser.documentHTMLToDocument(state.window.document))) {
385385
return &.{};
386386
}
387387
const heap_ptr = try state.call_arena.create(DOMRect);

src/browser/dom/intersection_observer.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub const IntersectionObserver = struct {
5050
// new IntersectionObserver(callback, options) [not supported yet]
5151
pub fn constructor(callback: Env.Callback, options_: ?IntersectionObserverOptions, state: *SessionState) !IntersectionObserver {
5252
var options = IntersectionObserverOptions{
53-
.root = parser.documentToNode(parser.documentHTMLToDocument(state.window.document.?)),
53+
.root = parser.documentToNode(parser.documentHTMLToDocument(state.window.document)),
5454
.rootMargin = "0px 0px 0px 0px",
5555
.threshold = &.{0.0},
5656
};
@@ -142,7 +142,7 @@ pub const IntersectionObserverEntry = struct {
142142
// Returns a DOMRectReadOnly for the intersection observer's root.
143143
pub fn get_rootBounds(self: *const IntersectionObserverEntry) !Element.DOMRect {
144144
const root = self.options.root.?;
145-
if (@intFromPtr(root) == @intFromPtr(self.state.window.document.?)) {
145+
if (@intFromPtr(root) == @intFromPtr(self.state.window.document)) {
146146
return self.state.renderer.boundingRect();
147147
}
148148

src/browser/dom/processing_instruction.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub const ProcessingInstruction = struct {
4141
// a simple workaround.
4242
pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool, state: *SessionState) !*parser.ProcessingInstruction {
4343
return try parser.documentCreateProcessingInstruction(
44-
@ptrCast(state.window.document.?),
44+
@ptrCast(state.window.document),
4545
try get_target(self),
4646
(try get_data(self)) orelse "",
4747
);

src/browser/dom/text.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub const Text = struct {
3434

3535
pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Text {
3636
return parser.documentCreateTextNode(
37-
parser.documentHTMLToDocument(state.window.document.?),
37+
parser.documentHTMLToDocument(state.window.document),
3838
data orelse "",
3939
);
4040
}

src/browser/html/document.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ pub const HTMLDocument = struct {
255255
// Thus we can add the HtmlHtmlElement and it's child HTMLBodyElement to the returned list.
256256
// TBD Should we instead return every parent that is an element? Note that a child does not physically need to be overlapping the parent.
257257
// Should we do a render pass on demand?
258-
const doc_elem = try parser.documentGetDocumentElement(parser.documentHTMLToDocument(state.window.document.?)) orelse {
258+
const doc_elem = try parser.documentGetDocumentElement(parser.documentHTMLToDocument(state.window.document)) orelse {
259259
return list.items;
260260
};
261-
if (try parser.documentHTMLBody(state.window.document.?)) |body| {
261+
if (try parser.documentHTMLBody(state.window.document)) |body| {
262262
list.appendAssumeCapacity(try Element.toInterface(parser.bodyToElement(body)));
263263
}
264264
list.appendAssumeCapacity(try Element.toInterface(doc_elem));
@@ -383,12 +383,12 @@ test "Browser.HTML.Document" {
383383
.{ "document.readyState", "loading" },
384384
}, .{});
385385

386-
try HTMLDocument.documentIsLoaded(runner.window.document.?, &runner.state);
386+
try HTMLDocument.documentIsLoaded(runner.window.document, &runner.state);
387387
try runner.testCases(&.{
388388
.{ "document.readyState", "interactive" },
389389
}, .{});
390390

391-
try HTMLDocument.documentIsComplete(runner.window.document.?, &runner.state);
391+
try HTMLDocument.documentIsComplete(runner.window.document, &runner.state);
392392
try runner.testCases(&.{
393393
.{ "document.readyState", "complete" },
394394
}, .{});

src/browser/html/window.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub const Window = struct {
4444
// Extend libdom event target for pure zig struct.
4545
base: parser.EventTargetTBase = parser.EventTargetTBase{},
4646

47-
document: ?*parser.DocumentHTML = null,
47+
document: *parser.DocumentHTML,
4848
target: []const u8 = "",
4949
history: History = .{},
5050
location: Location = .{},
@@ -60,7 +60,13 @@ pub const Window = struct {
6060
performance: Performance,
6161

6262
pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window {
63+
var fbs = std.io.fixedBufferStream("");
64+
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8");
65+
const doc = parser.documentHTMLToDocument(html_doc);
66+
try parser.documentSetDocumentURI(doc, "about:blank");
67+
6368
return .{
69+
.document = html_doc,
6470
.target = target orelse "",
6571
.navigator = navigator orelse .{},
6672
.performance = .{ .time_origin = try std.time.Timer.start() },
@@ -69,9 +75,7 @@ pub const Window = struct {
6975

7076
pub fn replaceLocation(self: *Window, loc: Location) !void {
7177
self.location = loc;
72-
if (self.document) |doc| {
73-
try parser.documentHTMLSetLocation(Location, doc, &self.location);
74-
}
78+
try parser.documentHTMLSetLocation(Location, self.document, &self.location);
7579
}
7680

7781
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {

src/browser/page.zig

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ pub const Page = struct {
6060
// Serves are the root object of our JavaScript environment
6161
window: Window,
6262

63-
doc: ?*parser.Document,
64-
6563
// The URL of the page
6664
url: URL,
6765

@@ -87,7 +85,6 @@ pub const Page = struct {
8785
self.* = .{
8886
.window = try Window.create(null, null),
8987
.arena = arena,
90-
.doc = null,
9188
.raw_data = null,
9289
.url = URL.empty,
9390
.session = session,
@@ -121,15 +118,14 @@ pub const Page = struct {
121118

122119
// dump writes the page content into the given file.
123120
pub fn dump(self: *const Page, out: std.fs.File) !void {
124-
// if no HTML document pointer available, dump the data content only.
125-
if (self.doc == null) {
126-
// no data loaded, nothing to do.
127-
if (self.raw_data == null) return;
128-
return try out.writeAll(self.raw_data.?);
121+
if (self.raw_data) |raw_data| {
122+
// raw_data was set if the document was not HTML, dump the data content only.
123+
return try out.writeAll(raw_data);
129124
}
130125

131126
// if the page has a pointer to a document, dumps the HTML.
132-
try Dump.writeHTML(self.doc.?, out);
127+
const doc = parser.documentHTMLToDocument(self.window.document);
128+
try Dump.writeHTML(doc, out);
133129
}
134130

135131
pub fn fetchModuleSource(ctx: *anyopaque, specifier: []const u8) !?[]const u8 {
@@ -187,7 +183,7 @@ pub const Page = struct {
187183
try self.loadHTMLDoc(fbs.reader(), "utf-8");
188184
// We do not processHTMLDoc here as we know we don't have any scripts
189185
// This assumption may be false when CDP Page.addScriptToEvaluateOnNewDocument is implemented
190-
try HTMLDocument.documentIsComplete(self.window.document.?, &self.state);
186+
try HTMLDocument.documentIsComplete(self.window.document, &self.state);
191187
return;
192188
}
193189

@@ -229,6 +225,7 @@ pub const Page = struct {
229225
} orelse .unknown;
230226

231227
if (mime.isHTML()) {
228+
self.raw_data = null;
232229
try self.loadHTMLDoc(&response, mime.charset orelse "utf-8");
233230
try self.processHTMLDoc();
234231
} else {
@@ -256,9 +253,6 @@ pub const Page = struct {
256253
const html_doc = try parser.documentHTMLParse(reader, ccharset);
257254
const doc = parser.documentHTMLToDocument(html_doc);
258255

259-
// save a document's pointer in the page.
260-
self.doc = doc;
261-
262256
// inject the URL to the document including the fragment.
263257
try parser.documentSetDocumentURI(doc, self.url.raw);
264258

@@ -270,8 +264,8 @@ pub const Page = struct {
270264
}
271265

272266
fn processHTMLDoc(self: *Page) !void {
273-
const doc = self.doc.?;
274-
const html_doc = self.window.document.?;
267+
const html_doc = self.window.document;
268+
const doc = parser.documentHTMLToDocument(html_doc);
275269

276270
const document_element = (try parser.documentGetDocumentElement(doc)) orelse return error.DocumentElementError;
277271
try parser.eventTargetAddEventListener(

0 commit comments

Comments
 (0)