feat(html-parser): allow passing Longs to interpolate

This commit is contained in:
alina 🌸 2024-05-29 23:37:17 +03:00
parent 7a6d984977
commit 9ff8f9d33f
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 14 additions and 3 deletions

View file

@ -501,6 +501,14 @@ describe('HtmlMessageEntityParser', () => {
) )
}) })
it('should handle numbers and Longs', () => {
test(
htm`a number ${123123} and a long ${Long.fromNumber(456456)}`,
[],
'a number 123123 and a long 456456',
)
})
it('should handle interpolation into attrs', () => { it('should handle interpolation into attrs', () => {
test( test(
htm`<a href="${'https'}://example.com/&quot;${'foo'}/bar/${'baz'}?foo=bar&baz=${'egg'}">link</a>`, htm`<a href="${'https'}://example.com/&quot;${'foo'}/bar/${'baz'}?foo=bar&baz=${'egg'}">link</a>`,
@ -526,7 +534,7 @@ describe('HtmlMessageEntityParser', () => {
'user', 'user',
) )
test( test(
htm`<tg-emoji id="${'123123123123'}">🚀</tg-emoji>`, htm`<tg-emoji id="${Long.fromString('123123123123')}">🚀</tg-emoji>`,
[ [
createEntity('messageEntityCustomEmoji', 0, 2, { createEntity('messageEntityCustomEmoji', 0, 2, {
documentId: Long.fromString('123123123123'), documentId: Long.fromString('123123123123'),

View file

@ -20,7 +20,7 @@ function escape(str: string, quote = false): string {
function parse( function parse(
strings: TemplateStringsArray | string, strings: TemplateStringsArray | string,
...sub: (InputText | MessageEntity | boolean | number | undefined | null)[] ...sub: (InputText | MessageEntity | Long | boolean | number | undefined | null)[]
): TextWithEntities { ): TextWithEntities {
const stacks: Record<string, tl.Mutable<tl.TypeMessageEntity>[]> = {} const stacks: Record<string, tl.Mutable<tl.TypeMessageEntity>[]> = {}
const entities: tl.TypeMessageEntity[] = [] const entities: tl.TypeMessageEntity[] = []
@ -250,6 +250,7 @@ function parse(
if (typeof it === 'string') text = it if (typeof it === 'string') text = it
else if (typeof it === 'number') text = it.toString() else if (typeof it === 'number') text = it.toString()
else if (Long.isLong(it)) text = it.toString(10)
else { else {
// obviously we can't have entities inside attributes, so just use the text // obviously we can't have entities inside attributes, so just use the text
text = it.text text = it.text
@ -261,6 +262,8 @@ function parse(
if (typeof it === 'string' || typeof it === 'number') { if (typeof it === 'string' || typeof it === 'number') {
pendingText += it pendingText += it
} else if (Long.isLong(it)) {
pendingText += it.toString(10)
} else { } else {
// TextWithEntities or MessageEntity // TextWithEntities or MessageEntity
const text = it.text const text = it.text
@ -451,7 +454,7 @@ export const html: {
*/ */
( (
strings: TemplateStringsArray, strings: TemplateStringsArray,
...sub: (InputText | MessageEntity | boolean | number | undefined | null)[] ...sub: (InputText | MessageEntity | boolean | Long | number | undefined | null)[]
): TextWithEntities ): TextWithEntities
/** /**
* A variant taking a plain JS string as input * A variant taking a plain JS string as input