From 49e3d569decec493d672881be019e97c3a96f4e8 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 5 Apr 2024 16:33:58 +0200 Subject: [PATCH] dom: fix processing instruction clone --- src/dom/document.zig | 2 ++ src/dom/processing_instruction.zig | 44 ++++++++++++++++++++++++------ src/netsurf.zig | 13 +++++++++ src/run_tests.zig | 2 ++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index c99cc5fc..d91ab8f7 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -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); diff --git a/src/dom/processing_instruction.zig b/src/dom/processing_instruction.zig index 2fa24eea..b7ca628e 100644 --- a/src/dom/processing_instruction.zig +++ b/src/dom/processing_instruction.zig @@ -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); +} diff --git a/src/netsurf.zig b/src/netsurf.zig index 1fb41d4a..619c37c7 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -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; diff --git a/src/run_tests.zig b/src/run_tests.zig index 84ca45fd..d9b1665c 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -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| {