Skip to content

Commit b2df0c1

Browse files
committed
dom: implement document fragment constructor
1 parent d823eeb commit b2df0c1

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/dom/document_fragment.zig

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,39 @@ const std = @import("std");
2020

2121
const parser = @import("../netsurf.zig");
2222

23+
const jsruntime = @import("jsruntime");
24+
const Case = jsruntime.test_utils.Case;
25+
const checkCases = jsruntime.test_utils.checkCases;
26+
2327
const Node = @import("node.zig").Node;
2428

29+
const UserContext = @import("../user_context.zig").UserContext;
30+
2531
// WEB IDL https://dom.spec.whatwg.org/#documentfragment
2632
pub const DocumentFragment = struct {
2733
pub const Self = parser.DocumentFragment;
2834
pub const prototype = *Node;
2935
pub const mem_guarantied = true;
3036

31-
// TODO add constructor, but I need to associate the new DocumentFragment
32-
// with the current document global object...
33-
// > The new DocumentFragment() constructor steps are to set this’s node
34-
// > document to current global object’s associated Document.
35-
// https://dom.spec.whatwg.org/#dom-documentfragment-documentfragment
36-
pub fn constructor() !*parser.DocumentFragment {
37-
return error.NotImplemented;
37+
pub fn constructor(userctx: UserContext) !*parser.DocumentFragment {
38+
if (userctx.document == null) return parser.DOMError.NotSupported;
39+
40+
return parser.documentCreateDocumentFragment(
41+
parser.documentHTMLToDocument(userctx.document.?),
42+
);
3843
}
3944
};
45+
46+
// Tests
47+
// -----
48+
49+
pub fn testExecFn(
50+
_: std.mem.Allocator,
51+
js_env: *jsruntime.Env,
52+
) anyerror!void {
53+
var constructor = [_]Case{
54+
.{ .src = "const dc = new DocumentFragment()", .ex = "undefined" },
55+
.{ .src = "dc.constructor.name", .ex = "DocumentFragment" },
56+
};
57+
try checkCases(js_env, &constructor);
58+
}

src/run_tests.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ 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;
4949
const CommentTestExecFn = @import("dom/comment.zig").testExecFn;
50+
const DocumentFragmentTestExecFn = @import("dom/document_fragment.zig").testExecFn;
5051
const EventTestExecFn = @import("events/event.zig").testExecFn;
5152
const XHRTestExecFn = xhr.testExecFn;
5253
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
@@ -116,6 +117,7 @@ fn testsAllExecFn(
116117
NodeListTestExecFn,
117118
AttrTestExecFn,
118119
CommentTestExecFn,
120+
DocumentFragmentTestExecFn,
119121
EventTargetTestExecFn,
120122
EventTestExecFn,
121123
XHRTestExecFn,

0 commit comments

Comments
 (0)