From e82240a60ed2d8256bd659b707dcb011a929177f Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 9 Jul 2025 12:48:44 +0800 Subject: [PATCH] Setting innerHTML now captures head elements I couldn't find where the behavior is described. AND, browsers seem to behave differently depending on the state of the page (blank document vs actual page). Still, some sites use innerHTML to load '; + // does _not_ create an empty script, but in a real page, it does. Weird. + const fragment_node = parser.documentFragmentToNode(fragment); + const html = try parser.nodeFirstChild(fragment_node) orelse return; + const head = try parser.nodeFirstChild(html) orelse return; + { + // First, copy some of the head element + const children = try parser.nodeGetChildNodes(head); + const ln = try parser.nodeListLength(children); + for (0..ln) |_| { + // always index 0, because nodeAppendChild moves the node out of + // the nodeList and into the new tree + const child = try parser.nodeListItem(children, 0) orelse continue; + _ = try parser.nodeAppendChild(node, child); + } + } - // append children to the node - const ln = try parser.nodeListLength(children); - for (0..ln) |_| { - // always index 0, because ndoeAppendChild moves the node out of - // the nodeList and into the new tree - const child = try parser.nodeListItem(children, 0) orelse continue; - _ = try parser.nodeAppendChild(node, child); + { + const body = try parser.nodeNextSibling(head) orelse return; + const children = try parser.nodeGetChildNodes(body); + const ln = try parser.nodeListLength(children); + for (0..ln) |_| { + // always index 0, because nodeAppendChild moves the node out of + // the nodeList and into the new tree + const child = try parser.nodeListItem(children, 0) orelse continue; + _ = try parser.nodeAppendChild(node, child); + } } } @@ -688,5 +712,10 @@ test "Browser.DOM.Element" { try runner.testCases(&.{ .{ "document.createElement('a').hasAttributes()", "false" }, + .{ "var fc; (fc = document.createElement('div')).innerHTML = '" }, + + .{ "fc; (fc = document.createElement('div')).innerHTML = '

hello

" }, }, .{}); } diff --git a/src/browser/netsurf.zig b/src/browser/netsurf.zig index 0a34bcb9..0b11a1dd 100644 --- a/src/browser/netsurf.zig +++ b/src/browser/netsurf.zig @@ -1925,18 +1925,6 @@ pub inline fn documentFragmentToNode(doc: *DocumentFragment) *Node { return @as(*Node, @alignCast(@ptrCast(doc))); } -pub fn documentFragmentBodyChildren(doc: *DocumentFragment) !?*NodeList { - const node = documentFragmentToNode(doc); - const html = try nodeFirstChild(node) orelse return null; - // TODO unref - const head = try nodeFirstChild(html) orelse return null; - // TODO unref - const body = try nodeNextSibling(head) orelse return null; - // TODO unref - - return try nodeGetChildNodes(body); -} - // Document Position pub const DocumentPosition = enum(u32) {