From 2fb5215f5f59d49f32130088d5900a3035be8d73 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 3 Jun 2018 13:48:43 +0200 Subject: [PATCH] Fix parsers misbehaving with None text --- telethon/extensions/html.py | 5 ++++- telethon/extensions/markdown.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/telethon/extensions/html.py b/telethon/extensions/html.py index bcbd13cc..d4233473 100644 --- a/telethon/extensions/html.py +++ b/telethon/extensions/html.py @@ -121,6 +121,9 @@ def parse(html): :param message: the message with HTML to be parsed. :return: a tuple consisting of (clean message, [message entities]). """ + if not html: + return html, [] + parser = HTMLToTelegramParser() parser.feed(_add_surrogate(html)) return _del_surrogate(parser.text), parser.entities @@ -135,7 +138,7 @@ def unparse(text, entities): :param entities: the MessageEntity's applied to the text. :return: a HTML representation of the combination of both inputs. """ - if not entities: + if not text or not entities: return text text = _add_surrogate(text) diff --git a/telethon/extensions/markdown.py b/telethon/extensions/markdown.py index a7af1fa8..cb54c99e 100644 --- a/telethon/extensions/markdown.py +++ b/telethon/extensions/markdown.py @@ -36,6 +36,9 @@ def parse(message, delimiters=None, url_re=None): :param url_re: the URL bytes regex to be used. Must have two groups. :return: a tuple consisting of (clean message, [message entities]). """ + if not message: + return message, [] + if url_re is None: url_re = DEFAULT_URL_RE elif isinstance(url_re, str): @@ -137,7 +140,7 @@ def unparse(text, entities, delimiters=None, url_fmt=None): :param entities: the MessageEntity's applied to the text. :return: a markdown-like text representing the combination of both inputs. """ - if not entities: + if not text or not entities: return text if not delimiters: