Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion telebot/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def hpre(content: str, escape: Optional[bool]=True, language: str="") -> str:
:return: The formatted string.
:rtype: :obj:`str`
"""
return '<pre><code class="{}">{}</code></pre>'.format(language, escape_html(content) if escape else content)
return '<pre><code class="{}">{}</code></pre>'.format(escape_html(language), escape_html(content) if escape else content)


def hide_link(url: str) -> str:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys

sys.path.append('../')

from html.parser import HTMLParser

from telebot.formatting import hpre


class AttributeCollector(HTMLParser):
def __init__(self):
super().__init__()
self.tags = []

def handle_starttag(self, tag, attrs):
self.tags.append((tag, attrs))


def test_hpre_keeps_plain_language():
assert hpre("x", language="python") == '<pre><code class="python">x</code></pre>'


def test_hpre_escapes_quotes_in_language():
assert hpre("x", language='a"b') == '<pre><code class="a&quot;b">x</code></pre>'


def test_hpre_language_cannot_add_attributes():
parser = AttributeCollector()
parser.feed(hpre("x", language='a" onmouseover="alert(1)'))
assert parser.tags == [
("pre", []),
("code", [("class", 'a" onmouseover="alert(1)')]),
]


def test_hpre_still_escapes_content():
assert hpre("<b>", language="python") == '<pre><code class="python">&lt;b&gt;</code></pre>'
Loading