Skip to content

Commit

Permalink
Fix replaceEntities
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 24, 2024
1 parent 054f052 commit e08c019
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,13 @@ func replaceEntities(b []byte, i int, entitiesMap map[string][]byte, revEntities
}
} else {
for ; j < len(b) && j-i-1 <= MaxEntityLength && b[j] != ';'; j++ {
if !(b[j] >= '0' && b[j] <= '9' || b[j] >= 'a' && b[j] <= 'z' || b[j] >= 'A' && b[j] <= 'Z') {
// invalid character reference character
break
}
}
if j <= i+1 || len(b) <= j {
return b, j - 1
if len(b) <= j || j == i+1 || b[j] != ';' {
return b, i
}

var ok bool
Expand Down Expand Up @@ -399,7 +403,7 @@ func ReplaceMultipleWhitespaceAndEntities(b []byte, entitiesMap map[string][]byt
if j == 0 {
return b
} else if j == 1 { // only if starts with whitespace
b[k-1] = b[0]
b[k-1] = b[0] // move newline to end of whitespace
return b[k-1:]
} else if k < len(b) {
j += copy(b[j:], b[k:])
Expand Down
4 changes: 4 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func TestReplaceMultipleWhitespaceAndEntities(t *testing.T) {
entity string
expected string
}{
{" ", " "},
{" & a ", " & a "},
{" &acute a ", " &acute a "},
{"\n Lorem & ipsum\n dolor sid amet\n", "\nLorem & ipsum\ndolor sid amet\n"},
{" &varphi; &#34; \n ", " &phiv; \"\n"},
}
for _, tt := range entityTests {
Expand Down

0 comments on commit e08c019

Please sign in to comment.