Repository navigation
fix(formatting): make date_time entities work and escape attribute values - #2649
Conversation
…tract_arguments
smart_split compared with `<`, so text whose length is an exact multiple of
chars_per_string went through the split path one last time on the remainder and
appended ''. smart_split("abc", 3) returned ["abc", ""], and sending that empty
part to Telegram fails with "message text is empty".
extract_arguments compiled its regexp and matched before looking at None, so a
non-text message raised TypeError on message.text. is_command and
extract_command already tolerate None, so this now matches them.
…lues
apply_html_entities formatted the date_time template with unix=, but the
template declares {unix_time}, so any message carrying a date_time entity
raised KeyError: 'unix_time' from .html_text. The entity was added in eternnoir#2570
and has never worked.
The text_link URL and the pre language were also interpolated raw. hlink and
hpre already escape both, so a URL or language containing a quote produced
broken HTML and could add attributes.
There was a problem hiding this comment.
🟡 Changes recommended
The formatting keyword rename breaks existing custom templates using {unix}, causing KeyError unless both unix and unix_time are supported.
1 open issue
What changed in this PR
This PR fixes HTML entity rendering in the shared formatting layer, including the date_time failure introduced with its support in #2570. It also addresses two utility edge cases.
Changes:
- Fix the
date_timetemplate keyword and escape link URLs and code-language attributes. - Handle
Nonein argument extraction and prevent trailing empty chunks insmart_split. - Add focused regression tests for formatting and utility behavior.
| File | Description |
|---|---|
| tests/test_util.py | Adds splitting and argument-extraction regression tests. |
| tests/test_formatting.py | Tests date-time rendering, attribute escaping, and content escaping. |
| telebot/util.py | Handles missing text and exact-length chunk boundaries. |
| telebot/formatting.py | Fixes date-time formatting and escapes HTML attribute values. |
Review effort: Balanced
| :rtype: :obj:`str` or :obj:`None` | ||
| """ | ||
| regexp = re.compile(r"/\w*(@\w*)*\s*([\s\S]*)", re.IGNORECASE) | ||
| if text is None: return None |
There was a problem hiding this comment.
I propose to move it before "re.compile" because compile bacame useless.
Formatting the date_time template with unix_time= broke custom submissions that
were written against the old {unix} placeholder, raising KeyError: 'unix'. Pass
both names so existing templates keep working.
extract_arguments also compiled its regexp before the None check, so the
compile was wasted on the early return. Move it below.
|
Good catch on the {unix} regression. Fixed in 5988ec8: the template is now formatted with both Also moved the Full suite: 319 passed, 72 skipped. |

Description
Three bugs in
apply_html_entities, all in the sameformat_entitybranch.date_timeraises. The template declares{unix_time}, but the call formats it withunix=, so the lookup fails:This reaches users through
Message.html_text, so any message with adate_timeentity (added in #2570) crashes. It looks like the entity has never worked.text_linkURL is interpolated raw. A URL containing a quote produces broken HTML:prelanguage is interpolated raw. Same problem, and it can add attributes:hlinkandhprealready pass both throughescape_html, so this just makes the entity path consistent with them.Describe your tests
Added four tests to
tests/test_formatting.py, reusing theAttributeCollectorparser helper that is already there. They assert thedate_timeoutput exactly, and feed the link andpreoutput through an HTML parser to prove no extra attributes appear.The existing
test_apply_html_entitiesis an integration test that sends real messages, so it never runs without a bot token — these are the first unit tests for this function.Full suite:
317 passed, 73 skipped.Python version: 3.12.3
OS: macOS 27.2
Checklist: