Skip to content

Commit

Permalink
msgconv/matrixfmt: fix trimming space from whitespace-only string
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 13, 2024
1 parent 4ede5dc commit adee821
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 7 additions & 0 deletions pkg/msgconv/matrixfmt/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func TestParse_HTML(t *testing.T) {
Length: 5,
Value: signalfmt.StyleBold,
}}},
{name: "UnnecessaryWhitespace", in: "<strong> Hello </strong>, World!", out: "Hello, World!", ent: signalfmt.BodyRangeList{{
Start: 0,
Length: 5,
Value: signalfmt.StyleBold,
}}},
{name: "UnnecessaryWhitespaceParagraph", in: "<p> Hello </p>", out: "Hello"},
{name: "EmptyParagraph", in: "<p>Hello</p><p> </p>", out: "Hello"},
{
name: "MultiBasic",
in: "<strong><em>Hell</em>o</strong>, <del>Wo<span data-mx-spoiler>rld</span></del><code>!</code>",
Expand Down
14 changes: 9 additions & 5 deletions pkg/msgconv/matrixfmt/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,26 @@ func (es *EntityString) TrimSpace() *EntityString {
return nil
}
DebugLog("TRIMSPACE %q %+v\n", es.String, es.Entities)
var cutEnd, cutStart int
for cutStart = 0; cutStart < len(es.String); cutStart++ {
cutStart := 0
for ; cutStart < len(es.String); cutStart++ {
switch es.String[cutStart] {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
continue
}
break
}
for cutEnd = len(es.String) - 1; cutEnd >= 0; cutEnd-- {
switch es.String[cutEnd] {
cutEnd := len(es.String)
for ; cutEnd > cutStart; cutEnd-- {
switch es.String[cutEnd-1] {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
continue
}
break
}
cutEnd++
if cutEnd == cutStart {
DebugLog(" -> EMPTY\n")
return NewEntityString("")
}
if cutStart == 0 && cutEnd == len(es.String) {
DebugLog(" -> NOOP\n")
return es
Expand Down

0 comments on commit adee821

Please sign in to comment.