use CData.render for innerText

This commit is contained in:
Pierre Tachoire
2025-12-08 09:55:31 +01:00
parent aac35ae868
commit 5284d75cb7
3 changed files with 6 additions and 3 deletions

View File

@@ -172,5 +172,5 @@
// TODO innerText is not rendered correctly for now.
//testing.expectEqual("This is a\ntext", d2.innerText);
testing.expectEqual(" This is a \n text", d2.innerText);
testing.expectEqual(" This is a \n text ", d2.innerText);
</script>

View File

@@ -102,7 +102,8 @@ pub fn render(self: *const CData, writer: *std.io.Writer, opts: RenderOpts) !voi
// Write the reminder chunk.
if (is_w) {
// Last chunk is whitespaces.
if (opts.trim_right == false) {
// If the string contains only whitespaces, don't write it.
if (start > 0 and opts.trim_right == false) {
try writer.writeByte(' ');
}
} else {
@@ -285,6 +286,8 @@ test "WebApi: CData.render" {
};
const test_cases = [_]TestCase{
.{ .value = " ", .expected = "" },
.{ .value = " ", .expected = "", .opts = .{ .trim_left = false, .trim_right = false } },
.{ .value = "foo bar", .expected = "foo bar" },
.{ .value = "foo bar", .expected = "foo bar" },
.{ .value = " foo bar", .expected = "foo bar" },

View File

@@ -239,7 +239,7 @@ pub fn getInnerText(self: *Element, writer: *std.Io.Writer) !void {
},
.cdata => |c| switch (c._type) {
.comment => continue,
.text => try c.render(writer, .{}),
.text => try c.render(writer, .{ .trim_right = false, .trim_left = false }),
},
.document => {},
.document_type => {},