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);
}

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const c = @cImport({
@cInclude("dom/dom.h");
@cInclude("core/pi.h");
@cInclude("dom/bindings/hubbub/parser.h");
@cInclude("events/event_target.h");
@cInclude("events/event.h");
@@ -1235,6 +1236,18 @@ pub const Comment = c.dom_comment;
// ProcessingInstruction
pub const ProcessingInstruction = c.dom_processing_instruction;
// processingInstructionToNode is an helper to convert an ProcessingInstruction to a node.
pub inline fn processingInstructionToNode(pi: *ProcessingInstruction) *Node {
return @as(*Node, @ptrCast(pi));
}
pub fn processInstructionCopy(pi: *ProcessingInstruction) !*ProcessingInstruction {
var res: ?*Node = undefined;
const err = c._dom_pi_copy(processingInstructionToNode(pi), &res);
try DOMErr(err);
return @as(*ProcessingInstruction, @ptrCast(res.?));
}
// Attribute
pub const Attribute = c.dom_attr;

View File

@@ -23,6 +23,7 @@ const DOMTokenListExecFn = @import("dom/token_list.zig").testExecFn;
const NodeListTestExecFn = @import("dom/nodelist.zig").testExecFn;
const AttrTestExecFn = @import("dom/attribute.zig").testExecFn;
const EventTargetTestExecFn = @import("dom/event_target.zig").testExecFn;
const ProcessingInstructionTestExecFn = @import("dom/processing_instruction.zig").testExecFn;
const EventTestExecFn = @import("events/event.zig").testExecFn;
const XHRTestExecFn = xhr.testExecFn;
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
@@ -80,6 +81,7 @@ fn testsAllExecFn(
EventTestExecFn,
XHRTestExecFn,
ProgressEventTestExecFn,
ProcessingInstructionTestExecFn,
};
inline for (testFns) |testFn| {