Lightpanda: the headless browser designed for AI and automation
Allow insertAdjacentHTML on non-HTML elements
a.dmitryukdc72c4a8199e
diff --git a/src/browser/parser/Parser.zig b/src/browser/parser/Parser.zigindex 463ff41c4..3eb7b84e2 100644--- a/src/browser/parser/Parser.zig+++ b/src/browser/parser/Parser.zig@@ -315,16 +315,20 @@ pub fn parseXML(self: *Parser, xml: []const u8) void {pub fn parseFragment(self: *Parser, html: []const u8) void {self.fragment = true;- const context_name: []const u8 = if (self.context orelse self.container.node.is(Element)) |el|- el.getLocalName()- else- "";+ const context_el = self.context orelse self.container.node.is(Element);+ const context_name: []const u8 = if (context_el) |el| el.getLocalName() else "";+ const context_ns: h5e.ContextNamespace = if (context_el) |el| switch (el._namespace) {+ .svg => .svg,+ .mathml => .mathml,+ else => .html,+ } else .html;h5e.html5ever_parse_fragment(html.ptr,html.len,context_name.ptr,context_name.len,+ context_ns,&self.container,self,createElementCallback,diff --git a/src/browser/parser/html5ever.zig b/src/browser/parser/html5ever.zigindex d83dc0a68..421dc79f5 100644--- a/src/browser/parser/html5ever.zig+++ b/src/browser/parser/html5ever.zig@@ -66,11 +66,20 @@ pub extern "c" fn html5ever_parse_document_with_encoding(allow_declarative_shadow: bool,) void;+// Namespace of the fragment parse context element, matched by+// html5ever_parse_fragment.+pub const ContextNamespace = enum(u8) {+ html = 0,+ svg = 1,+ mathml = 2,+};+pub extern "c" fn html5ever_parse_fragment(html: [*c]const u8,len: usize,context_name: [*c]const u8,context_name_len: usize,+ context_ns: ContextNamespace,doc: *anyopaque,ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,diff --git a/src/browser/tests/document/insert_adjacent_html.html b/src/browser/tests/document/insert_adjacent_html.htmlindex 3b304193b..befb11332 100644--- a/src/browser/tests/document/insert_adjacent_html.html+++ b/src/browser/tests/document/insert_adjacent_html.html@@ -130,3 +130,34 @@document.documentElement.insertAdjacentHTML('beforebegin', '<p>p</p>');});</script>++<script id=insertAdjacentHTML_svg>+ // Non-HTML elements accept it too and parse with themselves as context,+ // so the fragment lands in the SVG namespace.+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');+ document.body.appendChild(svg);+ svg.insertAdjacentHTML('beforeend', '<symbol id="iah-sym"><path d="M0 0h1"/></symbol>');+ testing.expectEqual('symbol', svg.lastChild.nodeName);+ testing.expectEqual('http://www.w3.org/2000/svg', svg.lastChild.namespaceURI);+ testing.expectEqual('http://www.w3.org/2000/svg', svg.lastChild.firstChild.namespaceURI);+ svg.firstChild.insertAdjacentHTML('beforebegin', '<g id="iah-g"></g>');+ testing.expectEqual('g', svg.firstChild.nodeName);+ testing.expectEqual('http://www.w3.org/2000/svg', svg.firstChild.namespaceURI);++ // innerHTML takes the same fragment parsing path.+ svg.innerHTML = '<foreignObject><div></div></foreignObject>';+ const fo = svg.firstChild;+ testing.expectEqual('foreignObject', fo.nodeName);+ testing.expectEqual('http://www.w3.org/2000/svg', fo.namespaceURI);+ // foreignObject is an HTML integration point: its content is HTML again.+ testing.expectEqual('http://www.w3.org/1999/xhtml', fo.firstChild.namespaceURI);+ fo.insertAdjacentHTML('beforeend', '<p>p</p>');+ testing.expectEqual('P', fo.lastChild.nodeName);+ testing.expectEqual('http://www.w3.org/1999/xhtml', fo.lastChild.namespaceURI);++ // innerText belongs to HTMLElement, not Element.+ testing.expectEqual('undefined', typeof svg.innerText);+ testing.expectEqual(false, 'innerText' in Element.prototype);+ testing.expectEqual(true, 'innerText' in HTMLElement.prototype);+ svg.remove();+</script>diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 9ea25dc6d..8bbedc8a0 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -549,8 +549,24 @@ pub fn insertAdjacentHTML(html_or_xml: []const u8,frame: *Frame,) !void {- const he = self.is(Html) orelse return error.NotHtmlElement;- return he.insertAdjacentHTML(position, html_or_xml, frame);+ const node = self.asNode();++ // The parse context is the node we insert into+ const context_node, _ = try node.findAdjacentNodes(position, .html);+ const context = if (context_node.is(Element)) |el|+ if (el.is(Html.Html) == null) el else null+ else+ null;++ const fragment = (try Node.DocumentFragment.init(node.getDocument(frame), frame)).asNode();+ try Frame.parse.fragment(frame, fragment, html_or_xml, .{ .context = context });++ const target_node, const prev_node = try node.findAdjacentNodes(position, .html);++ var iter = fragment.childrenIterator();+ while (iter.next()) |child_node| {+ _ = try target_node.insertBefore(child_node, prev_node, frame);+ }}pub fn requestFullscreen(_: *const Element, frame: *Frame) js.Promise {@@ -2649,13 +2665,6 @@ pub const JsApi = struct {// outside the built-in set instead of the placeholderpub const namespaceURI = bridge.accessor(Element.getNamespaceUri, null, .{});- pub const innerText = bridge.accessor(_innerText, Element.setInnerText, .{ .ce_reactions = true });- fn _innerText(self: *Element, frame: *Frame) ![]const u8 {- var buf = std.Io.Writer.Allocating.init(frame.local_arena);- try self.getInnerText(&buf.writer, frame);- return buf.written();- }-pub const outerHTML = bridge.accessor(_getOuterHTML, _setOuterHTML, .{ .ce_reactions = true });fn _getOuterHTML(self: *Element, frame: *Frame) ![]const u8 {// local_arena: serialization is read-only and the returned string isdiff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zigindex 8b1e299ac..1a42510cf 100644--- a/src/browser/webapi/element/Html.zig+++ b/src/browser/webapi/element/Html.zig@@ -404,32 +404,6 @@ pub fn setOuterText(self: *HtmlElement, text: []const u8, frame: *Frame) !void {}}-pub fn insertAdjacentHTML(- self: *HtmlElement,- position: []const u8,- html: []const u8,- frame: *Frame,-) !void {- const DocumentFragment = @import("../DocumentFragment.zig");-- // The parse context is the node we insert into- const context_node, _ = try self.asNode().findAdjacentNodes(position, .html);- const context = if (context_node.is(Element)) |el|- if (el.is(Element.Html.Html) == null) el else null- else- null;-- const fragment = (try DocumentFragment.init(self.asNode().getDocument(frame), frame)).asNode();- try Frame.parse.fragment(frame, fragment, html, .{ .context = context });-- const target_node, const prev_node = try self.asNode().findAdjacentNodes(position, .html);-- var iter = fragment.childrenIterator();- while (iter.next()) |child_node| {- _ = try target_node.insertBefore(child_node, prev_node, frame);- }-}-pub fn click(self: *HtmlElement, frame: *Frame) !void {switch (self._type) {.button, .input, .textarea, .select, .option, .optgroup => if (self.asElement().isDisabled()) return,@@ -1151,7 +1125,6 @@ pub const JsApi = struct {fn _setOuterText(self: *HtmlElement, value: js.Value, frame: *Frame) !void {return self.setOuterText(if (value.isNull()) "" else try value.toZig([]const u8), frame);}- pub const insertAdjacentHTML = bridge.function(HtmlElement.insertAdjacentHTML, .{ .ce_reactions = true });pub const click = bridge.function(HtmlElement.click, .{});pub const attachInternals = bridge.function(HtmlElement.attachInternals, .{});diff --git a/src/rust/html5ever/lib.rs b/src/rust/html5ever/lib.rsindex 8b0d2e272..826dd6df6 100644--- a/src/rust/html5ever/lib.rs+++ b/src/rust/html5ever/lib.rs@@ -481,6 +481,7 @@ pub extern "C" fn html5ever_parse_fragment(len: usize,context_name: *const c_uchar,context_name_len: usize,+ context_ns: u8,document: Ref,ctx: Ref,create_element_callback: CreateElementCallback,@@ -554,7 +555,14 @@ pub extern "C" fn html5ever_parse_fragment(..Default::default()};- let context_qname = QualName::new(None, ns!(html), context_local);+ // An svg or math context makes the fragment foreign content, so e.g.+ // <symbol> lands in the SVG namespace (0: html, 1: svg, 2: mathml).+ let context_namespace = match context_ns {+ 1 => ns!(svg),+ 2 => ns!(mathml),+ _ => ns!(html),+ };+ let context_qname = QualName::new(None, context_namespace, context_local);let context_data = arena.alloc(sink::ElementData {qname: context_qname.clone(),mathml_annotation_xml_integration_point: false,