Fix Escaping in Nested Entities in Message Properties (#2312)

* fix: overly escape and offset error markdown v2 symbols when nested

Signed-off-by: Eana Hufwe <ilove@1a23.com>

* fix: nested entity escape for HTML parsers and tests

Signed-off-by: Eana Hufwe <ilove@1a23.com>
This commit is contained in:
Eana Hufwe 2021-01-12 10:33:26 -08:00 committed by GitHub
parent 0c9915243d
commit be54cf4ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 17 deletions

View file

@ -2171,11 +2171,12 @@ class Message(TelegramObject):
} }
parsed_entities.extend(list(nested_entities.keys())) parsed_entities.extend(list(nested_entities.keys()))
orig_text = text
text = escape(text) text = escape(text)
if nested_entities: if nested_entities:
text = Message._parse_html( text = Message._parse_html(
text, nested_entities, urled=urled, offset=entity.offset orig_text, nested_entities, urled=urled, offset=entity.offset
) )
if entity.type == MessageEntity.TEXT_LINK: if entity.type == MessageEntity.TEXT_LINK:
@ -2341,7 +2342,11 @@ class Message(TelegramObject):
) )
text = Message._parse_markdown( text = Message._parse_markdown(
text, nested_entities, urled=urled, offset=entity.offset, version=version orig_text,
nested_entities,
urled=urled,
offset=entity.offset,
version=version,
) )
if entity.type == MessageEntity.TEXT_LINK: if entity.type == MessageEntity.TEXT_LINK:

View file

