From ea3884562220b7cf67f091bbe8b966fdd5928fa7 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 5 Aug 2025 17:29:12 +0200 Subject: [PATCH] detect HTML document --- src/browser/html/elements.zig | 2 +- src/browser/netsurf.zig | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/browser/html/elements.zig b/src/browser/html/elements.zig index e985ca5d..5be01a1e 100644 --- a/src/browser/html/elements.zig +++ b/src/browser/html/elements.zig @@ -640,7 +640,7 @@ pub const HTMLImageElement = struct { pub const prototype = *HTMLImageElement; pub fn constructor(width: ?u32, height: ?u32, page: *const Page) !*parser.Image { - const element = try parser.documentCreateHTMLElement(parser.documentHTMLToDocument(page.window.document), "img"); + const element = try parser.documentCreateElement(parser.documentHTMLToDocument(page.window.document), "img"); const image: *parser.Image = @ptrCast(element); if (width) |width_| try parser.imageSetWidth(image, width_); if (height) |height_| try parser.imageSetHeight(image, height_); diff --git a/src/browser/netsurf.zig b/src/browser/netsurf.zig index 903797c7..72eedee7 100644 --- a/src/browser/netsurf.zig +++ b/src/browser/netsurf.zig @@ -2062,20 +2062,20 @@ pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*Document const doc = documentHTMLToDocument(doc_html); // add hierarchy: html, head, body. - const html = try documentCreateHTMLElement(doc, "html"); + const html = try documentCreateElement(doc, "html"); _ = try nodeAppendChild(documentToNode(doc), elementToNode(html)); - const head = try documentCreateHTMLElement(doc, "head"); + const head = try documentCreateElement(doc, "head"); _ = try nodeAppendChild(elementToNode(html), elementToNode(head)); if (title) |t| { - const htitle = try documentCreateHTMLElement(doc, "title"); + const htitle = try documentCreateElement(doc, "title"); const txt = try documentCreateTextNode(doc, t); _ = try nodeAppendChild(elementToNode(htitle), @as(*Node, @alignCast(@ptrCast(txt)))); _ = try nodeAppendChild(elementToNode(head), elementToNode(htitle)); } - const body = try documentCreateHTMLElement(doc, "body"); + const body = try documentCreateElement(doc, "body"); _ = try nodeAppendChild(elementToNode(html), elementToNode(body)); return doc_html; @@ -2156,21 +2156,29 @@ pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML { return doc_html; } -pub fn documentCreateHTMLElement(doc: *Document, tag_name: []const u8) !*Element { +fn documentCreateHTMLElement(doc: *Document, tag_name: []const u8) !*Element { + std.debug.assert(doc.is_html); + var elem: ?*Element = undefined; const err = c._dom_html_document_create_element(doc, try strFromData(tag_name), &elem); try DOMErr(err); return elem.?; } -pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) !*Element { +pub fn documentCreateElement(doc: *Document, tag_name: []const u8) !*Element { + if (doc.is_html) { + return documentCreateHTMLElement(doc, tag_name); + } + var elem: ?*Element = undefined; const err = documentVtable(doc).dom_document_create_element.?(doc, try strFromData(tag_name), &elem); try DOMErr(err); return elem.?; } -pub fn documentCreateHTMLElementNS(doc: *Document, ns: []const u8, tag_name: []const u8) !*Element { +fn documentCreateHTMLElementNS(doc: *Document, ns: []const u8, tag_name: []const u8) !*Element { + std.debug.assert(doc.is_html); + var elem: ?*Element = undefined; const err = c._dom_html_document_create_element_ns( doc, @@ -2182,7 +2190,11 @@ pub fn documentCreateHTMLElementNS(doc: *Document, ns: []const u8, tag_name: []c return elem.?; } -pub inline fn documentCreateElementNS(doc: *Document, ns: []const u8, tag_name: []const u8) !*Element { +pub fn documentCreateElementNS(doc: *Document, ns: []const u8, tag_name: []const u8) !*Element { + if (doc.is_html) { + return documentCreateHTMLElementNS(doc, ns, tag_name); + } + var elem: ?*Element = undefined; const err = documentVtable(doc).dom_document_create_element_ns.?( doc,