Skip to content

Commit 840aea9

Browse files
committed
dom: fix document creation process
1 parent bf52293 commit 840aea9

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

src/dom/document.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub const Document = struct {
5353
if (userctx.document) |cur| {
5454
title = try parser.documentHTMLGetTitle(cur);
5555
}
56-
const doc = try parser.domImplementationCreateHTMLDocument(title);
56+
const doc = try parser.documentCreateDocument(title);
5757

5858
if (userctx.document) |cur| {
5959
// we have to work w/ document instead of html document.

src/dom/implementation.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub fn testExecFn(
9696
var getImplementation = [_]Case{
9797
.{ .src = "let impl = document.implementation", .ex = "undefined" },
9898
.{ .src = "impl.createHTMLDocument();", .ex = "[object HTMLDocument]" },
99+
.{ .src = "impl.createHTMLDocument('foo');", .ex = "[object HTMLDocument]" },
99100
.{ .src = "impl.createDocument(null, 'foo');", .ex = "[object Document]" },
100101
.{ .src = "impl.createDocumentType('foo', 'bar', 'baz')", .ex = "[object DocumentType]" },
101102
.{ .src = "impl.hasFeature()", .ex = "true" },

src/dom/node.zig

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,30 @@ pub const Node = struct {
277277
return try Node.toInterface(res);
278278
}
279279

280+
// Check if the hierarchy node tree constraints are respected.
281+
// For now, it checks only if new nodes are not self.
282+
// TODO implements the others contraints.
283+
// see https://dom.spec.whatwg.org/#concept-node-tree
284+
pub fn hierarchy(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !bool {
285+
if (nodes == null) return true;
286+
if (nodes.?.slice.len == 0) return true;
287+
288+
for (nodes.?.slice) |node| if (self == node) return false;
289+
290+
return true;
291+
}
292+
280293
// TODO according with https://dom.spec.whatwg.org/#parentnode, the
281294
// function must accept either node or string.
282295
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
283296
pub fn prepend(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
284297
if (nodes == null) return;
285298
if (nodes.?.slice.len == 0) return;
286-
const first = try parser.nodeFirstChild(self);
287299

300+
// check hierarchy
301+
if (!try hierarchy(self, nodes)) return parser.DOMError.HierarchyRequest;
302+
303+
const first = try parser.nodeFirstChild(self);
288304
if (first == null) {
289305
for (nodes.?.slice) |node| {
290306
_ = try parser.nodeAppendChild(self, node);
@@ -297,19 +313,6 @@ pub const Node = struct {
297313
}
298314
}
299315

300-
// Check if the hierarchy node tree constraints are respected.
301-
// For now, it checks only if new nodes are not self.
302-
// TODO implements the others contraints.
303-
// see https://dom.spec.whatwg.org/#concept-node-tree
304-
pub fn hierarchy(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !bool {
305-
if (nodes == null) return true;
306-
if (nodes.?.slice.len == 0) return true;
307-
308-
for (nodes.?.slice) |node| if (self == node) return false;
309-
310-
return true;
311-
}
312-
313316
// TODO according with https://dom.spec.whatwg.org/#parentnode, the
314317
// function must accept either node or string.
315318
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114

src/netsurf.zig

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,21 +1764,26 @@ pub inline fn domImplementationCreateDocumentType(
17641764
}
17651765

17661766
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*DocumentHTML {
1767-
var doc: ?*Document = undefined;
1768-
const err = c.dom_implementation_create_document(
1769-
c.DOM_IMPLEMENTATION_HTML,
1770-
null,
1771-
null,
1772-
null,
1773-
null,
1774-
null,
1775-
&doc,
1776-
);
1777-
try DOMErr(err);
1778-
1779-
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
1767+
const doc_html = try documentCreateDocument(title);
1768+
const doc = documentHTMLToDocument(doc_html);
1769+
1770+
// add hierarchy: html, head, body.
1771+
const html = try documentCreateElement(doc, "html");
1772+
_ = try nodeAppendChild(documentToNode(doc), elementToNode(html));
1773+
1774+
const head = try documentCreateElement(doc, "head");
1775+
_ = try nodeAppendChild(elementToNode(html), elementToNode(head));
1776+
1777+
if (title) |t| {
1778+
try documentHTMLSetTitle(doc_html, t);
1779+
const htitle = try documentCreateElement(doc, "title");
1780+
const txt = try documentCreateTextNode(doc, t);
1781+
_ = try nodeAppendChild(elementToNode(htitle), @as(*Node, @ptrCast(txt)));
1782+
_ = try nodeAppendChild(elementToNode(head), elementToNode(htitle));
1783+
}
17801784

1781-
if (title) |t| try documentHTMLSetTitle(doc_html, t);
1785+
const body = try documentCreateElement(doc, "body");
1786+
_ = try nodeAppendChild(elementToNode(html), elementToNode(body));
17821787

17831788
return doc_html;
17841789
}
@@ -1841,6 +1846,23 @@ pub inline fn documentSetInputEncoding(doc: *Document, enc: []const u8) !void {
18411846
try DOMErr(err);
18421847
}
18431848

1849+
pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
1850+
var doc: ?*Document = undefined;
1851+
const err = c.dom_implementation_create_document(
1852+
c.DOM_IMPLEMENTATION_HTML,
1853+
null,
1854+
null,
1855+
null,
1856+
null,
1857+
null,
1858+
&doc,
1859+
);
1860+
try DOMErr(err);
1861+
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
1862+
if (title) |t| try documentHTMLSetTitle(doc_html, t);
1863+
return doc_html;
1864+
}
1865+
18441866
pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) !*Element {
18451867
var elem: ?*Element = undefined;
18461868
const err = documentVtable(doc).dom_document_create_element.?(doc, try strFromData(tag_name), &elem);

0 commit comments

Comments
 (0)