Skip to content

Commit 69eaf0d

Browse files
committed
Manually "clone" processing_instruction
When you clone a processing_node via the node_clone_node, or directly via the processing_node copy, you end up in _dom_pi_copy: https://github.com/lightpanda-io/libdom/blob/da8b967905a38455e4f6b75cc9ad2767bae3ccde/src/core/pi.c#L104 For whatever, reason, the node created here gets a vtable that doesn't seem compatible with how we cast vtables in netsurf.zig. For now, a simple fix is to create a new new and copy the attributes over. Fixes #123 and a WPT crash.
1 parent 4a696b4 commit 69eaf0d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/browser/dom/processing_instruction.zig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const std = @import("std");
2020

2121
const parser = @import("../netsurf.zig");
2222
const Node = @import("node.zig").Node;
23+
const SessionState = @import("../env.zig").SessionState;
2324

2425
// https://dom.spec.whatwg.org/#processinginstruction
2526
pub const ProcessingInstruction = struct {
@@ -35,8 +36,15 @@ pub const ProcessingInstruction = struct {
3536
return try parser.nodeName(parser.processingInstructionToNode(self));
3637
}
3738

38-
pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool) !*parser.ProcessingInstruction {
39-
return try parser.processInstructionCopy(self);
39+
// There's something wrong when we try to clone a ProcessInstruction normally.
40+
// The resulting object can't be cast back into a node (it crashes). This is
41+
// a simple workaround.
42+
pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool, state: *SessionState) !*parser.ProcessingInstruction {
43+
return try parser.documentCreateProcessingInstruction(
44+
@ptrCast(state.document),
45+
try get_target(self),
46+
(try get_data(self)) orelse "",
47+
);
4048
}
4149

4250
pub fn get_data(self: *parser.ProcessingInstruction) !?[]const u8 {
@@ -61,5 +69,6 @@ test "Browser.DOM.ProcessingInstruction" {
6169
.{ "pi.data", "foo" },
6270

6371
.{ "let pi2 = pi.cloneNode()", "undefined" },
72+
.{ "pi2.nodeType", "7" },
6473
}, .{});
6574
}

0 commit comments

Comments
 (0)