blob: 66675127850d1858dc0b788ba90192b34670eb20 [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
Ezio Melotti369cbd72012-02-13 20:36:55 +0200218 def test_valid_doctypes(self):
219 # from http://www.w3.org/QA/2002/04/valid-dtd-list.html
220 dtds = ['HTML', # HTML5 doctype
221 ('HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
222 '"http://www.w3.org/TR/html4/strict.dtd"'),
223 ('HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" '
224 '"http://www.w3.org/TR/html4/loose.dtd"'),
225 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
226 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'),
227 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" '
228 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'),
229 ('math PUBLIC "-//W3C//DTD MathML 2.0//EN" '
230 '"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"'),
231 ('html PUBLIC "-//W3C//DTD '
232 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" '
233 '"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"'),
234 ('svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
235 '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"'),
236 'html PUBLIC "-//IETF//DTD HTML 2.0//EN"',
237 'html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"']
238 for dtd in dtds:
239 self._run_check("<!DOCTYPE %s>" % dtd,
240 [('decl', 'DOCTYPE ' + dtd)])
241
Fred Drake84bb9d82001-08-03 19:53:01 +0000242 def test_declaration_junk_chars(self):
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200243 self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')])
Fred Drakebd3090d2001-05-18 15:32:59 +0000244
Fred Drake84bb9d82001-08-03 19:53:01 +0000245 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000246 self._run_check("<p/>", [
247 ("startendtag", "p", []),
248 ])
249 self._run_check("<p></p>", [
250 ("starttag", "p", []),
251 ("endtag", "p"),
252 ])
253 self._run_check("<p><img src='foo' /></p>", [
254 ("starttag", "p", []),
255 ("startendtag", "img", [("src", "foo")]),
256 ("endtag", "p"),
257 ])
258
Ezio Melottif1174432012-02-13 16:28:54 +0200259 def test_invalid_end_tags(self):
260 # A collection of broken end tags. <br> is used as separator.
261 # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state
262 # and #13993
263 html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>'
264 '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>')
265 expected = [('starttag', 'br', []),
266 # < is part of the name, / is discarded, p is an attribute
267 ('endtag', 'label<'),
268 ('starttag', 'br', []),
269 # text and attributes are discarded
270 ('endtag', 'div'),
271 ('starttag', 'br', []),
272 # comment because the first char after </ is not a-zA-Z
273 ('comment', '<h4'),
274 ('starttag', 'br', []),
275 # attributes are discarded
276 ('endtag', 'li'),
277 ('starttag', 'br', []),
278 # everything till ul (included) is discarded
279 ('endtag', 'li'),
280 ('starttag', 'br', []),
281 # </> is ignored
282 ('starttag', 'br', [])]
283 self._run_check(html, expected)
284
285 def test_broken_invalid_end_tag(self):
286 # This is technically wrong (the "> shouldn't be included in the 'data')
287 # but is probably not worth fixing it (in addition to all the cases of
288 # the previous test, it would require a full attribute parsing).
289 # see #13993
290 html = '<b>This</b attr=">"> confuses the parser'
291 expected = [('starttag', 'b', []),
292 ('data', 'This'),
293 ('endtag', 'b'),
294 ('data', '"> confuses the parser')]
295 self._run_check(html, expected)
296
Fred Drake84bb9d82001-08-03 19:53:01 +0000297 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000298 s = """<foo:bar \n one="1"\ttwo=2 >"""
299 self._run_check_extra(s, [
300 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
301 ("starttag_text", s)])
302
Fred Drake84bb9d82001-08-03 19:53:01 +0000303 def test_cdata_content(self):
Ezio Melotti7e82b272011-11-01 14:09:56 +0200304 contents = [
305 '<!-- not a comment --> &not-an-entity-ref;',
306 "<not a='start tag'>",
307 '<a href="" /> <p> <span></span>',
308 'foo = "</scr" + "ipt>";',
309 'foo = "</SCRIPT" + ">";',
310 'foo = <\n/script> ',
311 '<!-- document.write("</scr" + "ipt>"); -->',
312 ('\n//<![CDATA[\n'
313 'document.write(\'<s\'+\'cript type="text/javascript" '
314 'src="http://www.example.org/r=\'+new '
315 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
316 '\n<!-- //\nvar foo = 3.14;\n// -->\n',
317 'foo = "</sty" + "le>";',
318 u'<!-- \u2603 -->',
319 # these two should be invalid according to the HTML 5 spec,
320 # section 8.1.2.2
321 #'foo = </\nscript>',
322 #'foo = </ script>',
323 ]
324 elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style']
325 for content in contents:
326 for element in elements:
327 element_lower = element.lower()
328 s = u'<{element}>{content}</{element}>'.format(element=element,
329 content=content)
330 self._run_check(s, [("starttag", element_lower, []),
331 ("data", content),
332 ("endtag", element_lower)])
333
Ezio Melotti00dc60b2011-11-18 18:00:40 +0200334 def test_cdata_with_closing_tags(self):
335 # see issue #13358
336 # make sure that HTMLParser calls handle_data only once for each CDATA.
337 # The normal event collector normalizes the events in get_events,
338 # so we override it to return the original list of events.
339 class Collector(EventCollector):
340 def get_events(self):
341 return self.events
342
343 content = """<!-- not a comment --> &not-an-entity-ref;
344 <a href="" /> </p><p> &amp; <span></span></style>
345 '</script' + '>' </html> </head> </scripter>!"""
346 for element in [' script', 'script ', ' script ',
347 '\nscript', 'script\n', '\nscript\n']:
348 s = u'<script>{content}</{element}>'.format(element=element,
349 content=content)
350 self._run_check(s, [("starttag", "script", []),
351 ("data", content),
352 ("endtag", "script")],
353 collector=Collector)
354
Victor Stinner554a3b82010-05-24 21:33:24 +0000355 def test_malformatted_charref(self):
356 self._run_check("<p>&#bad;</p>", [
357 ("starttag", "p", []),
358 ("data", "&#bad;"),
359 ("endtag", "p"),
360 ])
361
Senthil Kumaran3f60f092010-12-28 16:05:07 +0000362 def test_unescape_function(self):
363 parser = HTMLParser.HTMLParser()
364 self.assertEqual(parser.unescape('&#bad;'),'&#bad;')
365 self.assertEqual(parser.unescape('&#0038;'),'&')
366
Fred Drakebd3090d2001-05-18 15:32:59 +0000367
Ezio Melotti74592912011-11-08 02:07:18 +0200368
369class AttributesTestCase(TestCaseBase):
370
371 def test_attr_syntax(self):
372 output = [
373 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
374 ]
375 self._run_check("""<a b='v' c="v" d=v e>""", output)
376 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
377 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
378 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
379
380 def test_attr_values(self):
381 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
382 [("starttag", "a", [("b", "xxx\n\txxx"),
383 ("c", "yyy\t\nyyy"),
384 ("d", "\txyz\n")])])
385 self._run_check("""<a b='' c="">""",
386 [("starttag", "a", [("b", ""), ("c", "")])])
387 # Regression test for SF patch #669683.
388 self._run_check("<e a=rgb(1,2,3)>",
389 [("starttag", "e", [("a", "rgb(1,2,3)")])])
390 # Regression test for SF bug #921657.
391 self._run_check(
392 "<a href=mailto:xyz@example.com>",
393 [("starttag", "a", [("href", "mailto:xyz@example.com")])])
394
395 def test_attr_nonascii(self):
396 # see issue 7311
397 self._run_check(
398 u"<img src=/foo/bar.png alt=\u4e2d\u6587>",
399 [("starttag", "img", [("src", "/foo/bar.png"),
400 ("alt", u"\u4e2d\u6587")])])
401 self._run_check(
402 u"<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
403 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"),
404 ("href", u"\u30c6\u30b9\u30c8.html")])])
405 self._run_check(
406 u'<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
407 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"),
408 ("href", u"\u30c6\u30b9\u30c8.html")])])
409
410 def test_attr_entity_replacement(self):
411 self._run_check(
412 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
413 [("starttag", "a", [("b", "&><\"'")])])
414
415 def test_attr_funky_names(self):
416 self._run_check(
417 "<a a.b='v' c:d=v e-f=v>",
418 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200419 self._run_check(
420 "<a $><b $=%><c \=/>",
421 [("starttag", "a", [("$", None)]),
422 ("starttag", "b", [("$", "%")]),
423 ("starttag", "c", [("\\", "/")])])
Ezio Melotti74592912011-11-08 02:07:18 +0200424
425 def test_entityrefs_in_attributes(self):
426 self._run_check(
427 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
428 [("starttag", "html", [("foo", u"\u20AC&aa&unsupported;")])])
429
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200430 def test_entities_in_attribute_value(self):
431 # see #1200313
432 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
433 self._run_check('<a href="%s">' % entity,
434 [("starttag", "a", [("href", "&")])])
435 self._run_check("<a href='%s'>" % entity,
436 [("starttag", "a", [("href", "&")])])
437 self._run_check("<a href=%s>" % entity,
438 [("starttag", "a", [("href", "&")])])
439
440 def test_malformed_attributes(self):
441 # see #13357
442 html = (
443 "<a href=test'style='color:red;bad1'>test - bad1</a>"
444 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
445 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
446 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
447 )
448 expected = [
449 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
450 ('data', 'test - bad1'), ('endtag', 'a'),
451 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
452 ('data', 'test - bad2'), ('endtag', 'a'),
453 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad3'")]),
454 ('data', 'test - bad3'), ('endtag', 'a'),
455 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad4'")]),
456 ('data', 'test - bad4'), ('endtag', 'a')
457 ]
458 self._run_check(html, expected)
459
460 def test_malformed_adjacent_attributes(self):
461 # see #12629
462 self._run_check('<x><y z=""o"" /></x>',
463 [('starttag', 'x', []),
464 ('startendtag', 'y', [('z', ''), ('o""', None)]),
465 ('endtag', 'x')])
466 self._run_check('<x><y z="""" /></x>',
467 [('starttag', 'x', []),
468 ('startendtag', 'y', [('z', ''), ('""', None)]),
469 ('endtag', 'x')])
470
471 # see #755670 for the following 3 tests
472 def test_adjacent_attributes(self):
473 self._run_check('<a width="100%"cellspacing=0>',
474 [("starttag", "a",
475 [("width", "100%"), ("cellspacing","0")])])
476
477 self._run_check('<a id="foo"class="bar">',
478 [("starttag", "a",
479 [("id", "foo"), ("class","bar")])])
480
481 def test_missing_attribute_value(self):
482 self._run_check('<a v=>',
483 [("starttag", "a", [("v", "")])])
484
485 def test_javascript_attribute_value(self):
486 self._run_check("<a href=javascript:popup('/popup/help.html')>",
487 [("starttag", "a",
488 [("href", "javascript:popup('/popup/help.html')")])])
489
490 def test_end_tag_in_attribute_value(self):
491 # see #1745761
492 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
493 [("starttag", "a",
494 [("href", "http://www.example.org/\">;")]),
495 ("data", "spam"), ("endtag", "a")])
496
Ezio Melotti4b92cc32012-02-13 16:10:44 +0200497 def test_comments(self):
498 html = ("<!-- I'm a valid comment -->"
499 '<!--me too!-->'
500 '<!------>'
501 '<!---->'
502 '<!----I have many hyphens---->'
503 '<!-- I have a > in the middle -->'
504 '<!-- and I have -- in the middle! -->')
505 expected = [('comment', " I'm a valid comment "),
506 ('comment', 'me too!'),
507 ('comment', '--'),
508 ('comment', ''),
509 ('comment', '--I have many hyphens--'),
510 ('comment', ' I have a > in the middle '),
511 ('comment', ' and I have -- in the middle! ')]
512 self._run_check(html, expected)
513
514 def test_broken_comments(self):
515 html = ('<! not really a comment >'
516 '<! not a comment either -->'
517 '<! -- close enough -->'
518 '<!><!<-- this was an empty comment>'
519 '<!!! another bogus comment !!!>')
520 expected = [
521 ('comment', ' not really a comment '),
522 ('comment', ' not a comment either --'),
523 ('comment', ' -- close enough --'),
524 ('comment', ''),
525 ('comment', '<-- this was an empty comment'),
526 ('comment', '!! another bogus comment !!!'),
527 ]
528 self._run_check(html, expected)
529
Ezio Melotti6b7003a2011-12-19 07:28:08 +0200530 def test_condcoms(self):
531 html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
532 '<!--[if IE 8]>condcoms<![endif]-->'
533 '<!--[if lte IE 7]>pretty?<![endif]-->')
534 expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"),
535 ('comment', '[if IE 8]>condcoms<![endif]'),
536 ('comment', '[if lte IE 7]>pretty?<![endif]')]
537 self._run_check(html, expected)
538
539 def test_broken_condcoms(self):
540 # these condcoms are missing the '--' after '<!' and before the '>'
541 html = ('<![if !(IE)]>broken condcom<![endif]>'
542 '<![if ! IE]><link href="favicon.tiff"/><![endif]>'
543 '<![if !IE 6]><img src="firefox.png" /><![endif]>'
544 '<![if !ie 6]><b>foo</b><![endif]>'
545 '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>')
546 # According to the HTML5 specs sections "8.2.4.44 Bogus comment state"
547 # and "8.2.4.45 Markup declaration open state", comment tokens should
548 # be emitted instead of 'unknown decl', but calling unknown_decl
549 # provides more flexibility.
550 # See also Lib/_markupbase.py:parse_declaration
551 expected = [
552 ('unknown decl', 'if !(IE)'),
553 ('data', 'broken condcom'),
554 ('unknown decl', 'endif'),
555 ('unknown decl', 'if ! IE'),
556 ('startendtag', 'link', [('href', 'favicon.tiff')]),
557 ('unknown decl', 'endif'),
558 ('unknown decl', 'if !IE 6'),
559 ('startendtag', 'img', [('src', 'firefox.png')]),
560 ('unknown decl', 'endif'),
561 ('unknown decl', 'if !ie 6'),
562 ('starttag', 'b', []),
563 ('data', 'foo'),
564 ('endtag', 'b'),
565 ('unknown decl', 'endif'),
566 ('unknown decl', 'if (!IE)|(lt IE 9)'),
567 ('startendtag', 'img', [('src', 'mammoth.bmp')]),
568 ('unknown decl', 'endif')
569 ]
570 self._run_check(html, expected)
571
Ezio Melotti0f1571c2011-11-14 18:04:05 +0200572
Fred Drakee8220492001-09-24 20:19:08 +0000573def test_main():
Ezio Melotti74592912011-11-08 02:07:18 +0200574 test_support.run_unittest(HTMLParserTestCase, AttributesTestCase)
Fred Drakee8220492001-09-24 20:19:08 +0000575
576
577if __name__ == "__main__":
578 test_main()