diff --git a/plugins/platforms/telegram/standalone.py b/plugins/platforms/telegram/standalone.py index dae7ba7a0f..76f34b54fd 100644 --- a/plugins/platforms/telegram/standalone.py +++ b/plugins/platforms/telegram/standalone.py @@ -265,8 +265,10 @@ _TELEGRAM_HTML_TAGS = ("b", "strong", "i", "em", "u", "ins", "s", "strike", "del "code", "pre", "blockquote", "tg-spoiler", "span", "tg-emoji") # Opening tag (optionally with attributes) or closing tag. The name must end at ``>`` or # whitespace, so ```` never matches ````; longest-first keeps the alternation readable. +# Quoted attribute values may hold ``>`` (``href="a>b"``): the tag ends at the first unquoted one. _TELEGRAM_HTML_TAG_ALT = "|".join(sorted(_TELEGRAM_HTML_TAGS, key=len, reverse=True)) -_TELEGRAM_HTML_TAG_RE = re.compile(rf"]*)?>", re.IGNORECASE) +_TELEGRAM_HTML_TAG_RE = re.compile( + rf""""']++|"[^"]*+"|'[^']*+')*+)?>""", re.IGNORECASE) def _telegram_album_paths(media_files, force_document, caption): diff --git a/tests/tools/test_telegram_send_message_html_whitelist.py b/tests/tools/test_telegram_send_message_html_whitelist.py index 7ca6925bcf..5f495594bf 100644 --- a/tests/tools/test_telegram_send_message_html_whitelist.py +++ b/tests/tools/test_telegram_send_message_html_whitelist.py @@ -107,3 +107,12 @@ def test_tag_shown_only_as_markdown_code_keeps_the_markdown_branch( assert (parse_mode, has_html) == ("MarkdownV2", False) assert formatted.startswith("*Note*") and "**" not in formatted + + +def test_a_quoted_gt_inside_an_attribute_does_not_break_the_tag( + monkeypatch: pytest.MonkeyPatch) -> None: + from plugins.platforms.telegram.standalone import _telegram_format + + _install_telegram_mock(monkeypatch, MagicMock()) + markup = 'link and bold' + assert _telegram_format(markup) == (markup, "HTML", True)