text: add splitText method

Signed-off-by: Francis Bouvier <francis.bouvier@gmail.com>
This commit is contained in:
Francis Bouvier
2023-10-09 16:27:40 +02:00
parent de8f586223
commit b05e08e6a8
2 changed files with 21 additions and 0 deletions

View File

@@ -21,6 +21,13 @@ pub const Text = struct {
pub fn get_wholeText(self: *parser.Text) []const u8 { pub fn get_wholeText(self: *parser.Text) []const u8 {
return parser.textWholdeText(self); return parser.textWholdeText(self);
} }
// JS methods
// ----------
pub fn _splitText(self: *parser.Text, offset: u32) *parser.Text {
return parser.textSplitText(self, offset);
}
}; };
// Tests // Tests
@@ -36,4 +43,12 @@ pub fn testExecFn(
.{ .src = "text.wholeText === 'OK'", .ex = "true" }, .{ .src = "text.wholeText === 'OK'", .ex = "true" },
}; };
try checkCases(js_env, &get_whole_text); 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);
} }

View File

@@ -513,6 +513,12 @@ pub fn textWholdeText(text: *Text) []const u8 {
return stringToData(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 // Comment
pub const Comment = c.dom_comment; pub const Comment = c.dom_comment;