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

Skip to content

Commit 62f3d03

Browse files
committed
#13576: add tests about the handling of (possibly broken) condcoms.
1 parent 80a61e8 commit 62f3d03

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lib/_markupbase.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def parse_declaration(self, i):
107107
if decltype == "doctype":
108108
self.handle_decl(data)
109109
else:
110+
# According to the HTML5 specs sections "8.2.4.44 Bogus
111+
# comment state" and "8.2.4.45 Markup declaration open
112+
# state", a comment token should be emitted.
113+
# Calling unknown_decl provides more flexibility though.
110114
self.unknown_decl(data)
111115
return j + 1
112116
if c in "\"'":

Lib/test/test_htmlparser.py

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

326+
def test_condcoms(self):
327+
html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
328+
'<!--[if IE 8]>condcoms<![endif]-->'
329+
'<!--[if lte IE 7]>pretty?<![endif]-->')
330+
expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"),
331+
('comment', '[if IE 8]>condcoms<![endif]'),
332+
('comment', '[if lte IE 7]>pretty?<![endif]')]
333+
self._run_check(html, expected)
334+
335+
326336
class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
327337

328338
def get_collector(self):
@@ -416,6 +426,39 @@ def test_unescape_function(self):
416426
# see #12888
417427
self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
418428

429+
def test_broken_condcoms(self):
430+
# these condcoms are missing the '--' after '<!' and before the '>'
431+
html = ('<![if !(IE)]>broken condcom<![endif]>'
432+
'<![if ! IE]><link href="favicon.tiff"/><![endif]>'
433+
'<![if !IE 6]><img src="firefox.png" /><![endif]>'
434+
'<![if !ie 6]><b>foo</b><![endif]>'
435+
'<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>')
436+
# According to the HTML5 specs sections "8.2.4.44 Bogus comment state"
437+
# and "8.2.4.45 Markup declaration open state", comment tokens should
438+
# be emitted instead of 'unknown decl', but calling unknown_decl
439+
# provides more flexibility.
440+
# See also Lib/_markupbase.py:parse_declaration
441+
expected = [
442+
('unknown decl', 'if !(IE)'),
443+
('data', 'broken condcom'),
444+
('unknown decl', 'endif'),
445+
('unknown decl', 'if ! IE'),
446+
('startendtag', 'link', [('href', 'favicon.tiff')]),
447+
('unknown decl', 'endif'),
448+
('unknown decl', 'if !IE 6'),
449+
('startendtag', 'img', [('src', 'firefox.png')]),
450+
('unknown decl', 'endif'),
451+
('unknown decl', 'if !ie 6'),
452+
('starttag', 'b', []),
453+
('data', 'foo'),
454+
('endtag', 'b'),
455+
('unknown decl', 'endif'),
456+
('unknown decl', 'if (!IE)|(lt IE 9)'),
457+
('startendtag', 'img', [('src', 'mammoth.bmp')]),
458+
('unknown decl', 'endif')
459+
]
460+
self._run_check(html, expected)
461+
419462

420463
class AttributesStrictTestCase(TestCaseBase):
421464

0 commit comments

Comments
 (0)