From a011780964b51715de576cab2d8d9ef829e2266d Mon Sep 17 00:00:00 2001
From: extreme <226144099+extreme-mechagram@users.noreply.github.com>
Date: Sun, 8 Nov 2026 07:37:42 +0300
Subject: [PATCH] fix(botapi): decode HTML references without trailing
semicolons
---
internal/botapi/formatted_text.go | 70 ++++++++++++++++++-----------
internal/botapi/html_entity_test.go | 50 +++++++++++++++++++++
2 files changed, 94 insertions(+), 26 deletions(-)
create mode 100644 internal/botapi/html_entity_test.go
diff --git a/internal/botapi/formatted_text.go b/internal/botapi/formatted_text.go
index a030b8ac..dc6fefc0 100644
--- a/internal/botapi/formatted_text.go
+++ b/internal/botapi/formatted_text.go
@@ -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) {
diff --git a/internal/botapi/html_entity_test.go b/internal/botapi/html_entity_test.go
new file mode 100644
index 00000000..b62efa3b
--- /dev/null
+++ b/internal/botapi/html_entity_test.go
@@ -0,0 +1,50 @@
+package botapi
+
+import (
+ "reflect"
+ "testing"
+
+ "telesrv/internal/domain"
+)
+
+func TestHTMLCharacterReferencesOptionalSemicolon(t *testing.T) {
+ for _, tc := range []struct{ input, want string }{
+ {`< > & "`, `< > & "`},
+ {`< > & "`, `< > & "`},
+ {`&&`, `&&`},
+ {`A A 🚀`, "A A 🚀"},
+ {`A A 🚀`, "A A 🚀"},
+ {`AZ AZ`, `AZ AZ`},
+ {`&lifier &1`, `&lifier &1`},
+ {`&unknown; '`, `&unknown; '`},
+ {`& `, `& `},
+ {` `, ` `},
+ {``, ``},
+ } {
+ 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 := `🚀A link`
+ 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)
+ }
+}