Skip to content

Commit 14e1c44

Browse files
committed
dom: implement comment constructor
1 parent eef2fa9 commit 14e1c44

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/dom/comment.zig

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
//
1616
// You should have received a copy of the GNU Affero General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
const std = @import("std");
1819

1920
const parser = @import("../netsurf.zig");
2021

22+
const jsruntime = @import("jsruntime");
23+
const Case = jsruntime.test_utils.Case;
24+
const checkCases = jsruntime.test_utils.checkCases;
25+
2126
const CharacterData = @import("character_data.zig").CharacterData;
2227

28+
const UserContext = @import("../user_context.zig").UserContext;
29+
2330
// https://dom.spec.whatwg.org/#interface-comment
2431
pub const Comment = struct {
2532
pub const Self = parser.Comment;
@@ -32,7 +39,29 @@ pub const Comment = struct {
3239
// > and this’s node document to current global object’s associated
3340
// > Document.
3441
// https://dom.spec.whatwg.org/#dom-comment-comment
35-
pub fn constructor() !*parser.Comment {
36-
return error.NotImplemented;
42+
pub fn constructor(userctx: UserContext, data: ?[]const u8) !*parser.Comment {
43+
if (userctx.document == null) return parser.DOMError.NotSupported;
44+
45+
return parser.documentCreateComment(
46+
parser.documentHTMLToDocument(userctx.document.?),
47+
data orelse "",
48+
);
3749
}
3850
};
51+
52+
// Tests
53+
// -----
54+
55+
pub fn testExecFn(
56+
_: std.mem.Allocator,
57+
js_env: *jsruntime.Env,
58+
) anyerror!void {
59+
var constructor = [_]Case{
60+
.{ .src = "let comment = new Comment('foo')", .ex = "undefined" },
61+
.{ .src = "comment.data", .ex = "foo" },
62+
63+
.{ .src = "let emptycomment = new Comment()", .ex = "undefined" },
64+
.{ .src = "emptycomment.data", .ex = "" },
65+
};
66+
try checkCases(js_env, &constructor);
67+
}

src/run_tests.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const NodeListTestExecFn = @import("dom/nodelist.zig").testExecFn;
4646
const AttrTestExecFn = @import("dom/attribute.zig").testExecFn;
4747
const EventTargetTestExecFn = @import("dom/event_target.zig").testExecFn;
4848
const ProcessingInstructionTestExecFn = @import("dom/processing_instruction.zig").testExecFn;
49+
const CommentTestExecFn = @import("dom/comment.zig").testExecFn;
4950
const EventTestExecFn = @import("events/event.zig").testExecFn;
5051
const XHRTestExecFn = xhr.testExecFn;
5152
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
@@ -114,6 +115,7 @@ fn testsAllExecFn(
114115
DOMTokenListExecFn,
115116
NodeListTestExecFn,
116117
AttrTestExecFn,
118+
CommentTestExecFn,
117119
EventTargetTestExecFn,
118120
EventTestExecFn,
119121
XHRTestExecFn,

0 commit comments

Comments
 (0)