Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa3702d

Browse files
committed
#13960: HTMLParser is now able to handle broken comments when strict=False.
1 parent 5b14d73 commit fa3702d

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

Lib/html/parser.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,17 @@ def goahead(self, end):
184184
elif startswith("<?", i):
185185
k = self.parse_pi(i)
186186
elif startswith("<!", i):
187-
k = self.parse_declaration(i)
187+
# this might fail with things like <! not a comment > or
188+
# <! -- space before '--' -->. When strict is True an
189+
# error is raised, when it's False they will be considered
190+
# as bogus comments and parsed (see parse_bogus_comment).
191+
if self.strict:
192+
k = self.parse_declaration(i)
193+
else:
194+
try:
195+
k = self.parse_declaration(i)
196+
except HTMLParseError:
197+
k = self.parse_bogus_comment(i)
188198
elif (i + 1) < n:
189199
self.handle_data("<")
190200
k = i + 1
@@ -256,6 +266,19 @@ def goahead(self, end):
256266
i = self.updatepos(i, n)
257267
self.rawdata = rawdata[i:]
258268

269+
# Internal -- parse bogus comment, return length or -1 if not terminated
270+
# see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
271+
def parse_bogus_comment(self, i, report=1):
272+
rawdata = self.rawdata
273+
if rawdata[i:i+2] != '<!':
274+
self.error('unexpected call to parse_comment()')
275+
pos = rawdata.find('>', i+2)
276+
if pos == -1:
277+
return -1
278+
if report:
279+
self.handle_comment(rawdata[i+2:pos])
280+
return pos + 1
281+
259282
# Internal -- parse processing instr, return end or -1 if not terminated
260283
def parse_pi(self, i):
261284
rawdata = self.rawdata

Lib/test/test_htmlparser.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ def get_events(self):
323323
("endtag", element_lower)],
324324
collector=Collector())
325325

326+
def test_comments(self):
327+
html = ("<!-- I'm a valid comment -->"
328+
'<!--me too!-->'
329+
'<!------>'
330+
'<!---->'
331+
'<!----I have many hyphens---->'
332+
'<!-- I have a > in the middle -->'
333+
'<!-- and I have -- in the middle! -->')
334+
expected = [('comment', " I'm a valid comment "),
335+
('comment', 'me too!'),
336+
('comment', '--'),
337+
('comment', ''),
338+
('comment', '--I have many hyphens--'),
339+
('comment', ' I have a > in the middle '),
340+
('comment', ' and I have -- in the middle! ')]
341+
self._run_check(html, expected)
342+
326343
def test_condcoms(self):
327344
html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
328345
'<!--[if IE 8]>condcoms<![endif]-->'
@@ -426,6 +443,19 @@ def test_unescape_function(self):
426443
# see #12888
427444
self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
428445

446+
def test_broken_comments(self):
447+
html = ('<! not really a comment >'
448+
'<! not a comment either -->'
449+
'<! -- close enough -->'
450+
'<!!! another bogus comment !!!>')
451+
expected = [
452+
('comment', ' not really a comment '),
453+
('comment', ' not a comment either --'),
454+
('comment', ' -- close enough --'),
455+
('comment', '!! another bogus comment !!!'),
456+
]
457+
self._run_check(html, expected)
458+
429459
def test_broken_condcoms(self):
430460
# these condcoms are missing the '--' after '<!' and before the '>'
431461
html = ('<![if !(IE)]>broken condcom<![endif]>'

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ Core and Builtins
113113
Library
114114
-------
115115

116+
- Issue #13960: HTMLParser is now able to handle broken comments when
117+
strict=False.
118+
116119
- Issue #9021: Add an introduction to the copy module documentation.
117120

118121
- Issue #6005: Examples in the socket library documentation use sendall, where
@@ -123,7 +126,7 @@ Library
123126

124127
- Issue #10881: Fix test_site failure with OS X framework builds.
125128

126-
- Issue #964437 Make IDLE help window non-modal.
129+
- Issue #964437: Make IDLE help window non-modal.
127130
Patch by Guilherme Polo and Roger Serwy.
128131

129132
- Issue #2945: Make the distutils upload command aware of bdist_rpm products.

0 commit comments

Comments
 (0)