@ -259,14 +259,14 @@ class TestMessage:
}, },
{'length': 5, 'offset': 57, 'type': 'pre'}, {'length': 5, 'offset': 57, 'type': 'pre'},
{'length': 17, 'offset': 64, 'type': 'url'}, {'length': 17, 'offset': 64, 'type': 'url'},
{'length': 36, 'offset': 86, 'type': 'italic'}, {'length': 41, 'offset': 86, 'type': 'italic'},
{'length': 24, 'offset': 91, 'type': 'bold'}, {'length': 29, 'offset': 91, 'type': 'bold'},
{'length': 4, 'offset': 101, 'type': 'strikethrough'}, {'length': 9, 'offset': 101, 'type': 'strikethrough'},
{'length': 10, 'offset': 124, 'type': 'pre', 'language': 'python'}, {'length': 10, 'offset': 129, 'type': 'pre', 'language': 'python'},
] ]
test_text_v2 = ( test_text_v2 = (
r'Test for <bold, ita_lic, \`code, links, text-mention and `\pre. ' r'Test for <bold, ita_lic, \`code, links, text-mention and `\pre. '
'http://google.com and bold nested in strk nested in italic. Python pre.' 'http://google.com and bold nested in strk>trgh nested in italic. Python pre.'
) )
test_message = Message( test_message = Message(
message_id=1, message_id=1,
@ -357,7 +357,7 @@ class TestMessage:
r'<a href="http://github.com/abc\)def">links</a>, ' r'<a href="http://github.com/abc\)def">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and ' '<a href="tg://user?id=123456789">text-mention</a> and '
r'<pre>`\pre</pre>. http://google.com ' r'<pre>`\pre</pre>. http://google.com '
'and <i>bold <b>nested in <s>strk</s> nested in</b> italic</i>. ' 'and <i>bold <b>nested in <s>strk&gt;trgh</s> nested in</b> italic</i>. '
'<pre><code class="python">Python pre</code></pre>.' '<pre><code class="python">Python pre</code></pre>.'
) )
text_html = self.test_message_v2.text_html text_html = self.test_message_v2.text_html
@ -375,7 +375,7 @@ class TestMessage:
r'<a href="http://github.com/abc\)def">links</a>, ' r'<a href="http://github.com/abc\)def">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and ' '<a href="tg://user?id=123456789">text-mention</a> and '
r'<pre>`\pre</pre>. <a href="http://google.com">http://google.com</a> ' r'<pre>`\pre</pre>. <a href="http://google.com">http://google.com</a> '
'and <i>bold <b>nested in <s>strk</s> nested in</b> italic</i>. ' 'and <i>bold <b>nested in <s>strk&gt;trgh</s> nested in</b> italic</i>. '
'<pre><code class="python">Python pre</code></pre>.' '<pre><code class="python">Python pre</code></pre>.'
) )
text_html = self.test_message_v2.text_html_urled text_html = self.test_message_v2.text_html_urled
@ -396,7 +396,7 @@ class TestMessage:
r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, ' r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, '
'[links](http://github.com/abc\\\\\\)def), ' '[links](http://github.com/abc\\\\\\)def), '
'[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. ' '[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. '
r'http://google\.com and _bold *nested in ~strk~ nested in* italic_\. ' r'http://google\.com and _bold *nested in ~strk\>trgh~ nested in* italic_\. '
'```python\nPython pre```\\.' '```python\nPython pre```\\.'
) )
text_markdown = self.test_message_v2.text_markdown_v2 text_markdown = self.test_message_v2.text_markdown_v2
@ -442,7 +442,7 @@ class TestMessage:
r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, ' r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, '
'[links](http://github.com/abc\\\\\\)def), ' '[links](http://github.com/abc\\\\\\)def), '
'[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. ' '[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. '
r'[http://google\.com](http://google.com) and _bold *nested in ~strk~ ' r'[http://google\.com](http://google.com) and _bold *nested in ~strk\>trgh~ '
'nested in* italic_\\. ```python\nPython pre```\\.' 'nested in* italic_\\. ```python\nPython pre```\\.'
) )
text_markdown = self.test_message_v2.text_markdown_v2_urled text_markdown = self.test_message_v2.text_markdown_v2_urled
@ -473,7 +473,7 @@ class TestMessage:
r'<a href="http://github.com/abc\)def">links</a>, ' r'<a href="http://github.com/abc\)def">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and ' '<a href="tg://user?id=123456789">text-mention</a> and '
r'<pre>`\pre</pre>. http://google.com ' r'<pre>`\pre</pre>. http://google.com '
'and <i>bold <b>nested in <s>strk</s> nested in</b> italic</i>. ' 'and <i>bold <b>nested in <s>strk&gt;trgh</s> nested in</b> italic</i>. '
'<pre><code class="python">Python pre</code></pre>.' '<pre><code class="python">Python pre</code></pre>.'
) )
caption_html = self.test_message_v2.caption_html caption_html = self.test_message_v2.caption_html
@ -491,7 +491,7 @@ class TestMessage:
r'<a href="http://github.com/abc\)def">links</a>, ' r'<a href="http://github.com/abc\)def">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and ' '<a href="tg://user?id=123456789">text-mention</a> and '
r'<pre>`\pre</pre>. <a href="http://google.com">http://google.com</a> ' r'<pre>`\pre</pre>. <a href="http://google.com">http://google.com</a> '
'and <i>bold <b>nested in <s>strk</s> nested in</b> italic</i>. ' 'and <i>bold <b>nested in <s>strk&gt;trgh</s> nested in</b> italic</i>. '
'<pre><code class="python">Python pre</code></pre>.' '<pre><code class="python">Python pre</code></pre>.'
) )
caption_html = self.test_message_v2.caption_html_urled caption_html = self.test_message_v2.caption_html_urled
@ -512,7 +512,7 @@ class TestMessage:
r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, ' r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, '
'[links](http://github.com/abc\\\\\\)def), ' '[links](http://github.com/abc\\\\\\)def), '
'[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. ' '[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. '
r'http://google\.com and _bold *nested in ~strk~ nested in* italic_\. ' r'http://google\.com and _bold *nested in ~strk\>trgh~ nested in* italic_\. '
'```python\nPython pre```\\.' '```python\nPython pre```\\.'
) )
caption_markdown = self.test_message_v2.caption_markdown_v2 caption_markdown = self.test_message_v2.caption_markdown_v2
@ -539,7 +539,7 @@ class TestMessage:
r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, ' r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, '
'[links](http://github.com/abc\\\\\\)def), ' '[links](http://github.com/abc\\\\\\)def), '
'[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. ' '[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. '
r'[http://google\.com](http://google.com) and _bold *nested in ~strk~ ' r'[http://google\.com](http://google.com) and _bold *nested in ~strk\>trgh~ '
'nested in* italic_\\. ```python\nPython pre```\\.' 'nested in* italic_\\. ```python\nPython pre```\\.'
) )
caption_markdown = self.test_message_v2.caption_markdown_v2_urled caption_markdown = self.test_message_v2.caption_markdown_v2_urled
@ -698,7 +698,7 @@ class TestMessage:
r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, ' r'__Test__ for <*bold*, _ita\_lic_, `\\\`code`, '
'[links](http://github.com/abc\\\\\\)def), ' '[links](http://github.com/abc\\\\\\)def), '
'[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. ' '[text\\-mention](tg://user?id=123456789) and ```\\`\\\\pre```\\. '
r'http://google\.com and _bold *nested in ~strk~ nested in* italic_\. ' r'http://google\.com and _bold *nested in ~strk\>trgh~ nested in* italic_\. '
'```python\nPython pre```\\.' '```python\nPython pre```\\.'
) )
@ -738,7 +738,7 @@ class TestMessage:
r'<a href="http://github.com/abc\)def">links</a>, ' r'<a href="http://github.com/abc\)def">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and ' '<a href="tg://user?id=123456789">text-mention</a> and '
r'<pre>`\pre</pre>. http://google.com ' r'<pre>`\pre</pre>. http://google.com '
'and <i>bold <b>nested in <s>strk</s> nested in</b> italic</i>. ' 'and <i>bold <b>nested in <s>strk&gt;trgh</s> nested in</b> italic</i>. '
'<pre><code class="python">Python pre</code></pre>.' '<pre><code class="python">Python pre</code></pre>.'
) )