Skip to content

Commit

Permalink
Fix HTML unparsing when there's no entities
Browse files Browse the repository at this point in the history
  • Loading branch information
delivrance committed Dec 6, 2022
1 parent 86515bb commit 217bcb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pyrogram/parser/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ def recursive(entity_i: int) -> int:
while i < len(entities):
i += recursive(i)

last_offset = entities_offsets[-1][1]
# no need to sort, but still add entities starting from the end
for entity, offset in reversed(entities_offsets):
text = text[:offset] + entity + html.escape(text[offset:last_offset]) + text[last_offset:]
last_offset = offset
if entities_offsets:
last_offset = entities_offsets[-1][1]
# no need to sort, but still add entities starting from the end
for entity, offset in reversed(entities_offsets):
text = text[:offset] + entity + html.escape(text[offset:last_offset]) + text[last_offset:]
last_offset = offset

return utils.remove_surrogates(text)
8 changes: 8 additions & 0 deletions tests/parser/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ def test_html_unparse_escaped_nested():
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=8, length=16)])

assert HTML.unparse(text=text, entities=entities) == expected


def test_html_unparse_no_entities():
expected = "text"
text = "text"
entities = []

assert HTML.unparse(text=text, entities=entities) == expected

0 comments on commit 217bcb1

Please sign in to comment.