1
0
mirror of https://github.com/golang/go synced 2024-11-21 11:24:39 -07:00

html: handle single digit decimal numeric entities without semicolon

Fix handling of "&#9" and add tests for other single-digit cases.

Fixes #66058
Updates #21563
This commit is contained in:
Alexander Yastrebov 2024-03-06 18:47:06 +01:00
parent f0d1195e13
commit 011e8f37f6
2 changed files with 15 additions and 1 deletions

View File

@ -104,7 +104,8 @@ func unescapeEntity(b []byte, dst, src int) (dst1, src1 int) {
break
}
if i <= 3 { // No characters matched.
// We need to have at least "&#." or "&#x.".
if (!hex && i < 3) || (hex && i < 4) {
b[dst] = b[src]
return dst + 1, src + 1
}

View File

@ -49,12 +49,24 @@ var unescapeTests = []unescapeTest{
"Delta = &#916; ",
"Delta = Δ ",
},
// Handle single-digit decimal numeric entities.
{
"singleDigitDecimalEntity",
"Tab = &#9; = &#9 ",
"Tab = \t = \t ",
},
// Handle hexadecimal numeric entities.
{
"hexadecimalEntity",
"Lambda = &#x3bb; = &#X3Bb ",
"Lambda = λ = λ ",
},
// Handle single-digit hexadecimal numeric entities.
{
"singleDigitHexadecimalEntity",
"Tab = &#x9; = &#x9 ",
"Tab = \t = \t ",
},
// Handle numeric early termination.
{
"numericEnds",
@ -109,6 +121,7 @@ func TestUnescapeEscape(t *testing.T) {
`&quot;&lt;&amp;&gt;&quot;`,
`3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
`The special characters are: <, >, &, ' and "`,
`&#9; &#9 &#x9; &#x9`,
}
for _, s := range ss {
if got := UnescapeString(EscapeString(s)); got != s {