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