mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
dom: fix document creation process
This commit is contained in:
@@ -53,7 +53,7 @@ pub const Document = struct {
|
||||
if (userctx.document) |cur| {
|
||||
title = try parser.documentHTMLGetTitle(cur);
|
||||
}
|
||||
const doc = try parser.domImplementationCreateHTMLDocument(title);
|
||||
const doc = try parser.documentCreateDocument(title);
|
||||
|
||||
if (userctx.document) |cur| {
|
||||
// we have to work w/ document instead of html document.
|
||||
|
||||
@@ -96,6 +96,7 @@ pub fn testExecFn(
|
||||
var getImplementation = [_]Case{
|
||||
.{ .src = "let impl = document.implementation", .ex = "undefined" },
|
||||
.{ .src = "impl.createHTMLDocument();", .ex = "[object HTMLDocument]" },
|
||||
.{ .src = "impl.createHTMLDocument('foo');", .ex = "[object HTMLDocument]" },
|
||||
.{ .src = "impl.createDocument(null, 'foo');", .ex = "[object Document]" },
|
||||
.{ .src = "impl.createDocumentType('foo', 'bar', 'baz')", .ex = "[object DocumentType]" },
|
||||
.{ .src = "impl.hasFeature()", .ex = "true" },
|
||||
|
||||
@@ -277,26 +277,6 @@ pub const Node = struct {
|
||||
return try Node.toInterface(res);
|
||||
}
|
||||
|
||||
// TODO according with https://dom.spec.whatwg.org/#parentnode, the
|
||||
// function must accept either node or string.
|
||||
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
|
||||
pub fn prepend(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
|
||||
if (nodes == null) return;
|
||||
if (nodes.?.slice.len == 0) return;
|
||||
const first = try parser.nodeFirstChild(self);
|
||||
|
||||
if (first == null) {
|
||||
for (nodes.?.slice) |node| {
|
||||
_ = try parser.nodeAppendChild(self, node);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (nodes.?.slice) |node| {
|
||||
_ = try parser.nodeInsertBefore(self, node, first.?);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the hierarchy node tree constraints are respected.
|
||||
// For now, it checks only if new nodes are not self.
|
||||
// TODO implements the others contraints.
|
||||
@@ -310,6 +290,29 @@ pub const Node = struct {
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO according with https://dom.spec.whatwg.org/#parentnode, the
|
||||
// function must accept either node or string.
|
||||
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
|
||||
pub fn prepend(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
|
||||
if (nodes == null) return;
|
||||
if (nodes.?.slice.len == 0) return;
|
||||
|
||||
// check hierarchy
|
||||
if (!try hierarchy(self, nodes)) return parser.DOMError.HierarchyRequest;
|
||||
|
||||
const first = try parser.nodeFirstChild(self);
|
||||
if (first == null) {
|
||||
for (nodes.?.slice) |node| {
|
||||
_ = try parser.nodeAppendChild(self, node);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (nodes.?.slice) |node| {
|
||||
_ = try parser.nodeInsertBefore(self, node, first.?);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO according with https://dom.spec.whatwg.org/#parentnode, the
|
||||
// function must accept either node or string.
|
||||
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
|
||||
|
||||
@@ -1764,21 +1764,26 @@ pub inline fn domImplementationCreateDocumentType(
|
||||
}
|
||||
|
||||
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*DocumentHTML {
|
||||
var doc: ?*Document = undefined;
|
||||
const err = c.dom_implementation_create_document(
|
||||
c.DOM_IMPLEMENTATION_HTML,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
&doc,
|
||||
);
|
||||
try DOMErr(err);
|
||||
const doc_html = try documentCreateDocument(title);
|
||||
const doc = documentHTMLToDocument(doc_html);
|
||||
|
||||
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
// add hierarchy: html, head, body.
|
||||
const html = try documentCreateElement(doc, "html");
|
||||
_ = try nodeAppendChild(documentToNode(doc), elementToNode(html));
|
||||
|
||||
if (title) |t| try documentHTMLSetTitle(doc_html, t);
|
||||
const head = try documentCreateElement(doc, "head");
|
||||
_ = try nodeAppendChild(elementToNode(html), elementToNode(head));
|
||||
|
||||
if (title) |t| {
|
||||
try documentHTMLSetTitle(doc_html, t);
|
||||
const htitle = try documentCreateElement(doc, "title");
|
||||
const txt = try documentCreateTextNode(doc, t);
|
||||
_ = try nodeAppendChild(elementToNode(htitle), @as(*Node, @ptrCast(txt)));
|
||||
_ = try nodeAppendChild(elementToNode(head), elementToNode(htitle));
|
||||
}
|
||||
|
||||
const body = try documentCreateElement(doc, "body");
|
||||
_ = try nodeAppendChild(elementToNode(html), elementToNode(body));
|
||||
|
||||
return doc_html;
|
||||
}
|
||||
@@ -1841,6 +1846,23 @@ pub inline fn documentSetInputEncoding(doc: *Document, enc: []const u8) !void {
|
||||
try DOMErr(err);
|
||||
}
|
||||
|
||||
pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
|
||||
var doc: ?*Document = undefined;
|
||||
const err = c.dom_implementation_create_document(
|
||||
c.DOM_IMPLEMENTATION_HTML,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
&doc,
|
||||
);
|
||||
try DOMErr(err);
|
||||
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
if (title) |t| try documentHTMLSetTitle(doc_html, t);
|
||||
return doc_html;
|
||||
}
|
||||
|
||||
pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) !*Element {
|
||||
var elem: ?*Element = undefined;
|
||||
const err = documentVtable(doc).dom_document_create_element.?(doc, try strFromData(tag_name), &elem);
|
||||
|
||||
Reference in New Issue
Block a user