Fix HTML unparsing when there's no entities

This commit is contained in:
Dan 2022-12-06 21:33:46 +01:00
parent 86515bb9d1
commit 217bcb1dfb
2 changed files with 14 additions and 5 deletions

View File

@ -233,10 +233,11 @@ class HTML:
while i < len(entities): while i < len(entities):
i += recursive(i) i += recursive(i)
last_offset = entities_offsets[-1][1] if entities_offsets:
# no need to sort, but still add entities starting from the end last_offset = entities_offsets[-1][1]
for entity, offset in reversed(entities_offsets): # no need to sort, but still add entities starting from the end
text = text[:offset] + entity + html.escape(text[offset:last_offset]) + text[last_offset:] for entity, offset in reversed(entities_offsets):
last_offset = offset text = text[:offset] + entity + html.escape(text[offset:last_offset]) + text[last_offset:]
last_offset = offset
return utils.remove_surrogates(text) return utils.remove_surrogates(text)

View File

@ -137,3 +137,11 @@ def test_html_unparse_escaped_nested():
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=8, length=16)]) pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=8, length=16)])
assert HTML.unparse(text=text, entities=entities) == expected 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