dom: implement comment constructor

This commit is contained in:
Pierre Tachoire
2024-04-19 18:01:22 +02:00
parent eef2fa94d0
commit 14e1c44eb0
2 changed files with 33 additions and 2 deletions

View File

@@ -15,11 +15,18 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const CharacterData = @import("character_data.zig").CharacterData;
const UserContext = @import("../user_context.zig").UserContext;
// https://dom.spec.whatwg.org/#interface-comment
pub const Comment = struct {
pub const Self = parser.Comment;
@@ -32,7 +39,29 @@ pub const Comment = struct {
// > and thiss node document to current global objects associated
// > Document.
// https://dom.spec.whatwg.org/#dom-comment-comment
pub fn constructor() !*parser.Comment {
return error.NotImplemented;
pub fn constructor(userctx: UserContext, data: ?[]const u8) !*parser.Comment {
if (userctx.document == null) return parser.DOMError.NotSupported;
return parser.documentCreateComment(
parser.documentHTMLToDocument(userctx.document.?),
data orelse "",
);
}
};
// Tests
// -----
pub fn testExecFn(
_: std.mem.Allocator,
js_env: *jsruntime.Env,
) anyerror!void {
var constructor = [_]Case{
.{ .src = "let comment = new Comment('foo')", .ex = "undefined" },
.{ .src = "comment.data", .ex = "foo" },
.{ .src = "let emptycomment = new Comment()", .ex = "undefined" },
.{ .src = "emptycomment.data", .ex = "" },
};
try checkCases(js_env, &constructor);
}

View File

@@ -46,6 +46,7 @@ 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 CommentTestExecFn = @import("dom/comment.zig").testExecFn;
const EventTestExecFn = @import("events/event.zig").testExecFn;
const XHRTestExecFn = xhr.testExecFn;
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
@@ -114,6 +115,7 @@ fn testsAllExecFn(
DOMTokenListExecFn,
NodeListTestExecFn,
AttrTestExecFn,
CommentTestExecFn,
EventTargetTestExecFn,
EventTestExecFn,
XHRTestExecFn,