Merge pull request #175 from extreme-mechagram/fix/botapi-html-optional-semicolon

fix(botapi): decode HTML character references without semicolons
This commit is contained in:
yialdd
2026-11-08 16:31:06 +08:00
committed by GitHub
Unverified
2 changed files with 94 additions and 26 deletions
+44 -26
View File
@@ -497,34 +497,52 @@ func decodeBotAPIHTMLString(input string) (string, error) {
}
func decodeBotAPIHTMLEntity(input string, start int) (string, int, error) {
endRelative := strings.IndexByte(input[start:], ';')
if endRelative < 0 {
return "&", start + 1, nil
// TDLib scans the reference's name or digits first; a trailing semicolon
// is optional. Do not consume the next text character or HTML tag.
end := start + 1
var decoded string
if end < len(input) && input[end] == '#' {
end++
base := 10
if end < len(input) && (input[end] == 'x' || input[end] == 'X') {
base = 16
end++
}
digitsStart := end
for end < len(input) {
c := input[end]
if c >= '0' && c <= '9' || base == 16 && (c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') {
end++
continue
}
break
}
value, err := strconv.ParseInt(input[digitsStart:end], base, 32)
if err != nil || value <= 0 || value > utf8.MaxRune || value >= 0xd800 && value <= 0xdfff {
return "&", start + 1, nil
}
decoded = string(rune(value))
} else {
for end < len(input) && (input[end] >= 'a' && input[end] <= 'z' || input[end] >= 'A' && input[end] <= 'Z') {
end++
}
switch input[start+1 : end] {
case "lt":
decoded = "<"
case "gt":
decoded = ">"
case "amp":
decoded = "&"
case "quot":
decoded = "\""
default:
return "&", start + 1, nil
}
}
end := start + endRelative
name := input[start+1 : end]
switch name {
case "lt":
return "<", end + 1, nil
case "gt":
return ">", end + 1, nil
case "amp":
return "&", end + 1, nil
case "quot":
return "\"", end + 1, nil
if end < len(input) && input[end] == ';' {
end++
}
if !strings.HasPrefix(name, "#") {
return "&", start + 1, nil
}
base, digits := 10, name[1:]
if strings.HasPrefix(digits, "x") || strings.HasPrefix(digits, "X") {
base, digits = 16, digits[1:]
}
value, err := strconv.ParseInt(digits, base, 32)
if err != nil || value <= 0 || value > utf8.MaxRune || value >= 0xd800 && value <= 0xdfff {
return "&", start + 1, nil
}
return string(rune(value)), end + 1, nil
return decoded, end, nil
}
func parseBotAPIMarkdown(input string) (string, []domain.MessageEntity, error) {
+50
View File
@@ -0,0 +1,50 @@
package botapi
import (
"reflect"
"testing"
"telesrv/internal/domain"
)
func TestHTMLCharacterReferencesOptionalSemicolon(t *testing.T) {
for _, tc := range []struct{ input, want string }{
{`&lt &gt &amp &quot`, `< > & "`},
{`&lt; &gt; &amp; &quot;`, `< > & "`},
{`&amp&amp;`, `&&`},
{`&#65 &#x41 &#X1F680`, "A A 🚀"},
{`&#65; &#x41; &#X1F680;`, "A A 🚀"},
{`&#65Z &#x41Z`, `AZ AZ`},
{`&amplifier &amp1`, `&amplifier &1`},
{`&unknown; &apos;`, `&unknown; &apos;`},
{`& &# &#x &#;`, `& &# &#x &#;`},
{`&#0; &#xD800; &#1114112;`, `&#0; &#xD800; &#1114112;`},
{`&#999999999999999999999999;`, `&#999999999999999999999999;`},
} {
t.Run(tc.input, func(t *testing.T) {
plain, _, err := parseBotAPIHTML(tc.input)
if err != nil || plain != tc.want {
t.Fatalf("HTML text = %q, %v; want %q", plain, err, tc.want)
}
attribute, err := decodeBotAPIHTMLString(tc.input)
if err != nil || attribute != tc.want {
t.Fatalf("HTML attribute = %q, %v; want %q", attribute, err, tc.want)
}
})
}
}
func TestHTMLCharacterReferencesPreserveEntities(t *testing.T) {
input := `&#x1F680<b>&#65</b> <a href="https://example.test/?a=1&#38b=2">link</a>`
plain, entities, err := botAPIFormattedText(input, "HTML", nil, 4096, true)
if err != nil {
t.Fatal(err)
}
want := []domain.MessageEntity{
{Type: domain.MessageEntityBold, Offset: 2, Length: 1},
{Type: domain.MessageEntityTextURL, Offset: 4, Length: 4, URL: "https://example.test/?a=1&b=2"},
}
if plain != "🚀A link" || !reflect.DeepEqual(entities, want) {
t.Fatalf("formatted text = %q, %#v; want %q, %#v", plain, entities, "🚀A link", want)
}
}