detect HTML document

This commit is contained in:
Pierre Tachoire
2025-08-05 17:29:12 +02:00
parent 81a0e95916
commit ea38845622
2 changed files with 21 additions and 9 deletions

View File

@@ -640,7 +640,7 @@ pub const HTMLImageElement = struct {
pub const prototype = *HTMLImageElement; pub const prototype = *HTMLImageElement;
pub fn constructor(width: ?u32, height: ?u32, page: *const Page) !*parser.Image { 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); const image: *parser.Image = @ptrCast(element);
if (width) |width_| try parser.imageSetWidth(image, width_); if (width) |width_| try parser.imageSetWidth(image, width_);
if (height) |height_| try parser.imageSetHeight(image, height_); if (height) |height_| try parser.imageSetHeight(image, height_);

View File

@@ -2062,20 +2062,20 @@ pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*Document
const doc = documentHTMLToDocument(doc_html); const doc = documentHTMLToDocument(doc_html);
// add hierarchy: html, head, body. // add hierarchy: html, head, body.
const html = try documentCreateHTMLElement(doc, "html"); const html = try documentCreateElement(doc, "html");
_ = try nodeAppendChild(documentToNode(doc), elementToNode(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)); _ = try nodeAppendChild(elementToNode(html), elementToNode(head));
if (title) |t| { if (title) |t| {
const htitle = try documentCreateHTMLElement(doc, "title"); const htitle = try documentCreateElement(doc, "title");
const txt = try documentCreateTextNode(doc, t); const txt = try documentCreateTextNode(doc, t);
_ = try nodeAppendChild(elementToNode(htitle), @as(*Node, @alignCast(@ptrCast(txt)))); _ = try nodeAppendChild(elementToNode(htitle), @as(*Node, @alignCast(@ptrCast(txt))));
_ = try nodeAppendChild(elementToNode(head), elementToNode(htitle)); _ = try nodeAppendChild(elementToNode(head), elementToNode(htitle));
} }
const body = try documentCreateHTMLElement(doc, "body"); const body = try documentCreateElement(doc, "body");
_ = try nodeAppendChild(elementToNode(html), elementToNode(body)); _ = try nodeAppendChild(elementToNode(html), elementToNode(body));
return doc_html; return doc_html;
@@ -2156,21 +2156,29 @@ pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
return doc_html; 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; var elem: ?*Element = undefined;
const err = c._dom_html_document_create_element(doc, try strFromData(tag_name), &elem); const err = c._dom_html_document_create_element(doc, try strFromData(tag_name), &elem);
try DOMErr(err); try DOMErr(err);
return elem.?; 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; var elem: ?*Element = undefined;
const err = documentVtable(doc).dom_document_create_element.?(doc, try strFromData(tag_name), &elem); const err = documentVtable(doc).dom_document_create_element.?(doc, try strFromData(tag_name), &elem);
try DOMErr(err); try DOMErr(err);
return elem.?; 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; var elem: ?*Element = undefined;
const err = c._dom_html_document_create_element_ns( const err = c._dom_html_document_create_element_ns(
doc, doc,
@@ -2182,7 +2190,11 @@ pub fn documentCreateHTMLElementNS(doc: *Document, ns: []const u8, tag_name: []c
return elem.?; 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; var elem: ?*Element = undefined;
const err = documentVtable(doc).dom_document_create_element_ns.?( const err = documentVtable(doc).dom_document_create_element_ns.?(
doc, doc,