dom: fix processing instruction clone

This commit is contained in:
Pierre Tachoire
2024-04-05 16:33:58 +02:00
parent d58045c330
commit 49e3d569de
4 changed files with 52 additions and 9 deletions

View File

@@ -342,12 +342,14 @@ pub fn testExecFn(
var createComment = [_]Case{
.{ .src = "var v = document.createComment('foo')", .ex = "undefined" },
.{ .src = "v.nodeName", .ex = "#comment" },
.{ .src = "let v2 = v.cloneNode()", .ex = "undefined" },
};
try checkCases(js_env, &createComment);
var createProcessingInstruction = [_]Case{
.{ .src = "let pi = document.createProcessingInstruction('foo', 'bar')", .ex = "undefined" },
.{ .src = "pi.target", .ex = "foo" },
.{ .src = "let pi2 = pi.cloneNode()", .ex = "undefined" },
};
try checkCases(js_env, &createProcessingInstruction);

View File

@@ -1,6 +1,11 @@
const std = @import("std");
const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const parser = @import("../netsurf.zig");
const Node = @import("node.zig").Node;
// https://dom.spec.whatwg.org/#processinginstruction
pub const ProcessingInstruction = struct {
@@ -8,18 +13,39 @@ pub const ProcessingInstruction = struct {
// TODO for libdom processing instruction inherit from node.
// But the spec says it must inherit from CDATA.
// Moreover, inherit from Node causes also a crash with cloneNode.
// https://github.com/lightpanda-io/browsercore/issues/123
//
// In consequence, for now, we don't implement all these func for
// ProcessingInstruction.
//
//pub const prototype = *CharacterData;
pub const prototype = *Node;
pub const mem_guarantied = true;
pub fn get_target(self: *parser.ProcessingInstruction) ![]const u8 {
// libdom stores the ProcessingInstruction target in the node's name.
return try parser.nodeName(@as(*parser.Node, @ptrCast(self)));
return try parser.nodeName(parser.processingInstructionToNode(self));
}
pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool) !*parser.ProcessingInstruction {
return try parser.processInstructionCopy(self);
}
pub fn get_data(self: *parser.ProcessingInstruction) !?[]const u8 {
return try parser.nodeValue(parser.processingInstructionToNode(self));
}
pub fn set_data(self: *parser.ProcessingInstruction, data: []u8) !void {
try parser.nodeSetValue(parser.processingInstructionToNode(self), data);
}
};
pub fn testExecFn(
_: std.mem.Allocator,
js_env: *jsruntime.Env,
) anyerror!void {
var createProcessingInstruction = [_]Case{
.{ .src = "let pi = document.createProcessingInstruction('foo', 'bar')", .ex = "undefined" },
.{ .src = "pi.target", .ex = "foo" },
.{ .src = "pi.data", .ex = "bar" },
.{ .src = "pi.data = 'foo'", .ex = "foo" },
.{ .src = "pi.data", .ex = "foo" },
.{ .src = "let pi2 = pi.cloneNode()", .ex = "undefined" },
};
try checkCases(js_env, &createProcessingInstruction);
}