Fix TextLength to count characters instead of bytes

Use utf8.RuneCountInString so multi-byte characters like é count as one
character. Add a unicode test case to cover this.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-09 13:39:39 +04:00
parent 7cd4606278
commit 050073ab3c
2 changed files with 15 additions and 4 deletions

View File

@@ -487,6 +487,17 @@ func TestTextLength(t *testing.T) {
},
)
t.Run(
"multi-byte unicode characters",
func(t *testing.T) {
t.Parallel()
raw := `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"café résumé"}]}]}`
doc, err := Parse(raw)
require.NoError(t, err)
assert.Equal(t, 11, doc.TextLength())
},
)
t.Run(
"testdata document",
func(t *testing.T) {