dom: add document.createCDATASection

This commit is contained in:
Pierre Tachoire
2023-12-07 16:27:26 +01:00
parent 894b6182cf
commit 94876d01f1
5 changed files with 34 additions and 0 deletions

12
src/dom/cdata_section.zig Normal file
View File

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

View File

@@ -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

View File

@@ -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| {

View File

@@ -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)) },

View File

@@ -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;