Skip to content

Commit cd54f2e

Browse files
committed
Fix escape_html to escape_entity
1 parent 4b26fc4 commit cd54f2e

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

telebot/formatting.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,12 @@ def apply_html_entities_coder(text: str, entities=None, custom_subs=None) -> str
10711071
# Convert text to utf-16 encoding for proper handling
10721072
utf16_text = text.encode("utf-16-le")
10731073

1074+
def escape_entity(text_part):
1075+
"""Escape HTML special characters in a text part"""
1076+
if isinstance(text_part, bytes):
1077+
text_part = text_part.decode("utf-16-le")
1078+
return text_part.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
1079+
10741080
def format_entity(entity, content):
10751081
"""Apply entity formatting to the content"""
10761082
entity_type = entity.type
@@ -1099,19 +1105,19 @@ def process_entities(byte_text, entity_list, start_pos=0, end_pos=None):
10991105
end_pos = len(byte_text)
11001106

11011107
if not entity_list or start_pos >= end_pos:
1102-
return escape_html(byte_text[start_pos:end_pos])
1108+
return escape_entity(byte_text[start_pos:end_pos])
11031109

11041110
current_entity = entity_list[0]
11051111
current_start = current_entity.offset * 2
11061112
current_end = current_start + current_entity.length * 2
11071113

11081114
if current_end <= start_pos or current_start >= end_pos:
1109-
return escape_html(byte_text[start_pos:end_pos])
1115+
return escape_entity(byte_text[start_pos:end_pos])
11101116

11111117
result = []
11121118

11131119
if current_start > start_pos:
1114-
result.append(escape_html(byte_text[start_pos:current_start]))
1120+
result.append(escape_entity(byte_text[start_pos:current_start]))
11151121

11161122
nested_entities = []
11171123
remaining_entities = []
@@ -1133,7 +1139,7 @@ def process_entities(byte_text, entity_list, start_pos=0, end_pos=None):
11331139
current_end
11341140
)
11351141
else:
1136-
inner_content = escape_html(byte_text[current_start:current_end])
1142+
inner_content = escape_entity(byte_text[current_start:current_end])
11371143

11381144
result.append(format_entity(current_entity, inner_content))
11391145

@@ -1145,7 +1151,7 @@ def process_entities(byte_text, entity_list, start_pos=0, end_pos=None):
11451151
end_pos
11461152
))
11471153
elif current_end < end_pos:
1148-
result.append(escape_html(byte_text[current_end:end_pos]))
1154+
result.append(escape_entity(byte_text[current_end:end_pos]))
11491155

11501156
return "".join(result)
11511157

0 commit comments

Comments
 (0)