Skip to content

Commit 285cb37

Browse files
committed
Fix offset bug in Message._parse_*
1 parent 3e8b05e commit 285cb37

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

telegram/message.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ def parse_caption_entities(self, types=None):
10361036
}
10371037

10381038
@staticmethod
1039-
def _parse_html(message_text, entities, urled=False, offset=None):
1039+
def _parse_html(message_text, entities, urled=False, offset=0):
10401040
if message_text is None:
10411041
return None
10421042

@@ -1086,8 +1086,7 @@ def _parse_html(message_text, entities, urled=False, offset=None):
10861086
else:
10871087
insert = text
10881088

1089-
if offset is None:
1090-
offset = 0
1089+
if offset == 0:
10911090
if sys.maxunicode == 0xffff:
10921091
html_text += escape(message_text[last_offset:entity.offset
10931092
- offset]) + insert
@@ -1104,7 +1103,7 @@ def _parse_html(message_text, entities, urled=False, offset=None):
11041103

11051104
last_offset = entity.offset - offset + entity.length
11061105

1107-
if offset is None:
1106+
if offset == 0:
11081107
if sys.maxunicode == 0xffff:
11091108
html_text += escape(message_text[last_offset:])
11101109
else:
@@ -1172,7 +1171,7 @@ def caption_html_urled(self):
11721171
return self._parse_html(self.caption, self.parse_caption_entities(), urled=True)
11731172

11741173
@staticmethod
1175-
def _parse_markdown(message_text, entities, urled=False, version=1, offset=None):
1174+
def _parse_markdown(message_text, entities, urled=False, version=1, offset=0):
11761175
version = int(version)
11771176

11781177
if message_text is None:
@@ -1242,8 +1241,7 @@ def _parse_markdown(message_text, entities, urled=False, version=1, offset=None)
12421241
else:
12431242
insert = text
12441243

1245-
if offset is None:
1246-
offset = 0
1244+
if offset == 0:
12471245
if sys.maxunicode == 0xffff:
12481246
markdown_text += escape_markdown(message_text[last_offset:entity.offset
12491247
- offset],
@@ -1262,7 +1260,7 @@ def _parse_markdown(message_text, entities, urled=False, version=1, offset=None)
12621260

12631261
last_offset = entity.offset - offset + entity.length
12641262

1265-
if offset is None:
1263+
if offset == 0:
12661264
if sys.maxunicode == 0xffff:
12671265
markdown_text += escape_markdown(message_text[last_offset:], version=version)
12681266
else:

tests/test_message.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ def test_text_markdown_simple(self):
237237

238238
def test_text_markdown_v2_simple(self):
239239
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\`code`, '
240-
'[links](http://github.com/abc\\\\\\)def), '
241-
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```. '
242-
'http://google\.com and _bold *nested in ~strk~ nested in* italic_.')
240+
'[links](http://github.com/abc\\\\\)def), '
241+
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```\. '
242+
'http://google\.com and _bold *nested in ~strk~ nested in* italic_\.')
243243
text_markdown = self.test_message_v2.text_markdown_v2
244244
assert text_markdown == test_md_string
245245

@@ -258,10 +258,10 @@ def test_text_markdown_urled(self):
258258

259259
def test_text_markdown_v2_urled(self):
260260
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\`code`, '
261-
'[links](http://github.com/abc\\\\\\)def), '
262-
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```. '
261+
'[links](http://github.com/abc\\\\\)def), '
262+
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```\. '
263263
'[http://google\.com](http://google.com) and _bold *nested in ~strk~ '
264-
'nested in* italic_.')
264+
'nested in* italic_\.')
265265
text_markdown = self.test_message_v2.text_markdown_v2_urled
266266
assert text_markdown == test_md_string
267267

@@ -314,8 +314,8 @@ def test_caption_markdown_simple(self):
314314
def test_caption_markdown_v2_simple(self):
315315
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\`code`, '
316316
'[links](http://github.com/abc\\\\\\)def), '
317-
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```. '
318-
'http://google\.com and _bold *nested in ~strk~ nested in* italic_.')
317+
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```\. '
318+
'http://google\.com and _bold *nested in ~strk~ nested in* italic_\.')
319319
caption_markdown = self.test_message_v2.caption_markdown_v2
320320
assert caption_markdown == test_md_string
321321

@@ -335,9 +335,9 @@ def test_caption_markdown_urled(self):
335335
def test_caption_markdown_v2_urled(self):
336336
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\`code`, '
337337
'[links](http://github.com/abc\\\\\\)def), '
338-
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```. '
338+
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```\. '
339339
'[http://google\.com](http://google.com) and _bold *nested in ~strk~ '
340-
'nested in* italic_.')
340+
'nested in* italic_\.')
341341
caption_markdown = self.test_message_v2.caption_markdown_v2_urled
342342
assert caption_markdown == test_md_string
343343

@@ -448,8 +448,8 @@ def test(*args, **kwargs):
448448
quote=True)
449449

450450
def test_reply_markdown_v2(self, monkeypatch, message):
451-
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\\\`code`, '
452-
'[links](http://github.com/abc\\\\\\)def), '
451+
test_md_string = (r'Test for <*bold*, _ita\_lic_, `\\\`code`, '
452+
'[links](http://github.com/abc\\\\\)def), '
453453
'[text\-mention](tg://user?id=123456789) and ```\`\\\\pre```\. '
454454
'http://google\.com and _bold *nested in ~strk~ nested in* italic_\.')
455455

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy