@@ -28,6 +28,8 @@ const parser = @import("../netsurf.zig");
2828const CharacterData = @import ("character_data.zig" ).CharacterData ;
2929const CDATASection = @import ("cdata_section.zig" ).CDATASection ;
3030
31+ const UserContext = @import ("../user_context.zig" ).UserContext ;
32+
3133// Text interfaces
3234pub const Interfaces = generate .Tuple (.{
3335 CDATASection ,
@@ -44,8 +46,13 @@ pub const Text = struct {
4446 // > and this’s node document to current global object’s associated
4547 // > Document.
4648 // https://dom.spec.whatwg.org/#dom-text-text
47- pub fn constructor () ! * parser.Comment {
48- return error .NotImplemented ;
49+ pub fn constructor (userctx : UserContext , data : ? []const u8 ) ! * parser.Text {
50+ if (userctx .document == null ) return parser .DOMError .NotSupported ;
51+
52+ return parser .documentCreateTextNode (
53+ parser .documentHTMLToDocument (userctx .document .? ),
54+ data orelse "" ,
55+ );
4956 }
5057
5158 // JS funcs
@@ -72,6 +79,15 @@ pub fn testExecFn(
7279 _ : std.mem.Allocator ,
7380 js_env : * jsruntime.Env ,
7481) anyerror ! void {
82+ var constructor = [_ ]Case {
83+ .{ .src = "let t = new Text('foo')" , .ex = "undefined" },
84+ .{ .src = "t.data" , .ex = "foo" },
85+
86+ .{ .src = "let emptyt = new Text()" , .ex = "undefined" },
87+ .{ .src = "emptyt.data" , .ex = "" },
88+ };
89+ try checkCases (js_env , & constructor );
90+
7591 var get_whole_text = [_ ]Case {
7692 .{ .src = "let text = document.getElementById('link').firstChild" , .ex = "undefined" },
7793 .{ .src = "text.wholeText === 'OK'" , .ex = "true" },
0 commit comments