Skip to content

Commit 1ceccc1

Browse files
committed
Recognize short comments during encoding prescan
1 parent fd4f032 commit 1ceccc1

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎AUTHORS.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ Patches and suggestions
6363
- Ville Skyttä
6464
- Hugo van Kemenade
6565
- Mark Vasilkov
66-
66+
- Muneeb Sheikh

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Features:
1818

1919
Bug fixes:
2020

21+
* Detect declared encodings after short comments whose terminators overlap
22+
the comment opener. (#427)
23+
2124
* The sanitizer now permits ``<summary>`` tags. It used to allow ``<details>``
2225
already. (#423)
2326

‎html5lib/_inputstream.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ def getEncoding(self):
712712
return self.encoding
713713

714714
def handleComment(self):
715-
"""Skip over comments"""
715+
"""Skip over comments, including terminators overlapping the opener."""
716+
self.data.position -= 2
716717
return self.data.jumpTo(b"-->")
717718

718719
def handleMeta(self):

‎html5lib/tests/test_encoding.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,19 @@ def test_chardet():
115115
encoding = _inputstream.HTMLInputStream(fp.read()).charEncoding
116116
assert encoding[0].name == "big5"
117117
# pylint:enable=wrong-import-position
118+
119+
120+
@pytest.mark.parametrize("comment", [b"<!-->", b"<!--->", b"<!---->", b"<!--normal-->"])
121+
def test_prescan_short_comments(comment):
122+
data = comment + b"<meta charset=iso-8859-2>"
123+
encoding = _inputstream.EncodingParser(data).getEncoding()
124+
assert encoding is not None
125+
assert encoding.name == "iso-8859-2"
126+
stream = _inputstream.HTMLBinaryInputStream(data, useChardet=False)
127+
assert stream.charEncoding[0].name == "iso-8859-2"
128+
129+
130+
def test_prescan_ignores_meta_inside_comment():
131+
data = b"<!--<meta charset=utf-8>--><meta charset=iso-8859-2>"
132+
encoding = _inputstream.EncodingParser(data).getEncoding()
133+
assert encoding.name == "iso-8859-2"

0 commit comments

Comments
 (0)