mirror of
https://github.com/iamxvbaba/gramsrv.git
synced 2026-11-19 05:16:55 +03:00
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:
@@ -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) {
|
||||
|
||||
@@ -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; '`},
|
||||
{`& &# &#x &#;`, `& &# &#x &#;`},
|
||||
{`� � �`, `� � �`},
|
||||
{`�`, `�`},
|
||||
} {
|
||||
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 := `🚀<b>A</b> <a href="https://example.test/?a=1&b=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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user