Changeset 391 for python/trunk/Lib/test/test_htmlparser.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_htmlparser.py
r2 r391 115 115 text 116 116 “ 117 <!--comment2a-- --comment2b--> <!>117 <!--comment2a-- --comment2b--> 118 118 </Html> 119 119 """, [ … … 143 143 ]) 144 144 145 def test_doctype_decl(self):146 inside = """\147 DOCTYPE html [148 <!ELEMENT html - O EMPTY>149 <!ATTLIST html150 version CDATA #IMPLIED151 profile CDATA 'DublinCore'>152 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>153 <!ENTITY myEntity 'internal parsed entity'>154 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>155 <!ENTITY % paramEntity 'name|name|name'>156 %paramEntity;157 <!-- comment -->158 ]"""159 self._run_check("<!%s>" % inside, [160 ("decl", inside),161 ])162 163 145 def test_bad_nesting(self): 164 146 # Strangely, this *is* supposed to test that overlapping … … 182 164 ]) 183 165 184 def test_attr_syntax(self):185 output = [186 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])187 ]188 self._run_check("""<a b='v' c="v" d=v e>""", output)189 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)190 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)191 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)192 193 def test_attr_values(self):194 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",195 [("starttag", "a", [("b", "xxx\n\txxx"),196 ("c", "yyy\t\nyyy"),197 ("d", "\txyz\n")])198 ])199 self._run_check("""<a b='' c="">""", [200 ("starttag", "a", [("b", ""), ("c", "")]),201 ])202 # Regression test for SF patch #669683.203 self._run_check("<e a=rgb(1,2,3)>", [204 ("starttag", "e", [("a", "rgb(1,2,3)")]),205 ])206 # Regression test for SF bug #921657.207 self._run_check("<a href=mailto:xyz@example.com>", [208 ("starttag", "a", [("href", "mailto:xyz@example.com")]),209 ])210 211 def test_attr_entity_replacement(self):212 self._run_check("""<a b='&><"''>""", [213 ("starttag", "a", [("b", "&><\"'")]),214 ])215 216 def test_attr_funky_names(self):217 self._run_check("""<a a.b='v' c:d=v e-f=v>""", [218 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),219 ])220 221 166 def test_illegal_declarations(self): 222 self._parse_error('<!spacer type="block" height="25">') 167 self._run_check('<!spacer type="block" height="25">', 168 [('comment', 'spacer type="block" height="25"')]) 223 169 224 170 def test_starttag_end_boundary(self): … … 257 203 258 204 def test_starttag_junk_chars(self): 259 self._parse_error("</>") 260 self._parse_error("</$>") 261 self._parse_error("</") 262 self._parse_error("</a") 263 self._parse_error("<a<a>") 264 self._parse_error("</a<a>") 265 self._parse_error("<!") 266 self._parse_error("<a $>") 267 self._parse_error("<a") 268 self._parse_error("<a foo='bar'") 269 self._parse_error("<a foo='bar") 270 self._parse_error("<a foo='>'") 271 self._parse_error("<a foo='>") 272 self._parse_error("<a foo=>") 205 self._run_check("</>", []) 206 self._run_check("</$>", [('comment', '$')]) 207 self._run_check("</", [('data', '</')]) 208 self._run_check("</a", [('data', '</a')]) 209 # XXX this might be wrong 210 self._run_check("<a<a>", [('data', '<a'), ('starttag', 'a', [])]) 211 self._run_check("</a<a>", [('endtag', 'a<a')]) 212 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='>")]) 218 219 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 243 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 #see issue #14538 264 html = ('<meta><meta / ><meta // ><meta / / >' 265 '<meta/><meta /><meta //><meta//>') 266 expected = [ 267 ('starttag', 'meta', []), ('starttag', 'meta', []), 268 ('starttag', 'meta', []), ('starttag', 'meta', []), 269 ('startendtag', 'meta', []), ('startendtag', 'meta', []), 270 ('startendtag', 'meta', []), ('startendtag', 'meta', []), 271 ] 272 self._run_check(html, expected) 273 273 274 274 def test_declaration_junk_chars(self): 275 self._ parse_error("<!DOCTYPE foo $ >")275 self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')]) 276 276 277 277 def test_startendtag(self): … … 288 288 ("endtag", "p"), 289 289 ]) 290 291 def test_invalid_end_tags(self): 292 # A collection of broken end tags. <br> is used as separator. 293 # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state 294 # and #13993 295 html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>' 296 '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>') 297 expected = [('starttag', 'br', []), 298 # < is part of the name, / is discarded, p is an attribute 299 ('endtag', 'label<'), 300 ('starttag', 'br', []), 301 # text and attributes are discarded 302 ('endtag', 'div'), 303 ('starttag', 'br', []), 304 # comment because the first char after </ is not a-zA-Z 305 ('comment', '<h4'), 306 ('starttag', 'br', []), 307 # attributes are discarded 308 ('endtag', 'li'), 309 ('starttag', 'br', []), 310 # everything till ul (included) is discarded 311 ('endtag', 'li'), 312 ('starttag', 'br', []), 313 # </> is ignored 314 ('starttag', 'br', [])] 315 self._run_check(html, expected) 316 317 def test_broken_invalid_end_tag(self): 318 # This is technically wrong (the "> shouldn't be included in the 'data') 319 # but is probably not worth fixing it (in addition to all the cases of 320 # the previous test, it would require a full attribute parsing). 321 # see #13993 322 html = '<b>This</b attr=">"> confuses the parser' 323 expected = [('starttag', 'b', []), 324 ('data', 'This'), 325 ('endtag', 'b'), 326 ('data', '"> confuses the parser')] 327 self._run_check(html, expected) 290 328 291 329 def test_get_starttag_text(self): … … 296 334 297 335 def test_cdata_content(self): 298 s = """<script> <!-- not a comment --> ¬-an-entity-ref; </script>""" 299 self._run_check(s, [ 300 ("starttag", "script", []), 301 ("data", " <!-- not a comment --> ¬-an-entity-ref; "), 302 ("endtag", "script"), 303 ]) 304 s = """<script> <not a='start tag'> </script>""" 305 self._run_check(s, [ 306 ("starttag", "script", []), 307 ("data", " <not a='start tag'> "), 308 ("endtag", "script"), 309 ]) 336 contents = [ 337 '<!-- not a comment --> ¬-an-entity-ref;', 338 "<not a='start tag'>", 339 '<a href="" /> <p> <span></span>', 340 'foo = "</scr" + "ipt>";', 341 'foo = "</SCRIPT" + ">";', 342 'foo = <\n/script> ', 343 '<!-- document.write("</scr" + "ipt>"); -->', 344 ('\n//<![CDATA[\n' 345 'document.write(\'<s\'+\'cript type="text/javascript" ' 346 'src="http://www.example.org/r=\'+new ' 347 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'), 348 '\n<!-- //\nvar foo = 3.14;\n// -->\n', 349 'foo = "</sty" + "le>";', 350 u'<!-- \u2603 -->', 351 # these two should be invalid according to the HTML 5 spec, 352 # section 8.1.2.2 353 #'foo = </\nscript>', 354 #'foo = </ script>', 355 ] 356 elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style'] 357 for content in contents: 358 for element in elements: 359 element_lower = element.lower() 360 s = u'<{element}>{content}</{element}>'.format(element=element, 361 content=content) 362 self._run_check(s, [("starttag", element_lower, []), 363 ("data", content), 364 ("endtag", element_lower)]) 365 366 def test_cdata_with_closing_tags(self): 367 # see issue #13358 368 # make sure that HTMLParser calls handle_data only once for each CDATA. 369 # The normal event collector normalizes the events in get_events, 370 # so we override it to return the original list of events. 371 class Collector(EventCollector): 372 def get_events(self): 373 return self.events 374 375 content = """<!-- not a comment --> ¬-an-entity-ref; 376 <a href="" /> </p><p> & <span></span></style> 377 '</script' + '>' </html> </head> </scripter>!""" 378 for element in [' script', 'script ', ' script ', 379 '\nscript', 'script\n', '\nscript\n']: 380 s = u'<script>{content}</{element}>'.format(element=element, 381 content=content) 382 self._run_check(s, [("starttag", "script", []), 383 ("data", content), 384 ("endtag", "script")], 385 collector=Collector) 386 387 def test_malformatted_charref(self): 388 self._run_check("<p>&#bad;</p>", [ 389 ("starttag", "p", []), 390 ("data", "&#bad;"), 391 ("endtag", "p"), 392 ]) 393 394 def test_unescape_function(self): 395 parser = HTMLParser.HTMLParser() 396 self.assertEqual(parser.unescape('&#bad;'),'&#bad;') 397 self.assertEqual(parser.unescape('&'),'&') 398 399 400 401 class AttributesTestCase(TestCaseBase): 402 403 def test_attr_syntax(self): 404 output = [ 405 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)]) 406 ] 407 self._run_check("""<a b='v' c="v" d=v e>""", output) 408 self._run_check("""<a b = 'v' c = "v" d = v e>""", output) 409 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output) 410 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output) 411 412 def test_attr_values(self): 413 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", 414 [("starttag", "a", [("b", "xxx\n\txxx"), 415 ("c", "yyy\t\nyyy"), 416 ("d", "\txyz\n")])]) 417 self._run_check("""<a b='' c="">""", 418 [("starttag", "a", [("b", ""), ("c", "")])]) 419 # Regression test for SF patch #669683. 420 self._run_check("<e a=rgb(1,2,3)>", 421 [("starttag", "e", [("a", "rgb(1,2,3)")])]) 422 # Regression test for SF bug #921657. 423 self._run_check( 424 "<a href=mailto:xyz@example.com>", 425 [("starttag", "a", [("href", "mailto:xyz@example.com")])]) 426 427 def test_attr_nonascii(self): 428 # see issue 7311 429 self._run_check( 430 u"<img src=/foo/bar.png alt=\u4e2d\u6587>", 431 [("starttag", "img", [("src", "/foo/bar.png"), 432 ("alt", u"\u4e2d\u6587")])]) 433 self._run_check( 434 u"<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>", 435 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"), 436 ("href", u"\u30c6\u30b9\u30c8.html")])]) 437 self._run_check( 438 u'<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">', 439 [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"), 440 ("href", u"\u30c6\u30b9\u30c8.html")])]) 441 442 def test_attr_entity_replacement(self): 443 self._run_check( 444 "<a b='&><"''>", 445 [("starttag", "a", [("b", "&><\"'")])]) 446 447 def test_attr_funky_names(self): 448 self._run_check( 449 "<a a.b='v' c:d=v e-f=v>", 450 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])]) 451 self._run_check( 452 "<a $><b $=%><c \=/>", 453 [("starttag", "a", [("$", None)]), 454 ("starttag", "b", [("$", "%")]), 455 ("starttag", "c", [("\\", "/")])]) 310 456 311 457 def test_entityrefs_in_attributes(self): 312 self._run_check("<html foo='€&aa&unsupported;'>", [ 313 ("starttag", "html", [("foo", u"\u20AC&aa&unsupported;")]) 314 ]) 458 self._run_check( 459 "<html foo='€&aa&unsupported;'>", 460 [("starttag", "html", [("foo", u"\u20AC&aa&unsupported;")])]) 461 462 def test_entities_in_attribute_value(self): 463 # see #1200313 464 for entity in ['&', '&', '&', '&']: 465 self._run_check('<a href="%s">' % entity, 466 [("starttag", "a", [("href", "&")])]) 467 self._run_check("<a href='%s'>" % entity, 468 [("starttag", "a", [("href", "&")])]) 469 self._run_check("<a href=%s>" % entity, 470 [("starttag", "a", [("href", "&")])]) 471 472 def test_malformed_attributes(self): 473 # see #13357 474 html = ( 475 "<a href=test'style='color:red;bad1'>test - bad1</a>" 476 "<a href=test'+style='color:red;ba2'>test - bad2</a>" 477 "<a href=test' style='color:red;bad3'>test - bad3</a>" 478 "<a href = test' style='color:red;bad4' >test - bad4</a>" 479 ) 480 expected = [ 481 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]), 482 ('data', 'test - bad1'), ('endtag', 'a'), 483 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]), 484 ('data', 'test - bad2'), ('endtag', 'a'), 485 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad3'")]), 486 ('data', 'test - bad3'), ('endtag', 'a'), 487 ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad4'")]), 488 ('data', 'test - bad4'), ('endtag', 'a') 489 ] 490 self._run_check(html, expected) 491 492 def test_malformed_adjacent_attributes(self): 493 # see #12629 494 self._run_check('<x><y z=""o"" /></x>', 495 [('starttag', 'x', []), 496 ('startendtag', 'y', [('z', ''), ('o""', None)]), 497 ('endtag', 'x')]) 498 self._run_check('<x><y z="""" /></x>', 499 [('starttag', 'x', []), 500 ('startendtag', 'y', [('z', ''), ('""', None)]), 501 ('endtag', 'x')]) 502 503 # see #755670 for the following 3 tests 504 def test_adjacent_attributes(self): 505 self._run_check('<a width="100%"cellspacing=0>', 506 [("starttag", "a", 507 [("width", "100%"), ("cellspacing","0")])]) 508 509 self._run_check('<a id="foo"class="bar">', 510 [("starttag", "a", 511 [("id", "foo"), ("class","bar")])]) 512 513 def test_missing_attribute_value(self): 514 self._run_check('<a v=>', 515 [("starttag", "a", [("v", "")])]) 516 517 def test_javascript_attribute_value(self): 518 self._run_check("<a href=javascript:popup('/popup/help.html')>", 519 [("starttag", "a", 520 [("href", "javascript:popup('/popup/help.html')")])]) 521 522 def test_end_tag_in_attribute_value(self): 523 # see #1745761 524 self._run_check("<a href='http://www.example.org/\">;'>spam</a>", 525 [("starttag", "a", 526 [("href", "http://www.example.org/\">;")]), 527 ("data", "spam"), ("endtag", "a")]) 528 529 def test_comments(self): 530 html = ("<!-- I'm a valid comment -->" 531 '<!--me too!-->' 532 '<!------>' 533 '<!---->' 534 '<!----I have many hyphens---->' 535 '<!-- I have a > in the middle -->' 536 '<!-- and I have -- in the middle! -->') 537 expected = [('comment', " I'm a valid comment "), 538 ('comment', 'me too!'), 539 ('comment', '--'), 540 ('comment', ''), 541 ('comment', '--I have many hyphens--'), 542 ('comment', ' I have a > in the middle '), 543 ('comment', ' and I have -- in the middle! ')] 544 self._run_check(html, expected) 545 546 def test_broken_comments(self): 547 html = ('<! not really a comment >' 548 '<! not a comment either -->' 549 '<! -- close enough -->' 550 '<!><!<-- this was an empty comment>' 551 '<!!! another bogus comment !!!>') 552 expected = [ 553 ('comment', ' not really a comment '), 554 ('comment', ' not a comment either --'), 555 ('comment', ' -- close enough --'), 556 ('comment', ''), 557 ('comment', '<-- this was an empty comment'), 558 ('comment', '!! another bogus comment !!!'), 559 ] 560 self._run_check(html, expected) 561 562 def test_condcoms(self): 563 html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->' 564 '<!--[if IE 8]>condcoms<![endif]-->' 565 '<!--[if lte IE 7]>pretty?<![endif]-->') 566 expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"), 567 ('comment', '[if IE 8]>condcoms<![endif]'), 568 ('comment', '[if lte IE 7]>pretty?<![endif]')] 569 self._run_check(html, expected) 570 571 def test_broken_condcoms(self): 572 # these condcoms are missing the '--' after '<!' and before the '>' 573 html = ('<![if !(IE)]>broken condcom<![endif]>' 574 '<![if ! IE]><link href="favicon.tiff"/><![endif]>' 575 '<![if !IE 6]><img src="firefox.png" /><![endif]>' 576 '<![if !ie 6]><b>foo</b><![endif]>' 577 '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>') 578 # According to the HTML5 specs sections "8.2.4.44 Bogus comment state" 579 # and "8.2.4.45 Markup declaration open state", comment tokens should 580 # be emitted instead of 'unknown decl', but calling unknown_decl 581 # provides more flexibility. 582 # See also Lib/_markupbase.py:parse_declaration 583 expected = [ 584 ('unknown decl', 'if !(IE)'), 585 ('data', 'broken condcom'), 586 ('unknown decl', 'endif'), 587 ('unknown decl', 'if ! IE'), 588 ('startendtag', 'link', [('href', 'favicon.tiff')]), 589 ('unknown decl', 'endif'), 590 ('unknown decl', 'if !IE 6'), 591 ('startendtag', 'img', [('src', 'firefox.png')]), 592 ('unknown decl', 'endif'), 593 ('unknown decl', 'if !ie 6'), 594 ('starttag', 'b', []), 595 ('data', 'foo'), 596 ('endtag', 'b'), 597 ('unknown decl', 'endif'), 598 ('unknown decl', 'if (!IE)|(lt IE 9)'), 599 ('startendtag', 'img', [('src', 'mammoth.bmp')]), 600 ('unknown decl', 'endif') 601 ] 602 self._run_check(html, expected) 315 603 316 604 317 605 def test_main(): 318 test_support.run_unittest(HTMLParserTestCase )606 test_support.run_unittest(HTMLParserTestCase, AttributesTestCase) 319 607 320 608
Note:
See TracChangeset
for help on using the changeset viewer.