mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
Merge pull request #54 from Browsercore/text
text: properties and methods
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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| {
|
||||
|
||||
Reference in New Issue
Block a user