From 5fb0f5a2048318b4aa7344c9326466b9af5e4ff0 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 20 May 2026 14:29:30 +0800 Subject: [PATCH] parseHtmlAsChildren handling for unexpected dom (custom element callback) Removes an assertion that can break with custom element callbacks. https://github.com/lightpanda-io/browser/pull/2429 does not solve this issue since it isn't a result of when reactions are executed, but just that they happen. (I should note, that I'm not 100% sure the above statement is correct. It's possible that our CE reactions are (in some cases at least) too micro. Maybe some operations, like setInnerHtml should operate within a more atomic frameworks, vs an CE-reaction per internal step. But that's a pretty big change) --- src/browser/Frame.zig | 12 +++++-- .../parser_wrapper_removed.html | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/browser/tests/custom_elements/parser_wrapper_removed.html diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig index 1f081ded..7136dcea 100644 --- a/src/browser/Frame.zig +++ b/src/browser/Frame.zig @@ -3472,10 +3472,16 @@ pub fn parseHtmlAsChildren(self: *Frame, node: *Node, html: []const u8) !void { var parser = Parser.init(self.call_arena, node, self); parser.parseFragment(html); - // https://github.com/servo/html5ever/issues/583 + // html5ever wraps fragment output in an element; unwrap so its + // children land directly on `node`. See https://github.com/servo/html5ever/issues/583. + // Because of custom element callbacks, the structure might not be what + // we expect, and nodes might be altogether removed. We deal with this in a + // few different places, but always the same way: leave it as-is. const children = node._children orelse return; - const first = children.one; - lp.assert(first.is(Element.Html.Html) != null, "Frame.parseHtmlAsChildren root", .{ .type = first._type }); + const first = children.first(); + if (first.is(Element.Html.Html) == null) { + return; + } node._children = first._children; if (self.hasMutationObservers()) { diff --git a/src/browser/tests/custom_elements/parser_wrapper_removed.html b/src/browser/tests/custom_elements/parser_wrapper_removed.html new file mode 100644 index 00000000..77c637eb --- /dev/null +++ b/src/browser/tests/custom_elements/parser_wrapper_removed.html @@ -0,0 +1,36 @@ + + + + + + + + +