blob: c15bb660e2b4e1ae4557a73f014510d893a9b4f3 [file] [log] [blame]
Fred Drakebd3090d2001-05-18 15:32:59 +00001"""Tests for HTMLParser.py."""
2
Georg Brandlbcdafa42008-05-20 07:58:42 +00003import HTMLParser
Fred Drake029acfb2001-08-20 21:24:19 +00004import pprint
Fred Drakebd3090d2001-05-18 15:32:59 +00005import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00006from test import test_support
Fred Drakebd3090d2001-05-18 15:32:59 +00007
8
Georg Brandlbcdafa42008-05-20 07:58:42 +00009class EventCollector(HTMLParser.HTMLParser):
Fred Drakebd3090d2001-05-18 15:32:59 +000010
11 def __init__(self):
12 self.events = []
13 self.append = self.events.append
Georg Brandlbcdafa42008-05-20 07:58:42 +000014 HTMLParser.HTMLParser.__init__(self)
Fred Drakebd3090d2001-05-18 15:32:59 +000015
16 def get_events(self):
17 # Normalize the list of events so that buffer artefacts don't
18 # separate runs of contiguous characters.
19 L = []
20 prevtype = None
21 for event in self.events:
22 type = event[0]
23 if type == prevtype == "data":
24 L[-1] = ("data", L[-1][1] + event[1])
25 else:
26 L.append(event)
27 prevtype = type
28 self.events = L
29 return L
30
31 # structure markup
32
33 def handle_starttag(self, tag, attrs):
34 self.append(("starttag", tag, attrs))
35
36 def handle_startendtag(self, tag, attrs):
37 self.append(("startendtag", tag, attrs))
38
39 def handle_endtag(self, tag):
40 self.append(("endtag", tag))
41
42 # all other markup
43
44 def handle_comment(self, data):
45 self.append(("comment", data))
46
47 def handle_charref(self, data):
48 self.append(("charref", data))
49
50 def handle_data(self, data):
51 self.append(("data", data))
52
53 def handle_decl(self, data):
54 self.append(("decl", data))
55
56 def handle_entityref(self, data):
57 self.append(("entityref", data))
58
59 def handle_pi(self, data):
60 self.append(("pi", data))
61
Fred Drakec20a6982001-09-04 15:13:04 +000062 def unknown_decl(self, decl):
63 self.append(("unknown decl", decl))
64
Fred Drakebd3090d2001-05-18 15:32:59 +000065
66class EventCollectorExtra(EventCollector):
67
68 def handle_starttag(self, tag, attrs):
69 EventCollector.handle_starttag(self, tag, attrs)
70 self.append(("starttag_text", self.get_starttag_text()))
71
72
73class TestCaseBase(unittest.TestCase):
74
Fred Drakec20a6982001-09-04 15:13:04 +000075 def _run_check(self, source, expected_events, collector=EventCollector):
Fred Drakebd3090d2001-05-18 15:32:59 +000076 parser = collector()
Fred Drakebd3090d2001-05-18 15:32:59 +000077 for s in source:
78 parser.feed(s)
Fred Drakebd3090d2001-05-18 15:32:59 +000079 parser.close()
Fred Drake029acfb2001-08-20 21:24:19 +000080 events = parser.get_events()
Fred Drakec20a6982001-09-04 15:13:04 +000081 if events != expected_events:
82 self.fail("received events did not match expected events\n"
83 "Expected:\n" + pprint.pformat(expected_events) +
84 "\nReceived:\n" + pprint.pformat(events))
Fred Drakebd3090d2001-05-18 15:32:59 +000085
86 def _run_check_extra(self, source, events):
87 self._run_check(source, events, EventCollectorExtra)
88
89 def _parse_error(self, source):
90 def parse(source=source):
Georg Brandlbcdafa42008-05-20 07:58:42 +000091 parser = HTMLParser.HTMLParser()
Fred Drakebd3090d2001-05-18 15:32:59 +000092 parser.feed(source)
93 parser.close()
Georg Brandlbcdafa42008-05-20 07:58:42 +000094 self.assertRaises(HTMLParser.HTMLParseError, parse)
Fred Drakebd3090d2001-05-18 15:32:59 +000095
96
97class HTMLParserTestCase(TestCaseBase):
98
Fred Drake84bb9d82001-08-03 19:53:01 +000099 def test_processing_instruction_only(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000100 self._run_check("<?processing instruction>", [
101 ("pi", "processing instruction"),
102 ])
Fred Drakefafd56f2003-04-17 22:19:26 +0000103 self._run_check("<?processing instruction ?>", [
104 ("pi", "processing instruction ?"),
105 ])
Fred Drakebd3090d2001-05-18 15:32:59 +0000106
Fred Drake84bb9d82001-08-03 19:53:01 +0000107 def test_simple_html(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000108 self._run_check("""
109<!DOCTYPE html PUBLIC 'foo'>
110<HTML>&entity;&#32;
111<!--comment1a
112-></foo><bar>&lt;<?pi?></foo<bar
113comment1b-->
114<Img sRc='Bar' isMAP>sample
115text
Fred Drake84bb9d82001-08-03 19:53:01 +0000116&#x201C;
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200117<!--comment2a-- --comment2b-->
Fred Drakebd3090d2001-05-18 15:32:59 +0000118</Html>
119""", [
120 ("data", "\n"),
121 ("decl", "DOCTYPE html PUBLIC 'foo'"),
122 ("data", "\n"),
123 ("starttag", "html", []),
124 ("entityref", "entity"),
125 ("charref", "32"),
126 ("data", "\n"),
127 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
128 ("data", "\n"),
129 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
130 ("data", "sample\ntext\n"),
Fred Drake84bb9d82001-08-03 19:53:01 +0000131 ("charref", "x201C"),
132 ("data", "\n"),
Fred Drakebd3090d2001-05-18 15:32:59 +0000133 ("comment", "comment2a-- --comment2b"),
134 ("data", "\n"),
135 ("endtag", "html"),
136 ("data", "\n"),
137 ])
138
Fred Drake073148c2001-12-03 16:44:09 +0000139 def test_unclosed_entityref(self):
140 self._run_check("&entityref foo", [
141 ("entityref", "entityref"),
142 ("data", " foo"),
143 ])
144
Fred Drake84bb9d82001-08-03 19:53:01 +0000145 def test_bad_nesting(self):
146 # Strangely, this *is* supposed to test that overlapping
147 # elements are allowed. HTMLParser is more geared toward
148 # lexing the input that parsing the structure.
Fred Drakebd3090d2001-05-18 15:32:59 +0000149 self._run_check("<a><b></a></b>", [
150 ("starttag", "a", []),
151 ("starttag", "b", []),
152 ("endtag", "a"),
153 ("endtag", "b"),
154 ])
155
Fred Drake029acfb2001-08-20 21:24:19 +0000156 def test_bare_ampersands(self):
157 self._run_check("this text & contains & ampersands &", [
158 ("data", "this text & contains & ampersands &"),
159 ])
160
161 def test_bare_pointy_brackets(self):
162 self._run_check("this < text > contains < bare>pointy< brackets", [
163 ("data", "this < text > contains < bare>pointy< brackets"),
164 ])
165
Fred Drakec20a6982001-09-04 15:13:04 +0000166 def test_illegal_declarations(self):
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200167 self._run_check('<!spacer type="block" height="25">',
168 [('comment', 'spacer type="block" height="25"')])
Fred Drakec20a6982001-09-04 15:13:04 +0000169
Fred Drake84bb9d82001-08-03 19:53:01 +0000170 def test_starttag_end_boundary(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000171 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
172 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
173
Fred Drake84bb9d82001-08-03 19:53:01 +0000174 def test_buffer_artefacts(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000175 output = [("starttag", "a", [("b", "<")])]
176 self._run_check(["<a b='<'>"], output)
177 self._run_check(["<a ", "b='<'>"], output)
178 self._run_check(["<a b", "='<'>"], output)
179 self._run_check(["<a b=", "'<'>"], output)
180 self._run_check(["<a b='<", "'>"], output)
181 self._run_check(["<a b='<'", ">"], output)
182
183 output = [("starttag", "a", [("b", ">")])]
184 self._run_check(["<a b='>'>"], output)
185 self._run_check(["<a ", "b='>'>"], output)
186 self._run_check(["<a b", "='>'>"], output)
187 self._run_check(["<a b=", "'>'>"], output)
188 self._run_check(["<a b='>", "'>"], output)
189 self._run_check(["<a b='>'", ">"], output)
190
Fred Drake75d9a622004-09-08 22:57:01 +0000191 output = [("comment", "abc")]
192 self._run_check(["", "<!--abc-->"], output)
193 self._run_check(["<", "!--abc-->"], output)
194 self._run_check(["<!", "--abc-->"], output)
195 self._run_check(["<!-", "-abc-->"], output)
196 self._run_check(["<!--", "abc-->"], output)
197 self._run_check(["<!--a", "bc-->"], output)
198 self._run_check(["<!--ab", "c-->"], output)
199 self._run_check(["<!--abc", "-->"], output)
200 self._run_check(["<!--abc-", "->"], output)
201 self._run_check(["<!--abc--", ">"], output)
202 self._run_check(["<!--abc-->", ""], output)
203
Fred Drake84bb9d82001-08-03 19:53:01 +0000204 def test_starttag_junk_chars(self):
Ezio Melottif1174432012-02-13 16:28:54 +0200205 self._run_check("</>", [])
206 self._run_check("</$>", [('comment', '$')])
Fred Drakebd3090d2001-05-18 15:32:59 +0000207 self._parse_error("</")
208 self._parse_error("</a")
Fred Drakebd3090d2001-05-18 15:32:59 +0000209 self._parse_error("<a<a>")
Ezio Melottif1174432012-02-13 16:28:54 +0200210 self._run_check("</a<a>", [('endtag', 'a<a')])
Fred Drakebd3090d2001-05-18 15:32:59 +0000211 self._parse_error("<!")
Fred Drakebd3090d2001-05-18 15:32:59 +0000212 self._parse_error("<a")
213 self._parse_error("<a foo='bar'")
214 self._parse_error("<a foo='bar")
215 self._parse_error("<a foo='>'")
216 self._parse_error("<a foo='>")
Fred Drakebd3090d2001-05-18 15:32:59 +0000217
Fred Drake84bb9d82001-08-03 19:53:01 +0000218 def test_declaration_junk_chars(self):
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200219 self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')])
Fred Drakebd3090d2001-05-18 15:32:59 +0000220
Fred Drake84bb9d82001-08-03 19:53:01 +0000221 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000222 self._run_check("<p/>", [
223 ("startendtag", "p", []),
224 ])
225 self._run_check("<p></p>", [
226 ("starttag", "p", []),
227 ("endtag", "p"),
228 ])
229 self._run_check("<p><img src='foo' /></p>", [
230 ("starttag", "p", []),
231 ("startendtag", "img", [("src", "foo")]),
232 ("endtag", "p"),
233 ])
234
Ezio Melottif1174432012-02-13 16:28:54 +0200235 def test_invalid_end_tags(self):
236 # A collection of broken end tags. <br> is used as separator.
237 # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state
238 # and #13993
239 html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>'
240 '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>')
241 expected = [('starttag', 'br', []),
242 # < is part of the name, / is discarded, p is an attribute
243 ('endtag', 'label<'),
244 ('starttag', 'br', []),
245 # text and attributes are discarded
246 ('endtag', 'div'),
247 ('starttag', 'br', []),
248 # comment because the first char after </ is not a-zA-Z
249 ('comment', '<h4'),
250 ('starttag', 'br', []),
251 # attributes are discarded
252 ('endtag', 'li'),
253 ('starttag', 'br', []),
254 # everything till ul (included) is discarded
255 ('endtag', 'li'),
256 ('starttag', 'br', []),
257 # </> is ignored
258 ('starttag', 'br', [])]
259 self._run_check(html, expected)
260
261 def test_broken_invalid_end_tag(self):
262 # This is technically wrong (the "> shouldn't be included in the 'data')
263 # but is probably not worth fixing it (in addition to all the cases of
264 # the previous test, it would require a full attribute parsing).
265 # see #13993
266 html = '<b>This</b attr=">"> confuses the parser'
267 expected = [('starttag', 'b', []),
268 ('data', 'This'),
269 ('endtag', 'b'),
270 ('data', '"> confuses the parser')]
271 self._run_check(html, expected)
272
Fred Drake84bb9d82001-08-03 19:53:01 +0000273 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000274 s = """<foo:bar \n one="1"\ttwo=2 >"""
275 self._run_check_extra(s, [
276 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
277 ("starttag_text", s)])
278
Fred Drake84bb9d82001-08-03 19:53:01 +0000279 def test_cdata_content(self):
Ezio Melotti7e82b272011-11-01 14:09:56 +0200280 contents = [
281 '<!-- not a comment --> &not-an-entity-ref;',
282 "<not a='start tag'>",
283 '<a href="" /> <p> <span></span>',
284 'foo = "</scr" + "ipt>";',
285 'foo = "</SCRIPT" + ">";',
286 'foo = <\n/script> ',
287 '<!-- document.write("</scr" + "ipt>"); -->',
288 ('\n//<![CDATA[\n'
289 'document.write(\'<s\'+\'cript type="text/javascript" '
290 'src="http://www.example.org/r=\'+new '
291 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
292 '\n<!-- //\nvar foo = 3.14;\n// -->\n',
293 'foo = "</sty" + "le>";',
294 u'<!-- \u2603 -->',
295 # these two should be invalid according to the HTML 5 spec,
296 # section 8.1.2.2
297 #'foo = </\nscript>',
298 #'foo = </ script>',
299 ]
300 elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style']
301 for content in contents:
302 for element in elements:
303 element_lower = element.lower()
304 s = u'<{element}>{content}</{element}>'.format(element=element,
305 content=content)
306 self._run_check(s, [("starttag", element_lower, []),
307 ("data", content),
308 ("endtag", element_lower)])
309
Ezio Melotti00dc60b2011-11-18 18:00:40 +0200310 def test_cdata_with_closing_tags(self):
311 # see issue #13358
312 # make sure that HTMLParser calls handle_data only once for each CDATA.
313 # The normal event collector normalizes the events in get_events,
314 # so we override it to return the original list of events.
315 class Collector(EventCollector):
316 def get_events(self):
317 return self.events
318
319 content = """<!-- not a comment --> &not-an-entity-ref;
320 <a href="" /> </p><p> &amp; <span></span></style>
321 '</script' + '>' </html> </head> </scripter>!"""
322 for element in [' script', 'script ', ' script ',
323 '\nscript', 'script\n', '\nscript\n']:
324 s = u'<script>{content}</{element}>'.format(element=element,
325 content=content)
326 self._run_check(s, [("starttag", "script", []),
327 ("data", content),
328 ("endtag", "script")],
329 collector=Collector)
330
Victor Stinner554a3b82010-05-24 21:33:24 +0000331 def test_malformatted_charref(self):
332 self._run_check("<p>&#bad;</p>", [
333 ("starttag", "p", []),
334 ("data", "&#bad;"),
335 ("endtag", "p"),
336 ])
337
Senthil Kumaran3f60f092010-12-28 16:05:07 +0000338 def test_unescape_function(self):
339 parser = HTMLParser.HTMLParser()
340 self.assertEqual(parser.unescape('&#bad;'),'&#bad;')
341 self.assertEqual(parser.unescape('&#0038;'),'&')
342
Fred Drakebd3090d2001-05-18 15:32:59 +0000343
Ezio Melotti74592912011-11-08 02:07:18 +0200344
345class AttributesTestCase(TestCaseBase):
346
347 def test_attr_syntax(self):
348 output = [
349 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
350 ]
351 self._run_check("""<a b='v' c="v" d=v e>""", output)
352 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
353 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
354 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
355
356 def test_attr_values(self):
357 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
358 [("starttag", "a", [("b", "xxx\n\txxx"),
359 ("c", "yyy\t\nyyy"),
360 ("d", "\txyz\n")])])
361 self._run_check("""<a b='' c="">""",
362 [("starttag", "a", [("b", ""), ("c", "")])])
363 # Regression test for SF patch #669683.
364 self._run_check("<e a=rgb(1,2,3)>",
365 [("starttag", "e", [("a", "rgb(1,2,3)")])])
366 # Regression test for SF bug #921657.
367 self._run_check(
368 "<a href=mailto:xyz@example.com>",
369 [("starttag", "a", [("href", "mailto:xyz@example.com")])])
370
371 def test_attr_nonascii(self):
372 # see issue 7311
373 self._run_check(
374 u"<img src=/foo/bar.png alt=\u4e2d\u6587>",
375 [("starttag", "img", [("src", "/foo/bar.png"),
376 ("alt", u"\u4e2d\u6587")])])
377 self._run_check(
378 u"<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
379 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"),
380 ("href", u"\u30c6\u30b9\u30c8.html")])])
381 self._run_check(
382 u'<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
383 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"),
384 ("href", u"\u30c6\u30b9\u30c8.html")])])
385
386 def test_attr_entity_replacement(self):
387 self._run_check(
388 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
389 [("starttag", "a", [("b", "&><\"'")])])
390
391 def test_attr_funky_names(self):
392 self._run_check(
393 "<a a.b='v' c:d=v e-f=v>",
394 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200395 self._run_check(
396 "<a $><b $=%><c \=/>",
397 [("starttag", "a", [("$", None)]),
398 ("starttag", "b", [("$", "%")]),
399 ("starttag", "c", [("\\", "/")])])
Ezio Melotti74592912011-11-08 02:07:18 +0200400
401 def test_entityrefs_in_attributes(self):
402 self._run_check(
403 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
404 [("starttag", "html", [("foo", u"\u20AC&aa&unsupported;")])])
405
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200406 def test_entities_in_attribute_value(self):
407 # see #1200313
408 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
409 self._run_check('<a href="%s">' % entity,
410 [("starttag", "a", [("href", "&")])])
411 self._run_check("<a href='%s'>" % entity,
412 [("starttag", "a", [("href", "&")])])
413 self._run_check("<a href=%s>" % entity,
414 [("starttag", "a", [("href", "&")])])
415
416 def test_malformed_attributes(self):
417 # see #13357
418 html = (
419 "<a href=test'style='color:red;bad1'>test - bad1</a>"
420 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
421 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
422 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
423 )
424 expected = [
425 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
426 ('data', 'test - bad1'), ('endtag', 'a'),
427 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
428 ('data', 'test - bad2'), ('endtag', 'a'),
429 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad3'")]),
430 ('data', 'test - bad3'), ('endtag', 'a'),
431 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad4'")]),
432 ('data', 'test - bad4'), ('endtag', 'a')
433 ]
434 self._run_check(html, expected)
435
436 def test_malformed_adjacent_attributes(self):
437 # see #12629
438 self._run_check('<x><y z=""o"" /></x>',
439 [('starttag', 'x', []),
440 ('startendtag', 'y', [('z', ''), ('o""', None)]),
441 ('endtag', 'x')])
442 self._run_check('<x><y z="""" /></x>',
443 [('starttag', 'x', []),
444 ('startendtag', 'y', [('z', ''), ('""', None)]),
445 ('endtag', 'x')])
446
447 # see #755670 for the following 3 tests
448 def test_adjacent_attributes(self):
449 self._run_check('<a width="100%"cellspacing=0>',
450 [("starttag", "a",
451 [("width", "100%"), ("cellspacing","0")])])
452
453 self._run_check('<a id="foo"class="bar">',
454 [("starttag", "a",
455 [("id", "foo"), ("class","bar")])])
456
457 def test_missing_attribute_value(self):
458 self._run_check('<a v=>',
459 [("starttag", "a", [("v", "")])])
460
461 def test_javascript_attribute_value(self):
462 self._run_check("<a href=javascript:popup('/popup/help.html')>",
463 [("starttag", "a",
464 [("href", "javascript:popup('/popup/help.html')")])])
465
466 def test_end_tag_in_attribute_value(self):
467 # see #1745761
468 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
469 [("starttag", "a",
470 [("href", "http://www.example.org/\">;")]),
471 ("data", "spam"), ("endtag", "a")])
472
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200473 def test_comments(self):
474 html = ("<!-- I'm a valid comment -->"
475 '<!--me too!-->'
476 '<!------>'
477 '<!---->'
478 '<!----I have many hyphens---->'
479 '<!-- I have a > in the middle -->'
480 '<!-- and I have -- in the middle! -->')
481 expected = [('comment', " I'm a valid comment "),
482 ('comment', 'me too!'),
483 ('comment', '--'),
484 ('comment', ''),
485 ('comment', '--I have many hyphens--'),
486 ('comment', ' I have a > in the middle '),
487 ('comment', ' and I have -- in the middle! ')]
488 self._run_check(html, expected)
489
490 def test_broken_comments(self):
491 html = ('<! not really a comment >'
492 '<! not a comment either -->'
493 '<! -- close enough -->'
494 '<!><!<-- this was an empty comment>'
495 '<!!! another bogus comment !!!>')
496 expected = [
497 ('comment', ' not really a comment '),
498 ('comment', ' not a comment either --'),
499 ('comment', ' -- close enough --'),
500 ('comment', ''),
501 ('comment', '<-- this was an empty comment'),
502 ('comment', '!! another bogus comment !!!'),
503 ]
504 self._run_check(html, expected)
505
Ezio Melotti6b7003a2011-12-19 07:28:08 +0200506 def test_condcoms(self):
507 html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
508 '<!--[if IE 8]>condcoms<![endif]-->'
509 '<!--[if lte IE 7]>pretty?<![endif]-->')
510 expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"),
511 ('comment', '[if IE 8]>condcoms<![endif]'),
512 ('comment', '[if lte IE 7]>pretty?<![endif]')]
513 self._run_check(html, expected)
514
515 def test_broken_condcoms(self):
516 # these condcoms are missing the '--' after '<!' and before the '>'
517 html = ('<![if !(IE)]>broken condcom<![endif]>'
518 '<![if ! IE]><link href="favicon.tiff"/><![endif]>'
519 '<![if !IE 6]><img src="firefox.png" /><![endif]>'
520 '<![if !ie 6]><b>foo</b><![endif]>'
521 '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>')
522 # According to the HTML5 specs sections "8.2.4.44 Bogus comment state"
523 # and "8.2.4.45 Markup declaration open state", comment tokens should
524 # be emitted instead of 'unknown decl', but calling unknown_decl
525 # provides more flexibility.
526 # See also Lib/_markupbase.py:parse_declaration
527 expected = [
528 ('unknown decl', 'if !(IE)'),
529 ('data', 'broken condcom'),
530 ('unknown decl', 'endif'),
531 ('unknown decl', 'if ! IE'),
532 ('startendtag', 'link', [('href', 'favicon.tiff')]),
533 ('unknown decl', 'endif'),
534 ('unknown decl', 'if !IE 6'),
535 ('startendtag', 'img', [('src', 'firefox.png')]),
536 ('unknown decl', 'endif'),
537 ('unknown decl', 'if !ie 6'),
538 ('starttag', 'b', []),
539 ('data', 'foo'),
540 ('endtag', 'b'),
541 ('unknown decl', 'endif'),
542 ('unknown decl', 'if (!IE)|(lt IE 9)'),
543 ('startendtag', 'img', [('src', 'mammoth.bmp')]),
544 ('unknown decl', 'endif')
545 ]
546 self._run_check(html, expected)
547
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200548
Fred Drakee8220492001-09-24 20:19:08 +0000549def test_main():
Ezio Melotti74592912011-11-08 02:07:18 +0200550 test_support.run_unittest(HTMLParserTestCase, AttributesTestCase)
Fred Drakee8220492001-09-24 20:19:08 +0000551
552
553if __name__ == "__main__":
554 test_main()