@@ -20,20 +20,39 @@ const std = @import("std");
2020
2121const 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+
2327const Node = @import ("node.zig" ).Node ;
2428
29+ const UserContext = @import ("../user_context.zig" ).UserContext ;
30+
2531// WEB IDL https://dom.spec.whatwg.org/#documentfragment
2632pub 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+ }
0 commit comments