gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

Normalize tg://user?id=<username> hrefs so _parseMessageText can resolve them

Open zeynalnia opened this issue 7 months ago • 0 comments

Summary

Closes #831.

_parseMessageText (in gramjs/client/messageParse.ts) already converts MessageEntityTextUrl entities into mentions when the url field is either:

  • tg://user?id=<digits> (positive numeric user id), or
  • @<username> / +<phone> (username or phone-number reference).

So numeric ids are already handled end-to-end without changes to the HTML parser. What was missing: when HTMLParser.parse encountered <a href="tg://user?id=<username>">…</a>, the resulting TextUrl.url did not match the _parseMessageText regex and the mention was silently dropped.

This change normalizes only that case. When the id portion of tg://user?id= is a valid Telegram username (5-32 chars of [A-Za-z0-9_], at least one letter or number, optionally prefixed with @), the HTML parser rewrites the entity URL to @<username> so _parseMessageText can resolve it. Everything else — numeric ids, negative ids, malformed values, empty values — is left untouched and passed through with the original tg://user?id=… URL.

A short comment in html.ts explains why the numeric branch is intentionally absent.

Test plan

  • [x] npx tsc --noEmit passes
  • [x] All existing __tests__/extensions/HTML.spec.ts tests still pass
  • [x] New tests cover: bare username (alice), @-prefixed username (@alice), extra query params after the username, validation of length / character / @ rules, all-digits ids passed through, negative ids passed through, empty id passed through, and a <strong> nested inside the username href

zeynalnia avatar Apr 19 '26 22:04 zeynalnia