Skip to content

Commit

Permalink
Fixed nested hashtag, cashtag and email entnties parsing (#1263)
Browse files Browse the repository at this point in the history
* Fixed nested hashtag, cashtag and email message entities not being parsed correctly when these entities are inside another entity.

* Tests coverage
  • Loading branch information
JrooTJunior committed Aug 13, 2023
1 parent 020db29 commit a800315
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/1259.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed nested hashtag, cashtag and email message entities not being parsed correctly when these entities are inside another entity.
7 changes: 6 additions & 1 deletion aiogram/utils/text_decorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ def apply_entity(self, entity: MessageEntity, text: str) -> str:
MessageEntityType.URL,
MessageEntityType.MENTION,
MessageEntityType.PHONE_NUMBER,
MessageEntityType.HASHTAG,
MessageEntityType.CASHTAG,
MessageEntityType.EMAIL,
}:
# This entities should not be changed
# These entities should not be changed
return text
if entity.type in {
MessageEntityType.BOLD,
Expand All @@ -71,6 +74,8 @@ def apply_entity(self, entity: MessageEntity, text: str) -> str:
if entity.type == MessageEntityType.CUSTOM_EMOJI:
return self.custom_emoji(value=text, custom_emoji_id=cast(str, entity.custom_emoji_id))

# This case is not possible because of `if` above, but if any new entity is added to
# API it will be here too
return self.quote(text)

def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_utils/test_text_decorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ def test_apply_single_entity(
):
assert decorator.apply_entity(entity, "test") == result

def test_unknown_apply_entity(self):
assert (
html_decoration.apply_entity(
MessageEntity(type="unknown", offset=0, length=5), "<test>"
)
== "&lt;test&gt;"
)

@pytest.mark.parametrize(
"decorator,before,after",
[
Expand Down Expand Up @@ -243,6 +251,33 @@ def test_quote(self, decorator: TextDecoration, before: str, after: str):
[MessageEntity(type="bold", offset=0, length=8, url=None, user=None)],
"<b>👋🏾 Hi!</b>",
],
[
html_decoration,
"#test",
[
MessageEntity(type="hashtag", offset=0, length=5),
MessageEntity(type="bold", offset=0, length=5),
],
"<b>#test</b>",
],
[
html_decoration,
"$TEST",
[
MessageEntity(type="cashtag", offset=0, length=5),
MessageEntity(type="bold", offset=0, length=5),
],
"<b>$TEST</b>",
],
[
html_decoration,
"test@example.com",
[
MessageEntity(type="email", offset=0, length=16),
MessageEntity(type="bold", offset=0, length=16),
],
"<b>test@example.com</b>",
],
],
)
def test_unparse(
Expand Down

0 comments on commit a800315

Please sign in to comment.