blob: 87b5060611d3bfe9213c109bba7262357efcb6ea [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
Ezio Melotti15cb4892011-11-18 18:01:49 +0200304 def test_cdata_with_closing_tags(self):
305 # see issue #13358
306 # make sure that HTMLParser calls handle_data only once for each CDATA.
307 # The normal event collector normalizes the events in get_events,
308 # so we override it to return the original list of events.
309 class Collector(EventCollector):
310 def get_events(self):
311 return self.events
Fred Drakebd3090d2001-05-18 15:32:59 +0000312
Ezio Melotti15cb4892011-11-18 18:01:49 +0200313 content = """<!-- not a comment --> &not-an-entity-ref;
314 <a href="" /> </p><p> <span></span></style>
315 '</script' + '>'"""
316 for element in [' script', 'script ', ' script ',
317 '\nscript', 'script\n', '\nscript\n']:
318 element_lower = element.lower().strip()
319 s = '<script>{content}</{element}>'.format(element=element,
320 content=content)
321 self._run_check(s, [("starttag", element_lower, []),
322 ("data", content),
323 ("endtag", element_lower)],
324 collector=Collector())
Fred Drakebd3090d2001-05-18 15:32:59 +0000325
Ezio Melottic1e73c32011-11-01 18:57:15 +0200326class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
R. David Murrayb579dba2010-12-03 04:06:39 +0000327
Ezio Melottib9a48f72011-11-01 15:00:59 +0200328 def get_collector(self):
329 return EventCollector(strict=False)
R. David Murrayb579dba2010-12-03 04:06:39 +0000330
331 def test_tolerant_parsing(self):
332 self._run_check('<html <html>te>>xt&a<<bc</a></html>\n'
333 '<img src="URL><//img></html</html>', [
Ezio Melottic2fe5772011-11-14 18:53:33 +0200334 ('starttag', 'html', [('<html', None)]),
335 ('data', 'te>>xt'),
336 ('entityref', 'a'),
337 ('data', '<<bc'),
338 ('endtag', 'a'),
339 ('endtag', 'html'),
340 ('data', '\n<img src="URL><//img></html'),
341 ('endtag', 'html')])
R. David Murrayb579dba2010-12-03 04:06:39 +0000342
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200343 def test_with_unquoted_attributes(self):
Ezio Melottib9a48f72011-11-01 15:00:59 +0200344 # see #12008
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200345 html = ("<html><body bgcolor=d0ca90 text='181008'>"
346 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
347 "<td align=left><font size=-1>"
348 "- <a href=/rabota/><span class=en> software-and-i</span></a>"
349 "- <a href='/1/'><span class=en> library</span></a></table>")
350 expected = [
351 ('starttag', 'html', []),
352 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
353 ('starttag', 'table',
354 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
355 ('starttag', 'tr', []),
356 ('starttag', 'td', [('align', 'left')]),
357 ('starttag', 'font', [('size', '-1')]),
358 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
359 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
360 ('endtag', 'span'), ('endtag', 'a'),
361 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
362 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
363 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
364 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200365 self._run_check(html, expected)
Ezio Melotti18b0e5b2011-11-01 14:42:54 +0200366
R. David Murrayb579dba2010-12-03 04:06:39 +0000367 def test_comma_between_attributes(self):
368 self._run_check('<form action="/xxx.php?a=1&amp;b=2&amp", '
369 'method="post">', [
370 ('starttag', 'form',
371 [('action', '/xxx.php?a=1&b=2&amp'),
Ezio Melottic2fe5772011-11-14 18:53:33 +0200372 (',', None), ('method', 'post')])])
R. David Murrayb579dba2010-12-03 04:06:39 +0000373
374 def test_weird_chars_in_unquoted_attribute_values(self):
375 self._run_check('<form action=bogus|&#()value>', [
376 ('starttag', 'form',
Ezio Melottic1e73c32011-11-01 18:57:15 +0200377 [('action', 'bogus|&#()value')])])
R. David Murrayb579dba2010-12-03 04:06:39 +0000378
Ezio Melottib9a48f72011-11-01 15:00:59 +0200379 def test_correct_detection_of_start_tags(self):
380 # see #13273
Ezio Melottif50ffa92011-10-28 13:21:09 +0300381 html = ('<div style="" ><b>The <a href="some_url">rain</a> '
382 '<br /> in <span>Spain</span></b></div>')
383 expected = [
384 ('starttag', 'div', [('style', '')]),
385 ('starttag', 'b', []),
386 ('data', 'The '),
387 ('starttag', 'a', [('href', 'some_url')]),
388 ('data', 'rain'),
389 ('endtag', 'a'),
390 ('data', ' '),
391 ('startendtag', 'br', []),
392 ('data', ' in '),
393 ('starttag', 'span', []),
394 ('data', 'Spain'),
395 ('endtag', 'span'),
396 ('endtag', 'b'),
397 ('endtag', 'div')
398 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200399 self._run_check(html, expected)
Ezio Melottif50ffa92011-10-28 13:21:09 +0300400
Ezio Melottif50ffa92011-10-28 13:21:09 +0300401 html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>'
402 expected = [
Ezio Melottic2fe5772011-11-14 18:53:33 +0200403 ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]),
Ezio Melottif50ffa92011-10-28 13:21:09 +0300404 ('starttag', 'b', []),
405 ('data', 'The '),
406 ('starttag', 'a', [('href', 'some_url')]),
407 ('data', 'rain'),
408 ('endtag', 'a'),
409 ]
Ezio Melottic1e73c32011-11-01 18:57:15 +0200410 self._run_check(html, expected)
Ezio Melottif50ffa92011-10-28 13:21:09 +0300411
Senthil Kumaran164540f2010-12-28 15:55:16 +0000412 def test_unescape_function(self):
413 p = html.parser.HTMLParser()
414 self.assertEqual(p.unescape('&#bad;'),'&#bad;')
415 self.assertEqual(p.unescape('&#0038;'),'&')
Ezio Melottid9e0b062011-09-05 17:11:06 +0300416 # see #12888
417 self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
R. David Murrayb579dba2010-12-03 04:06:39 +0000418
Ezio Melottic1e73c32011-11-01 18:57:15 +0200419
Ezio Melottib245ed12011-11-14 18:13:22 +0200420class AttributesStrictTestCase(TestCaseBase):
421
422 def get_collector(self):
423 return EventCollector(strict=True)
424
425 def test_attr_syntax(self):
426 output = [
427 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
428 ]
429 self._run_check("""<a b='v' c="v" d=v e>""", output)
430 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
431 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
432 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
433
434 def test_attr_values(self):
435 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
436 [("starttag", "a", [("b", "xxx\n\txxx"),
437 ("c", "yyy\t\nyyy"),
438 ("d", "\txyz\n")])])
439 self._run_check("""<a b='' c="">""",
440 [("starttag", "a", [("b", ""), ("c", "")])])
441 # Regression test for SF patch #669683.
442 self._run_check("<e a=rgb(1,2,3)>",
443 [("starttag", "e", [("a", "rgb(1,2,3)")])])
444 # Regression test for SF bug #921657.
445 self._run_check(
446 "<a href=mailto:xyz@example.com>",
447 [("starttag", "a", [("href", "mailto:xyz@example.com")])])
448
449 def test_attr_nonascii(self):
450 # see issue 7311
451 self._run_check(
452 "<img src=/foo/bar.png alt=\u4e2d\u6587>",
453 [("starttag", "img", [("src", "/foo/bar.png"),
454 ("alt", "\u4e2d\u6587")])])
455 self._run_check(
456 "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
457 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
458 ("href", "\u30c6\u30b9\u30c8.html")])])
459 self._run_check(
460 '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
461 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
462 ("href", "\u30c6\u30b9\u30c8.html")])])
463
464 def test_attr_entity_replacement(self):
465 self._run_check(
466 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
467 [("starttag", "a", [("b", "&><\"'")])])
468
469 def test_attr_funky_names(self):
470 self._run_check(
471 "<a a.b='v' c:d=v e-f=v>",
472 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
473
474 def test_entityrefs_in_attributes(self):
475 self._run_check(
476 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
477 [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])])
478
479
Ezio Melottic2fe5772011-11-14 18:53:33 +0200480
481class AttributesTolerantTestCase(AttributesStrictTestCase):
482
483 def get_collector(self):
484 return EventCollector(strict=False)
485
486 def test_attr_funky_names2(self):
487 self._run_check(
488 "<a $><b $=%><c \=/>",
489 [("starttag", "a", [("$", None)]),
490 ("starttag", "b", [("$", "%")]),
491 ("starttag", "c", [("\\", "/")])])
492
493 def test_entities_in_attribute_value(self):
494 # see #1200313
495 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
496 self._run_check('<a href="%s">' % entity,
497 [("starttag", "a", [("href", "&")])])
498 self._run_check("<a href='%s'>" % entity,
499 [("starttag", "a", [("href", "&")])])
500 self._run_check("<a href=%s>" % entity,
501 [("starttag", "a", [("href", "&")])])
502
503 def test_malformed_attributes(self):
504 # see #13357
505 html = (
506 "<a href=test'style='color:red;bad1'>test - bad1</a>"
507 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
508 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
509 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
510 )
511 expected = [
512 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
513 ('data', 'test - bad1'), ('endtag', 'a'),
514 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
515 ('data', 'test - bad2'), ('endtag', 'a'),
516 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]),
517 ('data', 'test - bad3'), ('endtag', 'a'),
518 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]),
519 ('data', 'test - bad4'), ('endtag', 'a')
520 ]
521 self._run_check(html, expected)
522
523 def test_malformed_adjacent_attributes(self):
524 # see #12629
525 self._run_check('<x><y z=""o"" /></x>',
526 [('starttag', 'x', []),
527 ('startendtag', 'y', [('z', ''), ('o""', None)]),
528 ('endtag', 'x')])
529 self._run_check('<x><y z="""" /></x>',
530 [('starttag', 'x', []),
531 ('startendtag', 'y', [('z', ''), ('""', None)]),
532 ('endtag', 'x')])
533
534 # see #755670 for the following 3 tests
535 def test_adjacent_attributes(self):
536 self._run_check('<a width="100%"cellspacing=0>',
537 [("starttag", "a",
538 [("width", "100%"), ("cellspacing","0")])])
539
540 self._run_check('<a id="foo"class="bar">',
541 [("starttag", "a",
542 [("id", "foo"), ("class","bar")])])
543
544 def test_missing_attribute_value(self):
545 self._run_check('<a v=>',
546 [("starttag", "a", [("v", "")])])
547
548 def test_javascript_attribute_value(self):
549 self._run_check("<a href=javascript:popup('/popup/help.html')>",
550 [("starttag", "a",
551 [("href", "javascript:popup('/popup/help.html')")])])
552
553 def test_end_tag_in_attribute_value(self):
554 # see #1745761
555 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
556 [("starttag", "a",
557 [("href", "http://www.example.org/\">;")]),
558 ("data", "spam"), ("endtag", "a")])
559
560
561
Fred Drakee8220492001-09-24 20:19:08 +0000562def test_main():
Ezio Melottib245ed12011-11-14 18:13:22 +0200563 support.run_unittest(HTMLParserStrictTestCase, HTMLParserTolerantTestCase,
Ezio Melottic2fe5772011-11-14 18:53:33 +0200564 AttributesStrictTestCase, AttributesTolerantTestCase)
Fred Drakee8220492001-09-24 20:19:08 +0000565
566
567if __name__ == "__main__":
568 test_main()