diff --git a/src/dom/text.zig b/src/dom/text.zig index 2481c850..cb69e93a 100644 --- a/src/dom/text.zig +++ b/src/dom/text.zig @@ -1,3 +1,9 @@ +const std = @import("std"); + +const jsruntime = @import("jsruntime"); +const Case = jsruntime.test_utils.Case; +const checkCases = jsruntime.test_utils.checkCases; + const parser = @import("../netsurf.zig"); const CharacterData = @import("character_data.zig").CharacterData; @@ -6,4 +12,43 @@ pub const Text = struct { pub const Self = parser.Text; pub const prototype = *CharacterData; pub const mem_guarantied = true; + + // JS funcs + // -------- + + // Read attributes + + pub fn get_wholeText(self: *parser.Text) []const u8 { + return parser.textWholdeText(self); + } + + // JS methods + // ---------- + + pub fn _splitText(self: *parser.Text, offset: u32) *parser.Text { + return parser.textSplitText(self, offset); + } }; + +// Tests +// ----- + +pub fn testExecFn( + _: std.mem.Allocator, + js_env: *jsruntime.Env, + comptime _: []jsruntime.API, +) !void { + var get_whole_text = [_]Case{ + .{ .src = "let text = document.getElementById('link').firstChild", .ex = "undefined" }, + .{ .src = "text.wholeText === 'OK'", .ex = "true" }, + }; + try checkCases(js_env, &get_whole_text); + + var split_text = [_]Case{ + .{ .src = "text.data = 'OK modified'", .ex = "OK modified" }, + .{ .src = "let split = text.splitText('OK'.length)", .ex = "undefined" }, + .{ .src = "split.data === ' modified'", .ex = "true" }, + .{ .src = "text.data === 'OK'", .ex = "true" }, + }; + try checkCases(js_env, &split_text); +} diff --git a/src/netsurf.zig b/src/netsurf.zig index 866346b2..42d61fed 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -503,6 +503,22 @@ pub fn characterDataSubstringData(cdata: *CharacterData, offset: u32, count: u32 // Text pub const Text = c.dom_text; +fn textVtable(text: *Text) c.dom_text_vtable { + return getVtable(c.dom_text_vtable, Text, text); +} + +pub fn textWholdeText(text: *Text) []const u8 { + var s: ?*String = undefined; + _ = textVtable(text).dom_text_get_whole_text.?(text, &s); + return stringToData(s.?); +} + +pub fn textSplitText(text: *Text, offset: u32) *Text { + var res: ?*Text = undefined; + _ = textVtable(text).dom_text_split_text.?(text, offset, &res); + return res.?; +} + // Comment pub const Comment = c.dom_comment; diff --git a/src/run_tests.zig b/src/run_tests.zig index bb036659..9a34bd7f 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -9,6 +9,7 @@ const DOM = @import("dom.zig"); const docTestExecFn = @import("html/document.zig").testExecFn; const nodeTestExecFn = @import("dom/node.zig").testExecFn; const characterDataTestExecFn = @import("dom/character_data.zig").testExecFn; +const textTestExecFn = @import("dom/text.zig").testExecFn; var doc: *parser.DocumentHTML = undefined; @@ -43,6 +44,7 @@ fn testsAllExecFn( docTestExecFn, nodeTestExecFn, characterDataTestExecFn, + textTestExecFn, }; inline for (testFns) |testFn| {