Debian Codegraph @

Lib/test/test_htmlparser.py

raw
1 """Tests for HTMLParser.py."""
2
3 import html.parser
4 import pprint
5 import unittest
6
7 from unittest.mock import patch
8 from test import support
9
10
11 SAMPLE_RCDATA = (
12 '<!-- not a comment -->'
13 "<not a='start tag'>"
14 '<![CDATA[not a cdata]]>'
15 '<!not a bogus comment>'
16 '</not a bogus comment>'
17 '\u2603'
18 )
19
20 SAMPLE_RAWTEXT = SAMPLE_RCDATA + '&amp;&#9786;'
21
22
23 class EventCollector(html.parser.HTMLParser):
24
25 def __init__(self, *args, autocdata=False, **kw):
26 self.autocdata = autocdata
27 self.events = []
28 self.append = self.events.append
29 html.parser.HTMLParser.__init__(self, *args, **kw)
30 if autocdata:
31 self._set_support_cdata(False)
32
33 def get_events(self):
34 # Normalize the list of events so that buffer artefacts don't
35 # separate runs of contiguous characters.
36 L = []
37 prevtype = None
38 for event in self.events:
39 type = event[0]
40 if type == prevtype == "data":
41 L[-1] = ("data", L[-1][1] + event[1])
42 else:
43 L.append(event)
44 prevtype = type
45 self.events = L
46 return L
47
48 # structure markup
49
50 def handle_starttag(self, tag, attrs):
51 self.append(("starttag", tag, attrs))
52 if self.autocdata and tag == 'svg':
53 self._set_support_cdata(True)
54
55 def handle_startendtag(self, tag, attrs):
56 self.append(("startendtag", tag, attrs))
57
58 def handle_endtag(self, tag):
59 self.append(("endtag", tag))
60 if self.autocdata and tag == 'svg':
61 self._set_support_cdata(False)
62
63 # all other markup
64
65 def handle_comment(self, data):
66 self.append(("comment", data))
67
68 def handle_charref(self, data):
69 self.append(("charref", data))
70
71 def handle_data(self, data):
72 self.append(("data", data))
73
74 def handle_decl(self, data):
75 self.append(("decl", data))
76
77 def handle_entityref(self, data):
78 self.append(("entityref", data))
79
80 def handle_pi(self, data):
81 self.append(("pi", data))
82
83 def unknown_decl(self, decl):
84 self.append(("unknown decl", decl))
85
86
87 class EventCollectorExtra(EventCollector):
88
89 def handle_starttag(self, tag, attrs):
90 EventCollector.handle_starttag(self, tag, attrs)
91 self.append(("starttag_text", self.get_starttag_text()))
92
93
94 class EventCollectorCharrefs(EventCollector):
95
96 def handle_charref(self, data):
97 self.fail('This should never be called with convert_charrefs=True')
98
99 def handle_entityref(self, data):
100 self.fail('This should never be called with convert_charrefs=True')
101
102
103 # The normal event collector normalizes the events in get_events,
104 # so we override it to return the original list of events.
105 class EventCollectorNoNormalize(EventCollector):
106 def get_events(self):
107 return self.events
108
109
110 class TestCaseBase(unittest.TestCase):
111
112 def get_collector(self, convert_charrefs=False):
113 return EventCollector(convert_charrefs=convert_charrefs)
114
115 def _run_check(self, source, expected_events,
116 *, collector=None, convert_charrefs=False):
117 if collector is None:
118 collector = self.get_collector(convert_charrefs=convert_charrefs)
119 parser = collector
120 for s in source:
121 parser.feed(s)
122 parser.close()
123 events = parser.get_events()
124 if events != expected_events:
125 self.fail("received events did not match expected events" +
126 "\nSource:\n" + repr(source) +
127 "\nExpected:\n" + pprint.pformat(expected_events) +
128 "\nReceived:\n" + pprint.pformat(events))
129
130 def _run_check_extra(self, source, events):
131 self._run_check(source, events,
132 collector=EventCollectorExtra(convert_charrefs=False))
133
134
135 class HTMLParserTestCase(TestCaseBase):
136
137 def test_processing_instruction_only(self):
138 self._run_check("<?processing instruction>", [
139 ("pi", "processing instruction"),
140 ])
141 self._run_check("<?processing instruction ?>", [
142 ("pi", "processing instruction ?"),
143 ])
144
145 def test_simple_html(self):
146 self._run_check("""
147 <!DOCTYPE html PUBLIC 'foo'>
148 <HTML>&entity;&#32;
149 <!--comment1a
150 -></foo><bar>&lt;<?pi?></foo<bar
151 comment1b-->
152 <Img sRc='Bar' isMAP>sample
153 text
154 &#x201C;
155 <!--comment2a-- --comment2b-->
156 </Html>
157 """, [
158 ("data", "\n"),
159 ("decl", "DOCTYPE html PUBLIC 'foo'"),
160 ("data", "\n"),
161 ("starttag", "html", []),
162 ("entityref", "entity"),
163 ("charref", "32"),
164 ("data", "\n"),
165 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
166 ("data", "\n"),
167 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
168 ("data", "sample\ntext\n"),
169 ("charref", "x201C"),
170 ("data", "\n"),
171 ("comment", "comment2a-- --comment2b"),
172 ("data", "\n"),
173 ("endtag", "html"),
174 ("data", "\n"),
175 ])
176
177 def test_malformatted_charref(self):
178 self._run_check("<p>&#bad;</p>", [
179 ("starttag", "p", []),
180 ("data", "&#bad;"),
181 ("endtag", "p"),
182 ])
183 # add the [] as a workaround to avoid buffering (see #20288)
184 self._run_check(["<div>&#bad;</div>"], [
185 ("starttag", "div", []),
186 ("data", "&#bad;"),
187 ("endtag", "div"),
188 ])
189
190 def test_unclosed_entityref(self):
191 self._run_check('&gt &lt;', [('entityref', 'gt'), ('data', ' '), ('entityref', 'lt')],
192 convert_charrefs=False)
193 self._run_check('&gt &lt;', [('data', '> <')], convert_charrefs=True)
194
195 self._run_check('&undefined &lt;',
196 [('entityref', 'undefined'), ('data', ' '), ('entityref', 'lt')],
197 convert_charrefs=False)
198 self._run_check('&undefined &lt;', [('data', '&undefined <')],
199 convert_charrefs=True)
200
201 self._run_check('&gtundefined &lt;',
202 [('entityref', 'gtundefined'), ('data', ' '), ('entityref', 'lt')],
203 convert_charrefs=False)
204 self._run_check('&gtundefined &lt;', [('data', '>undefined <')],
205 convert_charrefs=True)
206
207 self._run_check('& &lt;', [('data', '& '), ('entityref', 'lt')],
208 convert_charrefs=False)
209 self._run_check('& &lt;', [('data', '& <')], convert_charrefs=True)
210
211 def test_eof_in_entityref(self):
212 self._run_check('&gt', [('entityref', 'gt')], convert_charrefs=False)
213 self._run_check('&gt', [('data', '>')], convert_charrefs=True)
214
215 self._run_check('&g', [('entityref', 'g')], convert_charrefs=False)
216 self._run_check('&g', [('data', '&g')], convert_charrefs=True)
217
218 self._run_check('&undefined', [('entityref', 'undefined')],
219 convert_charrefs=False)
220 self._run_check('&undefined', [('data', '&undefined')],
221 convert_charrefs=True)
222
223 self._run_check('&gtundefined', [('entityref', 'gtundefined')],
224 convert_charrefs=False)
225 self._run_check('&gtundefined', [('data', '>undefined')],
226 convert_charrefs=True)
227
228 self._run_check('&', [('data', '&')], convert_charrefs=False)
229 self._run_check('&', [('data', '&')], convert_charrefs=True)
230
231 def test_unclosed_charref(self):
232 self._run_check('&#123 &lt;', [('charref', '123'), ('data', ' '), ('entityref', 'lt')],
233 convert_charrefs=False)
234 self._run_check('&#123 &lt;', [('data', '{ <')], convert_charrefs=True)
235 self._run_check('&#xab &lt;', [('charref', 'xab'), ('data', ' '), ('entityref', 'lt')],
236 convert_charrefs=False)
237 self._run_check('&#xab &lt;', [('data', '\xab <')], convert_charrefs=True)
238
239 self._run_check('&#123456789 &lt;',
240 [('charref', '123456789'), ('data', ' '), ('entityref', 'lt')],
241 convert_charrefs=False)
242 self._run_check('&#123456789 &lt;', [('data', '\ufffd <')],
243 convert_charrefs=True)
244 self._run_check('&#x123456789 &lt;',
245 [('charref', 'x123456789'), ('data', ' '), ('entityref', 'lt')],
246 convert_charrefs=False)
247 self._run_check('&#x123456789 &lt;', [('data', '\ufffd <')],
248 convert_charrefs=True)
249
250 self._run_check('&# &lt;', [('data', '&# '), ('entityref', 'lt')], convert_charrefs=False)
251 self._run_check('&# &lt;', [('data', '&# <')], convert_charrefs=True)
252 self._run_check('&#x &lt;', [('data', '&#x '), ('entityref', 'lt')], convert_charrefs=False)
253 self._run_check('&#x &lt;', [('data', '&#x <')], convert_charrefs=True)
254
255 def test_eof_in_charref(self):
256 self._run_check('&#123', [('charref', '123')], convert_charrefs=False)
257 self._run_check('&#123', [('data', '{')], convert_charrefs=True)
258 self._run_check('&#xab', [('charref', 'xab')], convert_charrefs=False)
259 self._run_check('&#xab', [('data', '\xab')], convert_charrefs=True)
260
261 self._run_check('&#123456789', [('charref', '123456789')],
262 convert_charrefs=False)
263 self._run_check('&#123456789', [('data', '\ufffd')], convert_charrefs=True)
264 self._run_check('&#x123456789', [('charref', 'x123456789')],
265 convert_charrefs=False)
266 self._run_check('&#x123456789', [('data', '\ufffd')], convert_charrefs=True)
267
268 self._run_check('&#', [('data', '&#')], convert_charrefs=False)
269 self._run_check('&#', [('data', '&#')], convert_charrefs=True)
270 self._run_check('&#x', [('data', '&#x')], convert_charrefs=False)
271 self._run_check('&#x', [('data', '&#x')], convert_charrefs=True)
272
273 def test_bad_nesting(self):
274 # Strangely, this *is* supposed to test that overlapping
275 # elements are allowed. HTMLParser is more geared toward
276 # lexing the input that parsing the structure.
277 self._run_check("<a><b></a></b>", [
278 ("starttag", "a", []),
279 ("starttag", "b", []),
280 ("endtag", "a"),
281 ("endtag", "b"),
282 ])
283
284 def test_bare_ampersands(self):
285 self._run_check("this text & contains & ampersands &", [
286 ("data", "this text & contains & ampersands &"),
287 ])
288
289 def test_bare_pointy_brackets(self):
290 self._run_check("this < text > contains < bare>pointy< brackets", [
291 ("data", "this < text > contains < bare>pointy< brackets"),
292 ])
293
294 def test_starttag_end_boundary(self):
295 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
296 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
297
298 def test_buffer_artefacts(self):
299 output = [("starttag", "a", [("b", "<")])]
300 self._run_check(["<a b='<'>"], output)
301 self._run_check(["<a ", "b='<'>"], output)
302 self._run_check(["<a b", "='<'>"], output)
303 self._run_check(["<a b=", "'<'>"], output)
304 self._run_check(["<a b='<", "'>"], output)
305 self._run_check(["<a b='<'", ">"], output)
306
307 output = [("starttag", "a", [("b", ">")])]
308 self._run_check(["<a b='>'>"], output)
309 self._run_check(["<a ", "b='>'>"], output)
310 self._run_check(["<a b", "='>'>"], output)
311 self._run_check(["<a b=", "'>'>"], output)
312 self._run_check(["<a b='>", "'>"], output)
313 self._run_check(["<a b='>'", ">"], output)
314
315 output = [("comment", "abc")]
316 self._run_check(["", "<!--abc-->"], output)
317 self._run_check(["<", "!--abc-->"], output)
318 self._run_check(["<!", "--abc-->"], output)
319 self._run_check(["<!-", "-abc-->"], output)
320 self._run_check(["<!--", "abc-->"], output)
321 self._run_check(["<!--a", "bc-->"], output)
322 self._run_check(["<!--ab", "c-->"], output)
323 self._run_check(["<!--abc", "-->"], output)
324 self._run_check(["<!--abc-", "->"], output)
325 self._run_check(["<!--abc--", ">"], output)
326 self._run_check(["<!--abc-->", ""], output)
327
328 def test_valid_doctypes(self):
329 # from http://www.w3.org/QA/2002/04/valid-dtd-list.html
330 dtds = ['HTML', # HTML5 doctype
331 ('HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
332 '"http://www.w3.org/TR/html4/strict.dtd"'),
333 ('HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" '
334 '"http://www.w3.org/TR/html4/loose.dtd"'),
335 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
336 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'),
337 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" '
338 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'),
339 ('math PUBLIC "-//W3C//DTD MathML 2.0//EN" '
340 '"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"'),
341 ('html PUBLIC "-//W3C//DTD '
342 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" '
343 '"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"'),
344 ('svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
345 '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"'),
346 'html PUBLIC "-//IETF//DTD HTML 2.0//EN"',
347 'html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"']
348 for dtd in dtds:
349 self._run_check("<!DOCTYPE %s>" % dtd,
350 [('decl', 'DOCTYPE ' + dtd)])
351
352 def test_startendtag(self):
353 self._run_check("<p/>", [
354 ("startendtag", "p", []),
355 ])
356 self._run_check("<p></p>", [
357 ("starttag", "p", []),
358 ("endtag", "p"),
359 ])
360 self._run_check("<p><img src='foo' /></p>", [
361 ("starttag", "p", []),
362 ("startendtag", "img", [("src", "foo")]),
363 ("endtag", "p"),
364 ])
365
366 def test_get_starttag_text(self):
367 s = """<foo:bar \n one="1"\ttwo=2 >"""
368 self._run_check_extra(s, [
369 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
370 ("starttag_text", s)])
371
372 @support.subTests('content', [
373 '<!-- not a comment --> &not-an-entity-ref;',
374 "<not a='start tag'>",
375 '<a href="" /> <p> <span></span>',
376 'foo = "</scr" + "ipt>";',
377 'foo = "</SCRIPT" + ">";',
378 'foo = <\n/script> ',
379 '<!-- document.write("</scr" + "ipt>"); -->',
380 ('\n//<![CDATA[\n'
381 'document.write(\'<s\'+\'cript type="text/javascript" '
382 'src="http://www.example.org/r=\'+new '
383 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
384 '\n<!-- //\nvar foo = 3.14;\n// -->\n',
385 '<!-- \u2603 -->',
386 ])
387 def test_script_content(self, content):
388 s = f'<script>{content}</script>'
389 self._run_check(s, [
390 ("starttag", "script", []),
391 ("data", content),
392 ("endtag", "script"),
393 ])
394
395 @support.subTests('content', [
396 'a::before { content: "<!-- not a comment -->"; }',
397 'a::before { content: "&not-an-entity-ref;"; }',
398 'a::before { content: "<not a=\'start tag\'>"; }',
399 'a::before { content: "\u2603"; }',
400 ])
401 def test_style_content(self, content):
402 s = f'<style>{content}</style>'
403 self._run_check(s, [("starttag", "style", []),
404 ("data", content),
405 ("endtag", "style")])
406
407 @support.subTests('tag', ['title', 'textarea'])
408 def test_rcdata_content(self, tag):
409 source = f"<{tag}>{SAMPLE_RCDATA}</{tag}>"
410 self._run_check(source, [
411 ("starttag", tag, []),
412 ("data", SAMPLE_RCDATA),
413 ("endtag", tag),
414 ])
415 source = f"<{tag}>&amp;</{tag}>"
416 self._run_check(source, [
417 ("starttag", tag, []),
418 ('entityref', 'amp'),
419 ("endtag", tag),
420 ])
421
422 @support.subTests('tag',
423 ['style', 'xmp', 'iframe', 'noembed', 'noframes', 'script'])
424 def test_rawtext_content(self, tag):
425 source = f"<{tag}>{SAMPLE_RAWTEXT}</{tag}>"
426 self._run_check(source, [
427 ("starttag", tag, []),
428 ("data", SAMPLE_RAWTEXT),
429 ("endtag", tag),
430 ])
431
432 def test_noscript_content(self):
433 source = f"<noscript>{SAMPLE_RAWTEXT}</noscript>"
434 # scripting=False -- normal mode
435 self._run_check(source, [
436 ('starttag', 'noscript', []),
437 ('comment', ' not a comment '),
438 ('starttag', 'not', [('a', 'start tag')]),
439 ('unknown decl', 'CDATA[not a cdata'),
440 ('comment', 'not a bogus comment'),
441 ('endtag', 'not'),
442 ('data', '☃'),
443 ('entityref', 'amp'),
444 ('charref', '9786'),
445 ('endtag', 'noscript'),
446 ])
447 # scripting=True -- RAWTEXT mode
448 self._run_check(source, [
449 ("starttag", "noscript", []),
450 ("data", SAMPLE_RAWTEXT),
451 ("endtag", "noscript"),
452 ], collector=EventCollector(scripting=True))
453
454 def test_plaintext_content(self):
455 content = SAMPLE_RAWTEXT + '</plaintext>' # not closing
456 source = f"<plaintext>{content}"
457 self._run_check(source, [
458 ("starttag", "plaintext", []),
459 ("data", content),
460 ])
461
462 @support.subTests('endtag', ['script', 'SCRIPT', 'script ', 'script\n',
463 'script/', 'script foo=bar', 'script foo=">"'])
464 def test_script_closing_tag(self, endtag):
465 # see issue #13358
466 # make sure that HTMLParser calls handle_data only once for each CDATA.
467 content = """<!-- not a comment --> &not-an-entity-ref;
468 <a href="" /> </p><p> <span></span></style>
469 '</script' + '>'"""
470 s = f'<ScrIPt>{content}</{endtag}>'
471 self._run_check(s, [("starttag", "script", []),
472 ("data", content),
473 ("endtag", "script")],
474 collector=EventCollectorNoNormalize(convert_charrefs=False))
475
476 @support.subTests('tag', [
477 'script', 'style', 'xmp', 'iframe', 'noembed', 'noframes',
478 'textarea', 'title', 'noscript',
479 ])
480 def test_closing_tag(self, tag):
481 for endtag in [tag, tag.upper(), f'{tag} ', f'{tag}\n',
482 f'{tag}/', f'{tag} foo=bar', f'{tag} foo=">"']:
483 content = "<!-- not a comment --><i>Spam</i>"
484 s = f'<{tag.upper()}>{content}</{endtag}>'
485 self._run_check(s, [
486 ("starttag", tag, []),
487 ('data', content),
488 ("endtag", tag),
489 ], collector=EventCollectorNoNormalize(convert_charrefs=False, scripting=True))
490
491 @support.subTests('tag', [
492 'script', 'style', 'xmp', 'iframe', 'noembed', 'noframes',
493 'textarea', 'title', 'noscript',
494 ])
495 def test_invalid_closing_tag(self, tag):
496 content = (
497 f'< /{tag}>'
498 f'</ {tag}>'
499 f'</{tag}x>'
500 f'</{tag}\v>'
501 f'</{tag}\xa0>'
502 )
503 source = f"<{tag}>{content}</{tag}>"
504 self._run_check(source, [
505 ("starttag", tag, []),
506 ("data", content),
507 ("endtag", tag),
508 ], collector=EventCollector(convert_charrefs=False, scripting=True))
509
510 @support.subTests('tag,endtag', [
511 ('title', 'tıtle'),
512 ('style', 'ſtyle'),
513 ('style', 'ſtyle'),
514 ('style', 'style'),
515 ('iframe', 'ıframe'),
516 ('noframes', 'noframeſ'),
517 ('noscript', 'noſcript'),
518 ('noscript', 'noscrıpt'),
519 ('script', 'ſcript'),
520 ('script', 'scrıpt'),
521 ])
522 def test_invalid_nonascii_closing_tag(self, tag, endtag):
523 content = f"<br></{endtag}>"
524 source = f"<{tag}>{content}"
525 self._run_check(source, [
526 ("starttag", tag, []),
527 ("data", content),
528 ], collector=EventCollector(convert_charrefs=False, scripting=True))
529 source = f"<{tag}>{content}</{tag}>"
530 self._run_check(source, [
531 ("starttag", tag, []),
532 ("data", content),
533 ("endtag", tag),
534 ], collector=EventCollector(convert_charrefs=False, scripting=True))
535
536 @support.subTests('tail,end', [
537 ('', False),
538 ('<', False),
539 ('</', False),
540 ('</s', False),
541 ('</script', False),
542 ('</script ', True),
543 ('</script foo=bar', True),
544 ('</script foo=">', True),
545 ])
546 def test_eof_in_script(self, tail, end):
547 content = "a = 123"
548 s = f'<ScrIPt>{content}{tail}'
549 self._run_check(s, [("starttag", "script", []),
550 ("data", content if end else content + tail)],
551 collector=EventCollectorNoNormalize(convert_charrefs=False))
552
553 @support.subTests('tail,end', [
554 ('', False),
555 ('<', False),
556 ('</', False),
557 ('</t', False),
558 ('</title', False),
559 ('</title ', True),
560 ('</title foo=bar', True),
561 ('</title foo=">', True),
562 ])
563 def test_eof_in_title(self, tail, end):
564 s = f'<TitLe>Egg &amp; Spam{tail}'
565 self._run_check(s, [("starttag", "title", []),
566 ("data", "Egg & Spam" + ('' if end else tail))],
567 collector=EventCollectorNoNormalize(convert_charrefs=True))
568 self._run_check(s, [("starttag", "title", []),
569 ('data', 'Egg '),
570 ('entityref', 'amp'),
571 ('data', ' Spam' + ('' if end else tail))],
572 collector=EventCollectorNoNormalize(convert_charrefs=False))
573
574 def test_comments(self):
575 html = ("<!-- I'm a valid comment -->"
576 '<!--me too!-->'
577 '<!------>'
578 '<!----->'
579 '<!---->'
580 # abrupt-closing-of-empty-comment
581 '<!--->'
582 '<!-->'
583 '<!----I have many hyphens---->'
584 '<!-- I have a > in the middle -->'
585 '<!-- and I have -- in the middle! -->'
586 '<!--incorrectly-closed-comment--!>'
587 '<!----!>'
588 '<!----!-->'
589 '<!---- >-->'
590 '<!---!>-->'
591 '<!--!>-->'
592 # nested-comment
593 '<!-- <!-- nested --> -->'
594 '<!--<!-->'
595 '<!--<!--!>'
596 )
597 expected = [('comment', " I'm a valid comment "),
598 ('comment', 'me too!'),
599 ('comment', '--'),
600 ('comment', '-'),
601 ('comment', ''),
602 ('comment', ''),
603 ('comment', ''),
604 ('comment', '--I have many hyphens--'),
605 ('comment', ' I have a > in the middle '),
606 ('comment', ' and I have -- in the middle! '),
607 ('comment', 'incorrectly-closed-comment'),
608 ('comment', ''),
609 ('comment', '--!'),
610 ('comment', '-- >'),
611 ('comment', '-!>'),
612 ('comment', '!>'),
613 ('comment', ' <!-- nested '), ('data', ' -->'),
614 ('comment', '<!'),
615 ('comment', '<!'),
616 ]
617 self._run_check(html, expected)
618
619 def test_condcoms(self):
620 html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
621 '<!--[if IE 8]>condcoms<![endif]-->'
622 '<!--[if lte IE 7]>pretty?<![endif]-->')
623 expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"),
624 ('comment', '[if IE 8]>condcoms<![endif]'),
625 ('comment', '[if lte IE 7]>pretty?<![endif]')]
626 self._run_check(html, expected)
627
628 def test_convert_charrefs(self):
629 # default value for convert_charrefs is now True
630 collector = lambda: EventCollectorCharrefs()
631 self.assertTrue(collector().convert_charrefs)
632 charrefs = ['&quot;', '&#34;', '&#x22;', '&quot', '&#34', '&#x22']
633 # check charrefs in the middle of the text
634 expected = [('starttag', 'a', []), ('data', 'a"z'), ('endtag', 'a')]
635 for charref in charrefs:
636 self._run_check('<a>a{0}z</a>'.format(charref),
637 expected, collector=collector())
638 # check charrefs at the beginning/end of the text
639 expected = [('data', '"'), ('starttag', 'a', []),
640 ('data', '"'), ('endtag', 'a'), ('data', '"')]
641 for charref in charrefs:
642 self._run_check('{0}<a>'
643 '{0}</a>{0}'.format(charref),
644 expected, collector=collector())
645 # check charrefs in <script>/<style> elements
646 for charref in charrefs:
647 text = 'X'.join([charref]*3)
648 expected = [('data', '"'),
649 ('starttag', 'script', []), ('data', text),
650 ('endtag', 'script'), ('data', '"'),
651 ('starttag', 'style', []), ('data', text),
652 ('endtag', 'style'), ('data', '"')]
653 self._run_check('{1}<script>{0}</script>{1}'
654 '<style>{0}</style>{1}'.format(text, charref),
655 expected, collector=collector())
656 # check truncated charrefs at the end of the file
657 html = '&quo &# &#x'
658 for x in range(1, len(html)):
659 self._run_check(html[:x], [('data', html[:x])],
660 collector=collector())
661 # check a string with no charrefs
662 self._run_check('no charrefs here', [('data', 'no charrefs here')],
663 collector=collector())
664
665 def test_convert_charrefs_in_attribute_values(self):
666 # default value for convert_charrefs is now True
667 collector = lambda: EventCollectorCharrefs()
668 self.assertTrue(collector().convert_charrefs)
669
670 # always unescape terminated entity refs, numeric and hex char refs:
671 # - regardless whether they are at start, middle, end of attribute
672 # - or followed by alphanumeric, non-alphanumeric, or equals char
673 charrefs = ['&cent;', '&#xa2;', '&#xa2', '&#162;', '&#162']
674 expected = [('starttag', 'a',
675 [('x', '¢'), ('x', 'z¢'), ('x', '¢z'),
676 ('x', 'z¢z'), ('x', '¢ z'), ('x', '¢=z')]),
677 ('endtag', 'a')]
678 for charref in charrefs:
679 self._run_check('<a x="{0}" x="z{0}" x="{0}z" '
680 ' x="z{0}z" x="{0} z" x="{0}=z"></a>'
681 .format(charref), expected, collector=collector())
682
683 # only unescape unterminated entity matches if they are not followed by
684 # an alphanumeric or an equals sign
685 charref = '&cent'
686 expected = [('starttag', 'a',
687 [('x', '¢'), ('x', 'z¢'), ('x', '&centz'),
688 ('x', 'z&centz'), ('x', '¢ z'), ('x', '&cent=z')]),
689 ('endtag', 'a')]
690 self._run_check('<a x="{0}" x="z{0}" x="{0}z" '
691 ' x="z{0}z" x="{0} z" x="{0}=z"></a>'
692 .format(charref), expected, collector=collector())
693
694 # the remaining tests were for the "tolerant" parser (which is now
695 # the default), and check various kind of broken markup
696 def test_tolerant_parsing(self):
697 self._run_check('<html <html>te>>xt&a<<bc</a></html>\n'
698 '<img src="URL><//img></html</html>', [
699 ('starttag', 'html', [('<html', None)]),
700 ('data', 'te>>xt'),
701 ('entityref', 'a'),
702 ('data', '<'),
703 ('starttag', 'bc<', [('a', None)]),
704 ('endtag', 'html'),
705 ('data', '\n')])
706
707 def test_starttag_junk_chars(self):
708 self._run_check("<", [('data', '<')])
709 self._run_check("<>", [('data', '<>')])
710 self._run_check("< >", [('data', '< >')])
711 self._run_check("< ", [('data', '< ')])
712 self._run_check("</>", [])
713 self._run_check("<$>", [('data', '<$>')])
714 self._run_check("</$>", [('comment', '$')])
715 self._run_check("</", [('data', '</')])
716 self._run_check("</a", [])
717 self._run_check("</ a>", [('comment', ' a')])
718 self._run_check("</ a", [('comment', ' a')])
719 self._run_check("<a<a>", [('starttag', 'a<a', [])])
720 self._run_check("</a<a>", [('endtag', 'a<a')])
721 self._run_check("<!", [('comment', '')])
722 self._run_check("<a", [])
723 self._run_check("<a foo='bar'", [])
724 self._run_check("<a foo='bar", [])
725 self._run_check("<a foo='>'", [])
726 self._run_check("<a foo='>", [])
727 self._run_check("<a$>", [('starttag', 'a$', [])])
728 self._run_check("<a$b>", [('starttag', 'a$b', [])])
729 self._run_check("<a$b/>", [('startendtag', 'a$b', [])])
730 self._run_check("<a$b >", [('starttag', 'a$b', [])])
731 self._run_check("<a$b />", [('startendtag', 'a$b', [])])
732 self._run_check("</a$b>", [('endtag', 'a$b')])
733
734 def test_slashes_in_starttag(self):
735 self._run_check('<a foo="var"/>', [('startendtag', 'a', [('foo', 'var')])])
736 html = ('<img width=902 height=250px '
737 'src="/sites/default/files/images/homepage/foo.jpg" '
738 '/*what am I doing here*/ />')
739 expected = [(
740 'startendtag', 'img',
741 [('width', '902'), ('height', '250px'),
742 ('src', '/sites/default/files/images/homepage/foo.jpg'),
743 ('*what', None), ('am', None), ('i', None),
744 ('doing', None), ('here*', None)]
745 )]
746 self._run_check(html, expected)
747 html = ('<a / /foo/ / /=/ / /bar/ / />'
748 '<a / /foo/ / /=/ / /bar/ / >')
749 expected = [
750 ('startendtag', 'a', [('foo', None), ('=', None), ('bar', None)]),
751 ('starttag', 'a', [('foo', None), ('=', None), ('bar', None)])
752 ]
753 self._run_check(html, expected)
754 #see issue #14538
755 html = ('<meta><meta / ><meta // ><meta / / >'
756 '<meta/><meta /><meta //><meta//>')
757 expected = [
758 ('starttag', 'meta', []), ('starttag', 'meta', []),
759 ('starttag', 'meta', []), ('starttag', 'meta', []),
760 ('startendtag', 'meta', []), ('startendtag', 'meta', []),
761 ('startendtag', 'meta', []), ('startendtag', 'meta', []),
762 ]
763 self._run_check(html, expected)
764
765 def test_slashes_in_endtag(self):
766 self._run_check('</a/>', [('endtag', 'a')])
767 self._run_check('</a foo="var"/>', [('endtag', 'a')])
768
769 def test_declaration_junk_chars(self):
770 self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')])
771
772 def test_illegal_declarations(self):
773 self._run_check('<!spacer type="block" height="25">',
774 [('comment', 'spacer type="block" height="25"')])
775
776 def test_invalid_end_tags(self):
777 # A collection of broken end tags. <br> is used as separator.
778 # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state
779 # and #13993
780 html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>'
781 '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>')
782 expected = [('starttag', 'br', []),
783 # < is part of the name, / is discarded, p is an attribute
784 ('endtag', 'label<'),
785 ('starttag', 'br', []),
786 # text and attributes are discarded
787 ('endtag', 'div'),
788 ('starttag', 'br', []),
789 # comment because the first char after </ is not a-zA-Z
790 ('comment', '<h4'),
791 ('starttag', 'br', []),
792 # attributes are discarded
793 ('endtag', 'li'),
794 ('starttag', 'br', []),
795 # everything till ul (included) is discarded
796 ('endtag', 'li'),
797 ('starttag', 'br', []),
798 # </> is ignored
799 ('starttag', 'br', [])]
800 self._run_check(html, expected)
801
802 def test_broken_invalid_end_tag(self):
803 html = '<b>This</b attr=">"> confuses the parser'
804 expected = [('starttag', 'b', []),
805 ('data', 'This'),
806 ('endtag', 'b'),
807 ('data', ' confuses the parser')]
808 self._run_check(html, expected)
809
810 def test_correct_detection_of_start_tags(self):
811 # see #13273
812 html = ('<div style="" ><b>The <a href="some_url">rain</a> '
813 '<br /> in <span>Spain</span></b></div>')
814 expected = [
815 ('starttag', 'div', [('style', '')]),
816 ('starttag', 'b', []),
817 ('data', 'The '),
818 ('starttag', 'a', [('href', 'some_url')]),
819 ('data', 'rain'),
820 ('endtag', 'a'),
821 ('data', ' '),
822 ('startendtag', 'br', []),
823 ('data', ' in '),
824 ('starttag', 'span', []),
825 ('data', 'Spain'),
826 ('endtag', 'span'),
827 ('endtag', 'b'),
828 ('endtag', 'div')
829 ]
830 self._run_check(html, expected)
831
832 html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>'
833 expected = [
834 ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]),
835 ('starttag', 'b', []),
836 ('data', 'The '),
837 ('starttag', 'a', [('href', 'some_url')]),
838 ('data', 'rain'),
839 ('endtag', 'a'),
840 ]
841 self._run_check(html, expected)
842
843 def test_eof_in_comments(self):
844 data = [
845 ('<!--', [('comment', '')]),
846 ('<!---', [('comment', '')]),
847 ('<!----', [('comment', '')]),
848 ('<!-----', [('comment', '-')]),
849 ('<!------', [('comment', '--')]),
850 ('<!----!', [('comment', '')]),
851 ('<!---!', [('comment', '-!')]),
852 ('<!---!>', [('comment', '-!>')]),
853 ('<!--foo', [('comment', 'foo')]),
854 ('<!--foo-', [('comment', 'foo')]),
855 ('<!--foo--', [('comment', 'foo')]),
856 ('<!--foo--!', [('comment', 'foo')]),
857 ('<!--<!--', [('comment', '<!')]),
858 ('<!--<!--!', [('comment', '<!')]),
859 ]
860 for html, expected in data:
861 self._run_check(html, expected)
862
863 def test_eof_in_declarations(self):
864 data = [
865 ('<!', [('comment', '')]),
866 ('<!-', [('comment', '-')]),
867 ('<![', [('comment', '[')]),
868 ('<!DOCTYPE', [('decl', 'DOCTYPE')]),
869 ('<!DOCTYPE ', [('decl', 'DOCTYPE ')]),
870 ('<!DOCTYPE html', [('decl', 'DOCTYPE html')]),
871 ('<!DOCTYPE html ', [('decl', 'DOCTYPE html ')]),
872 ('<!DOCTYPE html PUBLIC', [('decl', 'DOCTYPE html PUBLIC')]),
873 ('<!DOCTYPE html PUBLIC "foo', [('decl', 'DOCTYPE html PUBLIC "foo')]),
874 ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "foo',
875 [('decl', 'DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "foo')]),
876 ]
877 for html, expected in data:
878 self._run_check(html, expected)
879
880 @support.subTests('content', ['', 'x', 'x]', 'x]]'])
881 def test_eof_in_cdata(self, content):
882 self._run_check('<![CDATA[' + content,
883 [('unknown decl', 'CDATA[' + content)])
884 self._run_check('<![CDATA[' + content,
885 [('comment', '[CDATA[' + content)],
886 collector=EventCollector(autocdata=True))
887 self._run_check('<svg><text y="100"><![CDATA[' + content,
888 [('starttag', 'svg', []),
889 ('starttag', 'text', [('y', '100')]),
890 ('unknown decl', 'CDATA[' + content)])
891
892 def test_bogus_comments(self):
893 html = ('<!ELEMENT br EMPTY>'
894 '<! not really a comment >'
895 '<! not a comment either -->'
896 '<! -- close enough -->'
897 '<!><!<-- this was an empty comment>'
898 '<!!! another bogus comment !!!>'
899 # see #32876
900 '<![with square brackets]!>'
901 '<![\nmultiline\nbogusness\n]!>'
902 '<![more brackets]-[and a hyphen]!>'
903 '<![cdata[should be uppercase]]>'
904 '<![CDATA [whitespaces are not ignored]]>'
905 '<![CDATA]]>' # required '[' after CDATA
906 )
907 expected = [
908 ('comment', 'ELEMENT br EMPTY'),
909 ('comment', ' not really a comment '),
910 ('comment', ' not a comment either --'),
911 ('comment', ' -- close enough --'),
912 ('comment', ''),
913 ('comment', '<-- this was an empty comment'),
914 ('comment', '!! another bogus comment !!!'),
915 ('comment', '[with square brackets]!'),
916 ('comment', '[\nmultiline\nbogusness\n]!'),
917 ('comment', '[more brackets]-[and a hyphen]!'),
918 ('comment', '[cdata[should be uppercase]]'),
919 ('comment', '[CDATA [whitespaces are not ignored]]'),
920 ('comment', '[CDATA]]'),
921 ]
922 self._run_check(html, expected)
923
924 def test_broken_condcoms(self):
925 # these condcoms are missing the '--' after '<!' and before the '>'
926 # and they are considered bogus comments according to
927 # "8.2.4.42. Markup declaration open state"
928 html = ('<![if !(IE)]>broken condcom<![endif]>'
929 '<![if ! IE]><link href="favicon.tiff"/><![endif]>'
930 '<![if !IE 6]><img src="firefox.png" /><![endif]>'
931 '<![if !ie 6]><b>foo</b><![endif]>'
932 '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>')
933 expected = [
934 ('comment', '[if !(IE)]'),
935 ('data', 'broken condcom'),
936 ('comment', '[endif]'),
937 ('comment', '[if ! IE]'),
938 ('startendtag', 'link', [('href', 'favicon.tiff')]),
939 ('comment', '[endif]'),
940 ('comment', '[if !IE 6]'),
941 ('startendtag', 'img', [('src', 'firefox.png')]),
942 ('comment', '[endif]'),
943 ('comment', '[if !ie 6]'),
944 ('starttag', 'b', []),
945 ('data', 'foo'),
946 ('endtag', 'b'),
947 ('comment', '[endif]'),
948 ('comment', '[if (!IE)|(lt IE 9)]'),
949 ('startendtag', 'img', [('src', 'mammoth.bmp')]),
950 ('comment', '[endif]')
951 ]
952 self._run_check(html, expected)
953
954 @support.subTests('content', [
955 'just some plain text',
956 '<!-- not a comment -->',
957 '&not-an-entity-ref;',
958 "<not a='start tag'>",
959 '',
960 '[[I have many brackets]]',
961 'I have a > in the middle',
962 'I have a ]] in the middle',
963 '] ]>',
964 ']] >',
965 ('\n'
966 ' if (a < b && a > b) {\n'
967 ' printf("[<marquee>How?</marquee>]");\n'
968 ' }\n'),
969 ])
970 def test_cdata_section_content(self, content):
971 # See "13.2.5.42 Markup declaration open state",
972 # "13.2.5.69 CDATA section state", and issue bpo-32876.
973 html = f'<svg><text y="100"><![CDATA[{content}]]></text></svg>'
974 expected = [
975 ('starttag', 'svg', []),
976 ('starttag', 'text', [('y', '100')]),
977 ('unknown decl', 'CDATA[' + content),
978 ('endtag', 'text'),
979 ('endtag', 'svg'),
980 ]
981 self._run_check(html, expected)
982 self._run_check(html, expected, collector=EventCollector(autocdata=True))
983
984 def test_cdata_section(self):
985 # See "13.2.5.42 Markup declaration open state".
986 html = ('<![CDATA[foo<br>bar]]>'
987 '<svg><text y="100"><![CDATA[foo<br>bar]]></text></svg>'
988 '<![CDATA[foo<br>bar]]>')
989 expected = [
990 ('comment', '[CDATA[foo<br'),
991 ('data', 'bar]]>'),
992 ('starttag', 'svg', []),
993 ('starttag', 'text', [('y', '100')]),
994 ('unknown decl', 'CDATA[foo<br>bar'),
995 ('endtag', 'text'),
996 ('endtag', 'svg'),
997 ('comment', '[CDATA[foo<br'),
998 ('data', 'bar]]>'),
999 ]
1000 self._run_check(html, expected, collector=EventCollector(autocdata=True))
1001
1002 def test_convert_charrefs_dropped_text(self):
1003 # #23144: make sure that all the events are triggered when
1004 # convert_charrefs is True, even if we don't call .close()
1005 parser = EventCollector(convert_charrefs=True)
1006 # before the fix, bar & baz was missing
1007 parser.feed("foo <a>link</a> bar &amp; baz")
1008 self.assertEqual(
1009 parser.get_events(),
1010 [('data', 'foo '), ('starttag', 'a', []), ('data', 'link'),
1011 ('endtag', 'a'), ('data', ' bar & baz')]
1012 )
1013
1014 @support.requires_resource('cpu')
1015 def test_eof_no_quadratic_complexity(self):
1016 # Each of these examples used to take about an hour.
1017 # Now they take a fraction of a second.
1018 def check(source):
1019 parser = html.parser.HTMLParser()
1020 parser.feed(source)
1021 parser.close()
1022 n = 120_000
1023 check("<a " * n)
1024 check("<a a=" * n)
1025 check("</a " * 14 * n)
1026 check("</a a=" * 11 * n)
1027 check("<!--" * 4 * n)
1028 check("<!" * 60 * n)
1029 check("<?" * 19 * n)
1030 check("</$" * 15 * n)
1031 check("<![CDATA[" * 9 * n)
1032 check("<!doctype" * 35 * n)
1033
1034
1035 class AttributesTestCase(TestCaseBase):
1036
1037 def test_attr_syntax(self):
1038 output = [
1039 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
1040 ]
1041 self._run_check("""<a b='v' c="v" d=v e>""", output)
1042 self._run_check("<a foo==bar>", [('starttag', 'a', [('foo', '=bar')])])
1043 self._run_check("<a foo =bar>", [('starttag', 'a', [('foo', 'bar')])])
1044 self._run_check("<a foo\t=bar>", [('starttag', 'a', [('foo', 'bar')])])
1045 self._run_check("<a foo\v=bar>", [('starttag', 'a', [('foo\v', 'bar')])])
1046 self._run_check("<a foo\xa0=bar>", [('starttag', 'a', [('foo\xa0', 'bar')])])
1047 self._run_check("<a foo= bar>", [('starttag', 'a', [('foo', 'bar')])])
1048 self._run_check("<a foo=\tbar>", [('starttag', 'a', [('foo', 'bar')])])
1049 self._run_check("<a foo=\vbar>", [('starttag', 'a', [('foo', '\vbar')])])
1050 self._run_check("<a foo=\xa0bar>", [('starttag', 'a', [('foo', '\xa0bar')])])
1051
1052 def test_attr_values(self):
1053 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
1054 [("starttag", "a", [("b", "xxx\n\txxx"),
1055 ("c", "yyy\t\nyyy"),
1056 ("d", "\txyz\n")])])
1057 self._run_check("""<a b='' c="">""",
1058 [("starttag", "a", [("b", ""), ("c", "")])])
1059 self._run_check("<a b=\tx c=\ny>",
1060 [('starttag', 'a', [('b', 'x'), ('c', 'y')])])
1061 self._run_check("<a b=\v c=\xa0>",
1062 [("starttag", "a", [("b", "\v"), ("c", "\xa0")])])
1063 # Regression test for SF patch #669683.
1064 self._run_check("<e a=rgb(1,2,3)>",
1065 [("starttag", "e", [("a", "rgb(1,2,3)")])])
1066 # Regression test for SF bug #921657.
1067 self._run_check(
1068 "<a href=mailto:[email protected]>",
1069 [("starttag", "a", [("href", "mailto:[email protected]")])])
1070
1071 def test_attr_nonascii(self):
1072 # see issue 7311
1073 self._run_check(
1074 "<img src=/foo/bar.png alt=\u4e2d\u6587>",
1075 [("starttag", "img", [("src", "/foo/bar.png"),
1076 ("alt", "\u4e2d\u6587")])])
1077 self._run_check(
1078 "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
1079 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
1080 ("href", "\u30c6\u30b9\u30c8.html")])])
1081 self._run_check(
1082 '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
1083 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
1084 ("href", "\u30c6\u30b9\u30c8.html")])])
1085
1086 def test_attr_entity_replacement(self):
1087 self._run_check(
1088 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
1089 [("starttag", "a", [("b", "&><\"'")])])
1090
1091 def test_attr_funky_names(self):
1092 self._run_check(
1093 "<a a.b='v' c:d=v e-f=v>",
1094 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
1095
1096 def test_entityrefs_in_attributes(self):
1097 self._run_check(
1098 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
1099 [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])])
1100
1101
1102 def test_attr_funky_names2(self):
1103 self._run_check(
1104 r"<a $><b $=%><c \=/>",
1105 [("starttag", "a", [("$", None)]),
1106 ("starttag", "b", [("$", "%")]),
1107 ("starttag", "c", [("\\", "/")])])
1108
1109 def test_entities_in_attribute_value(self):
1110 # see #1200313
1111 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
1112 self._run_check('<a href="%s">' % entity,
1113 [("starttag", "a", [("href", "&")])])
1114 self._run_check("<a href='%s'>" % entity,
1115 [("starttag", "a", [("href", "&")])])
1116 self._run_check("<a href=%s>" % entity,
1117 [("starttag", "a", [("href", "&")])])
1118
1119 def test_malformed_attributes(self):
1120 # see #13357
1121 html = (
1122 "<a href=test'style='color:red;bad1'>test - bad1</a>"
1123 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
1124 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
1125 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
1126 )
1127 expected = [
1128 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
1129 ('data', 'test - bad1'),
1130 ('endtag', 'a'),
1131 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
1132 ('data', 'test - bad2'),
1133 ('endtag', 'a'),
1134 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]),
1135 ('data', 'test - bad3'),
1136 ('endtag', 'a'),
1137 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]),
1138 ('data', 'test - bad4'),
1139 ('endtag', 'a'),
1140 ]
1141 self._run_check(html, expected)
1142
1143 def test_malformed_adjacent_attributes(self):
1144 # see #12629
1145 self._run_check('<x><y z=""o"" /></x>',
1146 [('starttag', 'x', []),
1147 ('startendtag', 'y', [('z', ''), ('o""', None)]),
1148 ('endtag', 'x')])
1149 self._run_check('<x><y z="""" /></x>',
1150 [('starttag', 'x', []),
1151 ('startendtag', 'y', [('z', ''), ('""', None)]),
1152 ('endtag', 'x')])
1153
1154 # see #755670 for the following 3 tests
1155 def test_adjacent_attributes(self):
1156 self._run_check('<a width="100%"cellspacing=0>',
1157 [("starttag", "a",
1158 [("width", "100%"), ("cellspacing","0")])])
1159
1160 self._run_check('<a id="foo"class="bar">',
1161 [("starttag", "a",
1162 [("id", "foo"), ("class","bar")])])
1163
1164 def test_missing_attribute_value(self):
1165 self._run_check('<a v=>',
1166 [("starttag", "a", [("v", "")])])
1167
1168 def test_javascript_attribute_value(self):
1169 self._run_check("<a href=javascript:popup('/popup/help.html')>",
1170 [("starttag", "a",
1171 [("href", "javascript:popup('/popup/help.html')")])])
1172
1173 def test_end_tag_in_attribute_value(self):
1174 # see #1745761
1175 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
1176 [("starttag", "a",
1177 [("href", "http://www.example.org/\">;")]),
1178 ("data", "spam"), ("endtag", "a")])
1179
1180 def test_with_unquoted_attributes(self):
1181 # see #12008
1182 html = ("<html><body bgcolor=d0ca90 text='181008'>"
1183 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
1184 "<td align=left><font size=-1>"
1185 "- <a href=/rabota/><span class=en> software-and-i</span></a>"
1186 "- <a href='/1/'><span class=en> library</span></a></table>")
1187 expected = [
1188 ('starttag', 'html', []),
1189 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
1190 ('starttag', 'table',
1191 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
1192 ('starttag', 'tr', []),
1193 ('starttag', 'td', [('align', 'left')]),
1194 ('starttag', 'font', [('size', '-1')]),
1195 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
1196 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
1197 ('endtag', 'span'), ('endtag', 'a'),
1198 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
1199 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
1200 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
1201 ]
1202 self._run_check(html, expected)
1203
1204 def test_comma_between_attributes(self):
1205 # see bpo 41478
1206 # HTMLParser preserves duplicate attributes, leaving the task of
1207 # removing duplicate attributes to a conformant html tree builder
1208 html = ('<div class=bar,baz=asd>' # between attrs (unquoted)
1209 '<div class="bar",baz="asd">' # between attrs (quoted)
1210 '<div class=bar, baz=asd,>' # after values (unquoted)
1211 '<div class="bar", baz="asd",>' # after values (quoted)
1212 '<div class="bar",>' # one comma values (quoted)
1213 '<div class=,bar baz=,asd>' # before values (unquoted)
1214 '<div class=,"bar" baz=,"asd">' # before values (quoted)
1215 '<div ,class=bar ,baz=asd>' # before names
1216 '<div class,="bar" baz,="asd">' # after names
1217 )
1218 expected = [
1219 ('starttag', 'div', [('class', 'bar,baz=asd'),]),
1220 ('starttag', 'div', [('class', 'bar'), (',baz', 'asd')]),
1221 ('starttag', 'div', [('class', 'bar,'), ('baz', 'asd,')]),
1222 ('starttag', 'div', [('class', 'bar'), (',', None),
1223 ('baz', 'asd'), (',', None)]),
1224 ('starttag', 'div', [('class', 'bar'), (',', None)]),
1225 ('starttag', 'div', [('class', ',bar'), ('baz', ',asd')]),
1226 ('starttag', 'div', [('class', ',"bar"'), ('baz', ',"asd"')]),
1227 ('starttag', 'div', [(',class', 'bar'), (',baz', 'asd')]),
1228 ('starttag', 'div', [('class,', 'bar'), ('baz,', 'asd')]),
1229 ]
1230 self._run_check(html, expected)
1231
1232 def test_weird_chars_in_unquoted_attribute_values(self):
1233 self._run_check('<form action=bogus|&#()value>', [
1234 ('starttag', 'form',
1235 [('action', 'bogus|&#()value')])])
1236
1237
1238 class TestInheritance(unittest.TestCase):
1239
1240 @patch("_markupbase.ParserBase.__init__")
1241 @patch("_markupbase.ParserBase.reset")
1242 def test_base_class_methods_called(self, super_reset_method, super_init_method):
1243 with patch('_markupbase.ParserBase') as parser_base:
1244 EventCollector()
1245 super_init_method.assert_called_once()
1246 super_reset_method.assert_called_once()
1247
1248
1249 if __name__ == "__main__":
1250 unittest.main()