From 94876d01f187eda1c04d033b4cc1deb371b375e2 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 7 Dec 2023 16:27:26 +0100 Subject: [PATCH] dom: add document.createCDATASection --- src/dom/cdata_section.zig | 12 ++++++++++++ src/dom/character_data.zig | 2 ++ src/dom/document.zig | 9 +++++++++ src/dom/node.zig | 1 + src/netsurf.zig | 10 ++++++++++ 5 files changed, 34 insertions(+) create mode 100644 src/dom/cdata_section.zig diff --git a/src/dom/cdata_section.zig b/src/dom/cdata_section.zig new file mode 100644 index 00000000..8f02ceb1 --- /dev/null +++ b/src/dom/cdata_section.zig @@ -0,0 +1,12 @@ +const std = @import("std"); + +const parser = @import("../netsurf.zig"); + +const Text = @import("text.zig").Text; + +// https://dom.spec.whatwg.org/#cdatasection +pub const CDATASection = struct { + pub const Self = parser.CDATASection; + pub const prototype = *Text; + pub const mem_guarantied = true; +}; diff --git a/src/dom/character_data.zig b/src/dom/character_data.zig index 8f139691..b515b719 100644 --- a/src/dom/character_data.zig +++ b/src/dom/character_data.zig @@ -10,12 +10,14 @@ const parser = @import("../netsurf.zig"); const Node = @import("node.zig").Node; const Comment = @import("comment.zig").Comment; const Text = @import("text.zig").Text; +const CDATASection = @import("cdata_section.zig").CDATASection; const HTMLElem = @import("../html/elements.zig"); // CharacterData interfaces pub const Interfaces = generate.Tuple(.{ Comment, Text, + CDATASection, }); // CharacterData implementation diff --git a/src/dom/document.zig b/src/dom/document.zig index ea29dad8..aa4a5c6a 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -133,6 +133,10 @@ pub const Document = struct { return try parser.documentCreateTextNode(self, data); } + pub fn _createCDATASection(self: *parser.Document, data: []const u8) !*parser.CDATASection { + return try parser.documentCreateCDATASection(self, data); + } + pub fn deinit(_: *parser.Document, _: std.mem.Allocator) void {} }; @@ -235,6 +239,11 @@ pub fn testExecFn( }; try checkCases(js_env, &createTextNode); + var createCDATASection = [_]Case{ + .{ .src = "document.createCDATASection('foo')", .ex = "[object CDATASection]" }, + }; + try checkCases(js_env, &createCDATASection); + const tags = comptime parser.Tag.all(); comptime var createElements: [(tags.len) * 2]Case = undefined; inline for (tags, 0..) |tag, i| { diff --git a/src/dom/node.zig b/src/dom/node.zig index 022aa9c7..b700b73f 100644 --- a/src/dom/node.zig +++ b/src/dom/node.zig @@ -54,6 +54,7 @@ pub const Node = struct { ), .comment => .{ .Comment = @as(*parser.Comment, @ptrCast(node)) }, .text => .{ .Text = @as(*parser.Text, @ptrCast(node)) }, + .cdata_section => .{ .CDATASection = @as(*parser.CDATASection, @ptrCast(node)) }, .document => .{ .HTMLDocument = @as(*parser.DocumentHTML, @ptrCast(node)) }, .document_type => .{ .DocumentType = @as(*parser.DocumentType, @ptrCast(node)) }, .attribute => .{ .Attr = @as(*parser.Attribute, @ptrCast(node)) }, diff --git a/src/netsurf.zig b/src/netsurf.zig index b764c9dd..3042da20 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -791,6 +791,9 @@ pub fn characterDataSubstringData(cdata: *CharacterData, offset: u32, count: u32 return strToData(s.?); } +// CDATASection +pub const CDATASection = c.dom_cdata_section; + // Text pub const Text = c.dom_text; @@ -1144,6 +1147,13 @@ pub inline fn documentCreateTextNode(doc: *Document, s: []const u8) !*Text { return txt.?; } +pub inline fn documentCreateCDATASection(doc: *Document, s: []const u8) !*CDATASection { + var cdata: ?*CDATASection = undefined; + const err = documentVtable(doc).dom_document_create_cdata_section.?(doc, try strFromData(s), &cdata); + try DOMErr(err); + return cdata.?; +} + // DocumentHTML pub const DocumentHTML = c.dom_html_document;