Skip to content

Commit 74cf400

Browse files
committed
bpo-32876: fix CDATA handling and add a couple of tests.
1 parent 5c0a5f3 commit 74cf400

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Lib/html/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def parse_html_declaration(self, i):
260260
if rawdata[i:i+4] == '<!--':
261261
# this case is actually already handled in goahead()
262262
return self.parse_comment(i)
263-
elif rawdata[i:i+3] == '<![CDATA[':
263+
elif rawdata[i:i+9] == '<![CDATA[':
264264
return self.parse_marked_section(i)
265265
elif rawdata[i:i+9].lower() == '<!doctype':
266266
# find the closing >

Lib/test/test_htmlparser.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,30 @@ def test_broken_condcoms(self):
635635
]
636636
self._run_check(html, expected)
637637

638+
def test_cdata_declarations(self):
639+
# More tests should be added. See also "8.2.4.42. Markup
640+
# declaration open state", "8.2.4.69. CDATA section state",
641+
# and issue 32876
642+
html = ('<![CDATA[just some plain text]]>')
643+
expected = [('unknown decl', 'CDATA[just some plain text')]
644+
self._run_check(html, expected)
645+
646+
def test_cdata_declarations_multiline(self):
647+
html = ('<code><![CDATA['
648+
' if (a < b && a > b) {'
649+
' printf("[<marquee>How?</marquee>]");'
650+
' }'
651+
']]></code>')
652+
expected = [
653+
('starttag', 'code', []),
654+
('unknown decl',
655+
'CDATA[ if (a < b && a > b) { '
656+
'printf("[<marquee>How?</marquee>]"); }'),
657+
('endtag', 'code')
658+
]
659+
self._run_check(html, expected)
660+
661+
638662
def test_convert_charrefs_dropped_text(self):
639663
# #23144: make sure that all the events are triggered when
640664
# convert_charrefs is True, even if we don't call .close()

0 commit comments

Comments
 (0)