Skip to content

Commit 55b80ec

Browse files
committed
dom: implement text constructor
1 parent 14e1c44 commit 55b80ec

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/dom/text.zig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const parser = @import("../netsurf.zig");
2828
const CharacterData = @import("character_data.zig").CharacterData;
2929
const CDATASection = @import("cdata_section.zig").CDATASection;
3030

31+
const UserContext = @import("../user_context.zig").UserContext;
32+
3133
// Text interfaces
3234
pub 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

Comments
 (0)