blob: 1ce4594a44a65145ee12b2fb6561fab1969eed9e [file] [log] [blame]
Fred Drakebd3090d2001-05-18 15:32:59 +00001"""Tests for HTMLParser.py."""
2
Mark Dickinsonf64dcf32008-05-21 13:51:18 +00003import html.parser
Fred Drake029acfb2001-08-20 21:24:19 +00004import pprint
Fred Drakebd3090d2001-05-18 15:32:59 +00005import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Fred Drakebd3090d2001-05-18 15:32:59 +00007
8
Mark Dickinsonf64dcf32008-05-21 13:51:18 +00009class EventCollector(html.parser.HTMLParser):
Fred Drakebd3090d2001-05-18 15:32:59 +000010
R. David Murrayb579dba2010-12-03 04:06:39 +000011 def __init__(self, *args, **kw):
Fred Drakebd3090d2001-05-18 15:32:59 +000012 self.events = []
13 self.append = self.events.append
R. David Murrayb579dba2010-12-03 04:06:39 +000014 html.parser.HTMLParser.__init__(self, *args, **kw)
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
Ezio Melottic1e73c32011-11-01 18:57:15 +020075 def get_collector(self):
76 raise NotImplementedError
77
R. David Murrayb579dba2010-12-03 04:06:39 +000078 def _run_check(self, source, expected_events, collector=None):
79 if collector is None:
Ezio Melottic1e73c32011-11-01 18:57:15 +020080 collector = self.get_collector()
R. David Murrayb579dba2010-12-03 04:06:39 +000081 parser = collector
Fred Drakebd3090d2001-05-18 15:32:59 +000082 for s in source:
83 parser.feed(s)
Fred Drakebd3090d2001-05-18 15:32:59 +000084 parser.close()
Fred Drake029acfb2001-08-20 21:24:19 +000085 events = parser.get_events()
Fred Drakec20a6982001-09-04 15:13:04 +000086 if events != expected_events:
87 self.fail("received events did not match expected events\n"
88 "Expected:\n" + pprint.pformat(expected_events) +
89 "\nReceived:\n" + pprint.pformat(events))
Fred Drakebd3090d2001-05-18 15:32:59 +000090
91 def _run_check_extra(self, source, events):
R. David Murrayb579dba2010-12-03 04:06:39 +000092 self._run_check(source, events, EventCollectorExtra())
Fred Drakebd3090d2001-05-18 15:32:59 +000093
94 def _parse_error(self, source):
95 def parse(source=source):
Mark Dickinsonf64dcf32008-05-21 13:51:18 +000096 parser = html.parser.HTMLParser()
Fred Drakebd3090d2001-05-18 15:32:59 +000097 parser.feed(source)
98 parser.close()
Mark Dickinsonf64dcf32008-05-21 13:51:18 +000099 self.assertRaises(html.parser.HTMLParseError, parse)
Fred Drakebd3090d2001-05-18 15:32:59 +0000100
101
Ezio Melottic1e73c32011-11-01 18:57:15 +0200102class HTMLParserStrictTestCase(TestCaseBase):
103
104 def get_collector(self):
105 return EventCollector(strict=True)
Fred Drakebd3090d2001-05-18 15:32:59 +0000106
Fred Drake84bb9d82001-08-03 19:53:01 +0000107 def test_processing_instruction_only(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000108 self._run_check("<?processing instruction>", [
109 ("pi", "processing instruction"),
110 ])
Fred Drakefafd56f2003-04-17 22:19:26 +0000111 self._run_check("<?processing instruction ?>", [
112 ("pi", "processing instruction ?"),
113 ])
Fred Drakebd3090d2001-05-18 15:32:59 +0000114
Fred Drake84bb9d82001-08-03 19:53:01 +0000115 def test_simple_html(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000116 self._run_check("""
117<!DOCTYPE html PUBLIC 'foo'>
118<HTML>&entity;&#32;
119<!--comment1a
120-></foo><bar>&lt;<?pi?></foo<bar
121comment1b-->
122<Img sRc='Bar' isMAP>sample
123text
Fred Drake84bb9d82001-08-03 19:53:01 +0000124&#x201C;
Georg Brandld09def32006-03-09 13:27:14 +0000125<!--comment2a-- --comment2b--><!>
Fred Drakebd3090d2001-05-18 15:32:59 +0000126</Html>
127""", [
128 ("data", "\n"),
129 ("decl", "DOCTYPE html PUBLIC 'foo'"),
130 ("data", "\n"),
131 ("starttag", "html", []),
132 ("entityref", "entity"),
133 ("charref", "32"),
134 ("data", "\n"),
135 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
136 ("data", "\n"),
137 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
138 ("data", "sample\ntext\n"),
Fred Drake84bb9d82001-08-03 19:53:01 +0000139 ("charref", "x201C"),
140 ("data", "\n"),
Fred Drakebd3090d2001-05-18 15:32:59 +0000141 ("comment", "comment2a-- --comment2b"),
142 ("data", "\n"),
143 ("endtag", "html"),
144 ("data", "\n"),
145 ])
146
Victor Stinnere021f4b2010-05-24 21:46:25 +0000147 def test_malformatted_charref(self):
148 self._run_check("<p>&#bad;</p>", [
149 ("starttag", "p", []),
150 ("data", "&#bad;"),
151 ("endtag", "p"),
152 ])
153
Fred Drake073148c2001-12-03 16:44:09 +0000154 def test_unclosed_entityref(self):
155 self._run_check("&entityref foo", [
156 ("entityref", "entityref"),
157 ("data", " foo"),
158 ])
159
Fred Drake029acfb2001-08-20 21:24:19 +0000160 def test_doctype_decl(self):
161 inside = """\
162DOCTYPE html [
163 <!ELEMENT html - O EMPTY>
164 <!ATTLIST html
Fred Drakec20a6982001-09-04 15:13:04 +0000165 version CDATA #IMPLIED
166 profile CDATA 'DublinCore'>
167 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
168 <!ENTITY myEntity 'internal parsed entity'>
169 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
170 <!ENTITY % paramEntity 'name|name|name'>
171 %paramEntity;
Fred Drake029acfb2001-08-20 21:24:19 +0000172 <!-- comment -->
173]"""
174 self._run_check("<!%s>" % inside, [
175 ("decl", inside),
176 ])
177
Fred Drake84bb9d82001-08-03 19:53:01 +0000178 def test_bad_nesting(self):
179 # Strangely, this *is* supposed to test that overlapping
180 # elements are allowed. HTMLParser is more geared toward
181 # lexing the input that parsing the structure.
Fred Drakebd3090d2001-05-18 15:32:59 +0000182 self._run_check("<a><b></a></b>", [
183 ("starttag", "a", []),
184 ("starttag", "b", []),
185 ("endtag", "a"),
186 ("endtag", "b"),
187 ])
188
Fred Drake029acfb2001-08-20 21:24:19 +0000189 def test_bare_ampersands(self):
190 self._run_check("this text & contains & ampersands &", [
191 ("data", "this text & contains & ampersands &"),
192 ])
193
194 def test_bare_pointy_brackets(self):
195 self._run_check("this < text > contains < bare>pointy< brackets", [
196 ("data", "this < text > contains < bare>pointy< brackets"),
197 ])
198
Fred Drakec20a6982001-09-04 15:13:04 +0000199 def test_illegal_declarations(self):
Fred Drake7cf613d2001-09-04 16:26:03 +0000200 self._parse_error('<!spacer type="block" height="25">')
Fred Drakec20a6982001-09-04 15:13:04 +0000201
Fred Drake84bb9d82001-08-03 19:53:01 +0000202 def test_starttag_end_boundary(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000203 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
204 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
205
Fred Drake84bb9d82001-08-03 19:53:01 +0000206 def test_buffer_artefacts(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000207 output = [("starttag", "a", [("b", "<")])]
208 self._run_check(["<a b='<'>"], output)
209 self._run_check(["<a ", "b='<'>"], output)
210 self._run_check(["<a b", "='<'>"], output)
211 self._run_check(["<a b=", "'<'>"], output)
212 self._run_check(["<a b='<", "'>"], output)
213 self._run_check(["<a b='<'", ">"], output)
214
215 output = [("starttag", "a", [("b", ">")])]
216 self._run_check(["<a b='>'>"], output)
217 self._run_check(["<a ", "b='>'>"], output)
218 self._run_check(["<a b", "='>'>"], output)
219 self._run_check(["<a b=", "'>'>"], output)
220 self._run_check(["<a b='>", "'>"], output)
221 self._run_check(["<a b='>'", ">"], output)
222
Fred Drake75d9a622004-09-08 22:57:01 +0000223 output = [("comment", "abc")]
224 self._run_check(["", "<!--abc-->"], output)
225 self._run_check(["<", "!--abc-->"], output)
226 self._run_check(["<!", "--abc-->"], output)
227 self._run_check(["<!-", "-abc-->"], output)
228 self._run_check(["<!--", "abc-->"], output)
229 self._run_check(["<!--a", "bc-->"], output)
230 self._run_check(["<!--ab", "c-->"], output)
231 self._run_check(["<!--abc", "-->"], output)
232 self._run_check(["<!--abc-", "->"], output)
233 self._run_check(["<!--abc--", ">"], output)
234 self._run_check(["<!--abc-->", ""], output)
235
Fred Drake84bb9d82001-08-03 19:53:01 +0000236 def test_starttag_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000237 self._parse_error("</>")
238 self._parse_error("</$>")
239 self._parse_error("</")
240 self._parse_error("</a")
Fred Drakebd3090d2001-05-18 15:32:59 +0000241 self._parse_error("<a<a>")
242 self._parse_error("</a<a>")
Fred Drakebd3090d2001-05-18 15:32:59 +0000243 self._parse_error("<!")
Fred Drakebd3090d2001-05-18 15:32:59 +0000244 self._parse_error("<a")
245 self._parse_error("<a foo='bar'")
246 self._parse_error("<a foo='bar")
247 self._parse_error("<a foo='>'")
248 self._parse_error("<a foo='>")
Fred Drakebd3090d2001-05-18 15:32:59 +0000249
Fred Drake84bb9d82001-08-03 19:53:01 +0000250 def test_declaration_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000251 self._parse_error("<!DOCTYPE foo $ >")
252
Fred Drake84bb9d82001-08-03 19:53:01 +0000253 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000254 self._run_check("<p/>", [
255 ("startendtag", "p", []),
256 ])
257 self._run_check("<p></p>", [
258 ("starttag", "p", []),
259 ("endtag", "p"),
260 ])
261 self._run_check("<p><img src='foo' /></p>", [
262 ("starttag", "p", []),
263 ("startendtag", "img", [("src", "foo")]),
264 ("endtag", "p"),
265 ])
266
Fred Drake84bb9d82001-08-03 19:53:01 +0000267 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000268 s = """<foo:bar \n one="1"\ttwo=2 >"""
269 self._run_check_extra(s, [
270 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
271 ("starttag_text", s)])
272
Fred Drake84bb9d82001-08-03 19:53:01 +0000273 def test_cdata_content(self):
Ezio Melotti7de56f62011-11-01 14:12:22 +0200274 contents = [
275 '<!-- not a comment --> &not-an-entity-ref;',
276 "<not a='start tag'>",
277 '<a href="" /> <p> <span></span>',
278 'foo = "</scr" + "ipt>";',
279 'foo = "</SCRIPT" + ">";',
280 'foo = <\n/script> ',
281 '<!-- document.write("</scr" + "ipt>"); -->',
282 ('\n//<![CDATA[\n'
283 'document.write(\'<s\'+\'cript type="text/javascript" '
284 'src="http://www.example.org/r=\'+new '
285 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
286 '\n<!-- //\nvar foo = 3.14;\n// -->\n',
287 'foo = "</sty" + "le>";',
288 '<!-- \u2603 -->',
289 # these two should be invalid according to the HTML 5 spec,
290 # section 8.1.2.2
291 #'foo = </\nscript>',
292 #'foo = </ script>',
293 ]
294 elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style']
295 for content in contents:
296 for element in elements:
297 element_lower = element.lower()
298 s = '<{element}>{content}</{element}>'.format(element=element,
299 content=content)
300 self._run_check(s, [("starttag", element_lower, []),
301 ("data", content),
302 ("endtag", element_lower)])
303
Fred Drakebd3090d2001-05-18 15:32:59 +0000304
305
Ezio Melottic1e73c32011-11-01 18:57:15 +0200306class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
R. David Murrayb579dba2010-12-03 04:06:39 +0000307
Ezio Melottib9a48f72011-11-01 15:00:59 +0200308 def get_collector(self):
309 return EventCollector(strict=False)
R. David Murrayb579dba2010-12-03 04:06:39 +0000310
311 def test_tolerant_parsing(self):
312 self._run_check('<html <html>te>>xt&a<<bc</a></html>\n'
313 '<img src="URL><//img></html</html>', [
Ezio Melottic2fe5772011-11-14 18:53:33 +0200314 ('starttag', 'html', [('<html', None)]),
315 ('data', 'te>>xt'),
316 ('entityref', 'a'),
317 ('data', '<<bc'),
318 ('endtag', 'a'),
319 ('endtag', 'html'),
320 ('data', '\n<img src="URL><//img></html'),
321 ('endtag', 'html')])
R. David Murrayb579dba2010-12-03 04:06:39 +0000322
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200323 def test_with_unquoted_attributes(self):
Ezio Melottib9a48f72011-11-01 15:00:59 +0200324 # see #12008
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200325 html = ("<html><body bgcolor=d0ca90 text='181008'>"
326 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
327 "<td align=left><font size=-1>"
328 "- <a href=/rabota/><span class=en> software-and-i</span></a>"
329 "- <a href='/1/'><span class=en> library</span></a></table>")
330 expected = [
331 ('starttag', 'html', []),
332 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
333 ('starttag', 'table',
334 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
335 ('starttag', 'tr', []),
336 ('starttag', 'td', [('align', 'left')]),
337 ('starttag', 'font', [('size', '-1')]),
338 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
339 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
340 ('endtag', 'span'), ('endtag', 'a'),
341 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
342 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
343 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
344 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200345 self._run_check(html, expected)
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200346
R. David Murrayb579dba2010-12-03 04:06:39 +0000347 def test_comma_between_attributes(self):
348 self._run_check('<form action="/xxx.php?a=1&amp;b=2&amp", '
349 'method="post">', [
350 ('starttag', 'form',
351 [('action', '/xxx.php?a=1&b=2&amp'),
Ezio Melottic2fe5772011-11-14 18:53:33 +0200352 (',', None), ('method', 'post')])])
R. David Murrayb579dba2010-12-03 04:06:39 +0000353
354 def test_weird_chars_in_unquoted_attribute_values(self):
355 self._run_check('<form action=bogus|&#()value>', [
356 ('starttag', 'form',
Ezio Melottic1e73c32011-11-01 18:57:15 +0200357 [('action', 'bogus|&#()value')])])
R. David Murrayb579dba2010-12-03 04:06:39 +0000358
Ezio Melottib9a48f72011-11-01 15:00:59 +0200359 def test_correct_detection_of_start_tags(self):
360 # see #13273
Ezio Melottif50ffa92011-10-28 13:21:09 +0300361 html = ('<div style="" ><b>The <a href="some_url">rain</a> '
362 '<br /> in <span>Spain</span></b></div>')
363 expected = [
364 ('starttag', 'div', [('style', '')]),
365 ('starttag', 'b', []),
366 ('data', 'The '),
367 ('starttag', 'a', [('href', 'some_url')]),
368 ('data', 'rain'),
369 ('endtag', 'a'),
370 ('data', ' '),
371 ('startendtag', 'br', []),
372 ('data', ' in '),
373 ('starttag', 'span', []),
374 ('data', 'Spain'),
375 ('endtag', 'span'),
376 ('endtag', 'b'),
377 ('endtag', 'div')
378 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200379 self._run_check(html, expected)
Ezio Melottif50ffa92011-10-28 13:21:09 +0300380
Ezio Melottif50ffa92011-10-28 13:21:09 +0300381 html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>'
382 expected = [
Ezio Melottic2fe5772011-11-14 18:53:33 +0200383 ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]),
Ezio Melottif50ffa92011-10-28 13:21:09 +0300384 ('starttag', 'b', []),
385 ('data', 'The '),
386 ('starttag', 'a', [('href', 'some_url')]),
387 ('data', 'rain'),
388 ('endtag', 'a'),
389 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200390 self._run_check(html, expected)
Ezio Melottif50ffa92011-10-28 13:21:09 +0300391
Senthil Kumaran164540f2010-12-28 15:55:16 +0000392 def test_unescape_function(self):
393 p = html.parser.HTMLParser()
394 self.assertEqual(p.unescape('&#bad;'),'&#bad;')
395 self.assertEqual(p.unescape('&#0038;'),'&')
Ezio Melottid9e0b062011-09-05 17:11:06 +0300396 # see #12888
397 self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
R. David Murrayb579dba2010-12-03 04:06:39 +0000398
Ezio Melottic1e73c32011-11-01 18:57:15 +0200399
Ezio Melottib245ed12011-11-14 18:13:22 +0200400class AttributesStrictTestCase(TestCaseBase):
401
402 def get_collector(self):
403 return EventCollector(strict=True)
404
405 def test_attr_syntax(self):
406 output = [
407 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
408 ]
409 self._run_check("""<a b='v' c="v" d=v e>""", output)
410 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
411 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
412 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
413
414 def test_attr_values(self):
415 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
416 [("starttag", "a", [("b", "xxx\n\txxx"),
417 ("c", "yyy\t\nyyy"),
418 ("d", "\txyz\n")])])
419 self._run_check("""<a b='' c="">""",
420 [("starttag", "a", [("b", ""), ("c", "")])])
421 # Regression test for SF patch #669683.
422 self._run_check("<e a=rgb(1,2,3)>",
423 [("starttag", "e", [("a", "rgb(1,2,3)")])])
424 # Regression test for SF bug #921657.
425 self._run_check(
426 "<a href=mailto:xyz@example.com>",
427 [("starttag", "a", [("href", "mailto:xyz@example.com")])])
428
429 def test_attr_nonascii(self):
430 # see issue 7311
431 self._run_check(
432 "<img src=/foo/bar.png alt=\u4e2d\u6587>",
433 [("starttag", "img", [("src", "/foo/bar.png"),
434 ("alt", "\u4e2d\u6587")])])
435 self._run_check(
436 "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
437 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
438 ("href", "\u30c6\u30b9\u30c8.html")])])
439 self._run_check(
440 '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
441 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
442 ("href", "\u30c6\u30b9\u30c8.html")])])
443
444 def test_attr_entity_replacement(self):
445 self._run_check(
446 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
447 [("starttag", "a", [("b", "&><\"'")])])
448
449 def test_attr_funky_names(self):
450 self._run_check(
451 "<a a.b='v' c:d=v e-f=v>",
452 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
453
454 def test_entityrefs_in_attributes(self):
455 self._run_check(
456 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
457 [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])])
458
459
Ezio Melottic2fe5772011-11-14 18:53:33 +0200460
461class AttributesTolerantTestCase(AttributesStrictTestCase):
462
463 def get_collector(self):
464 return EventCollector(strict=False)
465
466 def test_attr_funky_names2(self):
467 self._run_check(
468 "<a $><b $=%><c \=/>",
469 [("starttag", "a", [("$", None)]),
470 ("starttag", "b", [("$", "%")]),
471 ("starttag", "c", [("\\", "/")])])
472
473 def test_entities_in_attribute_value(self):
474 # see #1200313
475 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
476 self._run_check('<a href="%s">' % entity,
477 [("starttag", "a", [("href", "&")])])
478 self._run_check("<a href='%s'>" % entity,
479 [("starttag", "a", [("href", "&")])])
480 self._run_check("<a href=%s>" % entity,
481 [("starttag", "a", [("href", "&")])])
482
483 def test_malformed_attributes(self):
484 # see #13357
485 html = (
486 "<a href=test'style='color:red;bad1'>test - bad1</a>"
487 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
488 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
489 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
490 )
491 expected = [
492 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
493 ('data', 'test - bad1'), ('endtag', 'a'),
494 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
495 ('data', 'test - bad2'), ('endtag', 'a'),
496 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]),
497 ('data', 'test - bad3'), ('endtag', 'a'),
498 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]),
499 ('data', 'test - bad4'), ('endtag', 'a')
500 ]
501 self._run_check(html, expected)
502
503 def test_malformed_adjacent_attributes(self):
504 # see #12629
505 self._run_check('<x><y z=""o"" /></x>',
506 [('starttag', 'x', []),
507 ('startendtag', 'y', [('z', ''), ('o""', None)]),
508 ('endtag', 'x')])
509 self._run_check('<x><y z="""" /></x>',
510 [('starttag', 'x', []),
511 ('startendtag', 'y', [('z', ''), ('""', None)]),
512 ('endtag', 'x')])
513
514 # see #755670 for the following 3 tests
515 def test_adjacent_attributes(self):
516 self._run_check('<a width="100%"cellspacing=0>',
517 [("starttag", "a",
518 [("width", "100%"), ("cellspacing","0")])])
519
520 self._run_check('<a id="foo"class="bar">',
521 [("starttag", "a",
522 [("id", "foo"), ("class","bar")])])
523
524 def test_missing_attribute_value(self):
525 self._run_check('<a v=>',
526 [("starttag", "a", [("v", "")])])
527
528 def test_javascript_attribute_value(self):
529 self._run_check("<a href=javascript:popup('/popup/help.html')>",
530 [("starttag", "a",
531 [("href", "javascript:popup('/popup/help.html')")])])
532
533 def test_end_tag_in_attribute_value(self):
534 # see #1745761
535 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
536 [("starttag", "a",
537 [("href", "http://www.example.org/\">;")]),
538 ("data", "spam"), ("endtag", "a")])
539
540
541
Fred Drakee8220492001-09-24 20:19:08 +0000542def test_main():
Ezio Melottib245ed12011-11-14 18:13:22 +0200543 support.run_unittest(HTMLParserStrictTestCase, HTMLParserTolerantTestCase,
Ezio Melottic2fe5772011-11-14 18:53:33 +0200544 AttributesStrictTestCase, AttributesTolerantTestCase)
Fred Drakee8220492001-09-24 20:19:08 +0000545
546
547if __name__ == "__main__":
548 test_main()