Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 1 | """Tests for HTMLParser.py.""" |
| 2 | |
Georg Brandl | bcdafa4 | 2008-05-20 07:58:42 +0000 | [diff] [blame] | 3 | import HTMLParser |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 4 | import pprint |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 5 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 6 | from test import test_support |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 7 | |
| 8 | |
Georg Brandl | bcdafa4 | 2008-05-20 07:58:42 +0000 | [diff] [blame] | 9 | class EventCollector(HTMLParser.HTMLParser): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 10 | |
| 11 | def __init__(self): |
| 12 | self.events = [] |
| 13 | self.append = self.events.append |
Georg Brandl | bcdafa4 | 2008-05-20 07:58:42 +0000 | [diff] [blame] | 14 | HTMLParser.HTMLParser.__init__(self) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 15 | |
| 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 Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 62 | def unknown_decl(self, decl): |
| 63 | self.append(("unknown decl", decl)) |
| 64 | |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 65 | |
| 66 | class 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 | |
| 73 | class TestCaseBase(unittest.TestCase): |
| 74 | |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 75 | def _run_check(self, source, expected_events, collector=EventCollector): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 76 | parser = collector() |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 77 | for s in source: |
| 78 | parser.feed(s) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 79 | parser.close() |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 80 | events = parser.get_events() |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 81 | if events != expected_events: |
| 82 | self.fail("received events did not match expected events\n" |
| 83 | "Expected:\n" + pprint.pformat(expected_events) + |
| 84 | "\nReceived:\n" + pprint.pformat(events)) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 85 | |
| 86 | def _run_check_extra(self, source, events): |
| 87 | self._run_check(source, events, EventCollectorExtra) |
| 88 | |
| 89 | def _parse_error(self, source): |
| 90 | def parse(source=source): |
Georg Brandl | bcdafa4 | 2008-05-20 07:58:42 +0000 | [diff] [blame] | 91 | parser = HTMLParser.HTMLParser() |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 92 | parser.feed(source) |
| 93 | parser.close() |
Georg Brandl | bcdafa4 | 2008-05-20 07:58:42 +0000 | [diff] [blame] | 94 | self.assertRaises(HTMLParser.HTMLParseError, parse) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | class HTMLParserTestCase(TestCaseBase): |
| 98 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 99 | def test_processing_instruction_only(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 100 | self._run_check("<?processing instruction>", [ |
| 101 | ("pi", "processing instruction"), |
| 102 | ]) |
Fred Drake | fafd56f | 2003-04-17 22:19:26 +0000 | [diff] [blame] | 103 | self._run_check("<?processing instruction ?>", [ |
| 104 | ("pi", "processing instruction ?"), |
| 105 | ]) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 106 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 107 | def test_simple_html(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 108 | self._run_check(""" |
| 109 | <!DOCTYPE html PUBLIC 'foo'> |
| 110 | <HTML>&entity;  |
| 111 | <!--comment1a |
| 112 | -></foo><bar><<?pi?></foo<bar |
| 113 | comment1b--> |
| 114 | <Img sRc='Bar' isMAP>sample |
| 115 | text |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 116 | “ |
Georg Brandl | d09def3 | 2006-03-09 13:27:14 +0000 | [diff] [blame] | 117 | <!--comment2a-- --comment2b--><!> |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 118 | </Html> |
| 119 | """, [ |
| 120 | ("data", "\n"), |
| 121 | ("decl", "DOCTYPE html PUBLIC 'foo'"), |
| 122 | ("data", "\n"), |
| 123 | ("starttag", "html", []), |
| 124 | ("entityref", "entity"), |
| 125 | ("charref", "32"), |
| 126 | ("data", "\n"), |
| 127 | ("comment", "comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b"), |
| 128 | ("data", "\n"), |
| 129 | ("starttag", "img", [("src", "Bar"), ("ismap", None)]), |
| 130 | ("data", "sample\ntext\n"), |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 131 | ("charref", "x201C"), |
| 132 | ("data", "\n"), |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 133 | ("comment", "comment2a-- --comment2b"), |
| 134 | ("data", "\n"), |
| 135 | ("endtag", "html"), |
| 136 | ("data", "\n"), |
| 137 | ]) |
| 138 | |
Fred Drake | 073148c | 2001-12-03 16:44:09 +0000 | [diff] [blame] | 139 | def test_unclosed_entityref(self): |
| 140 | self._run_check("&entityref foo", [ |
| 141 | ("entityref", "entityref"), |
| 142 | ("data", " foo"), |
| 143 | ]) |
| 144 | |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 145 | def test_doctype_decl(self): |
| 146 | inside = """\ |
| 147 | DOCTYPE html [ |
| 148 | <!ELEMENT html - O EMPTY> |
| 149 | <!ATTLIST html |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 150 | version CDATA #IMPLIED |
| 151 | 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; |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 157 | <!-- comment --> |
| 158 | ]""" |
| 159 | self._run_check("<!%s>" % inside, [ |
| 160 | ("decl", inside), |
| 161 | ]) |
| 162 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 163 | def test_bad_nesting(self): |
| 164 | # Strangely, this *is* supposed to test that overlapping |
| 165 | # elements are allowed. HTMLParser is more geared toward |
| 166 | # lexing the input that parsing the structure. |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 167 | self._run_check("<a><b></a></b>", [ |
| 168 | ("starttag", "a", []), |
| 169 | ("starttag", "b", []), |
| 170 | ("endtag", "a"), |
| 171 | ("endtag", "b"), |
| 172 | ]) |
| 173 | |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 174 | def test_bare_ampersands(self): |
| 175 | self._run_check("this text & contains & ampersands &", [ |
| 176 | ("data", "this text & contains & ampersands &"), |
| 177 | ]) |
| 178 | |
| 179 | def test_bare_pointy_brackets(self): |
| 180 | self._run_check("this < text > contains < bare>pointy< brackets", [ |
| 181 | ("data", "this < text > contains < bare>pointy< brackets"), |
| 182 | ]) |
| 183 | |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 184 | def test_illegal_declarations(self): |
Fred Drake | 7cf613d | 2001-09-04 16:26:03 +0000 | [diff] [blame] | 185 | self._parse_error('<!spacer type="block" height="25">') |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 186 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 187 | def test_starttag_end_boundary(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 188 | self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])]) |
| 189 | self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])]) |
| 190 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 191 | def test_buffer_artefacts(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 192 | output = [("starttag", "a", [("b", "<")])] |
| 193 | self._run_check(["<a b='<'>"], output) |
| 194 | self._run_check(["<a ", "b='<'>"], output) |
| 195 | self._run_check(["<a b", "='<'>"], output) |
| 196 | self._run_check(["<a b=", "'<'>"], output) |
| 197 | self._run_check(["<a b='<", "'>"], output) |
| 198 | self._run_check(["<a b='<'", ">"], output) |
| 199 | |
| 200 | output = [("starttag", "a", [("b", ">")])] |
| 201 | self._run_check(["<a b='>'>"], output) |
| 202 | self._run_check(["<a ", "b='>'>"], output) |
| 203 | self._run_check(["<a b", "='>'>"], output) |
| 204 | self._run_check(["<a b=", "'>'>"], output) |
| 205 | self._run_check(["<a b='>", "'>"], output) |
| 206 | self._run_check(["<a b='>'", ">"], output) |
| 207 | |
Fred Drake | 75d9a62 | 2004-09-08 22:57:01 +0000 | [diff] [blame] | 208 | output = [("comment", "abc")] |
| 209 | self._run_check(["", "<!--abc-->"], output) |
| 210 | self._run_check(["<", "!--abc-->"], output) |
| 211 | self._run_check(["<!", "--abc-->"], output) |
| 212 | self._run_check(["<!-", "-abc-->"], output) |
| 213 | self._run_check(["<!--", "abc-->"], output) |
| 214 | self._run_check(["<!--a", "bc-->"], output) |
| 215 | self._run_check(["<!--ab", "c-->"], output) |
| 216 | self._run_check(["<!--abc", "-->"], output) |
| 217 | self._run_check(["<!--abc-", "->"], output) |
| 218 | self._run_check(["<!--abc--", ">"], output) |
| 219 | self._run_check(["<!--abc-->", ""], output) |
| 220 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 221 | def test_starttag_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 222 | self._parse_error("</>") |
| 223 | self._parse_error("</$>") |
| 224 | self._parse_error("</") |
| 225 | self._parse_error("</a") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 226 | self._parse_error("<a<a>") |
| 227 | self._parse_error("</a<a>") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 228 | self._parse_error("<!") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 229 | self._parse_error("<a") |
| 230 | self._parse_error("<a foo='bar'") |
| 231 | self._parse_error("<a foo='bar") |
| 232 | self._parse_error("<a foo='>'") |
| 233 | self._parse_error("<a foo='>") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 234 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 235 | def test_declaration_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 236 | self._parse_error("<!DOCTYPE foo $ >") |
| 237 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 238 | def test_startendtag(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 239 | self._run_check("<p/>", [ |
| 240 | ("startendtag", "p", []), |
| 241 | ]) |
| 242 | self._run_check("<p></p>", [ |
| 243 | ("starttag", "p", []), |
| 244 | ("endtag", "p"), |
| 245 | ]) |
| 246 | self._run_check("<p><img src='foo' /></p>", [ |
| 247 | ("starttag", "p", []), |
| 248 | ("startendtag", "img", [("src", "foo")]), |
| 249 | ("endtag", "p"), |
| 250 | ]) |
| 251 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 252 | def test_get_starttag_text(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 253 | s = """<foo:bar \n one="1"\ttwo=2 >""" |
| 254 | self._run_check_extra(s, [ |
| 255 | ("starttag", "foo:bar", [("one", "1"), ("two", "2")]), |
| 256 | ("starttag_text", s)]) |
| 257 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 258 | def test_cdata_content(self): |
Ezio Melotti | 7e82b27 | 2011-11-01 14:09:56 +0200 | [diff] [blame] | 259 | contents = [ |
| 260 | '<!-- not a comment --> ¬-an-entity-ref;', |
| 261 | "<not a='start tag'>", |
| 262 | '<a href="" /> <p> <span></span>', |
| 263 | 'foo = "</scr" + "ipt>";', |
| 264 | 'foo = "</SCRIPT" + ">";', |
| 265 | 'foo = <\n/script> ', |
| 266 | '<!-- document.write("</scr" + "ipt>"); -->', |
| 267 | ('\n//<![CDATA[\n' |
| 268 | 'document.write(\'<s\'+\'cript type="text/javascript" ' |
| 269 | 'src="http://www.example.org/r=\'+new ' |
| 270 | 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'), |
| 271 | '\n<!-- //\nvar foo = 3.14;\n// -->\n', |
| 272 | 'foo = "</sty" + "le>";', |
| 273 | u'<!-- \u2603 -->', |
| 274 | # these two should be invalid according to the HTML 5 spec, |
| 275 | # section 8.1.2.2 |
| 276 | #'foo = </\nscript>', |
| 277 | #'foo = </ script>', |
| 278 | ] |
| 279 | elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style'] |
| 280 | for content in contents: |
| 281 | for element in elements: |
| 282 | element_lower = element.lower() |
| 283 | s = u'<{element}>{content}</{element}>'.format(element=element, |
| 284 | content=content) |
| 285 | self._run_check(s, [("starttag", element_lower, []), |
| 286 | ("data", content), |
| 287 | ("endtag", element_lower)]) |
| 288 | |
Ezio Melotti | 00dc60b | 2011-11-18 18:00:40 +0200 | [diff] [blame] | 289 | def test_cdata_with_closing_tags(self): |
| 290 | # see issue #13358 |
| 291 | # make sure that HTMLParser calls handle_data only once for each CDATA. |
| 292 | # The normal event collector normalizes the events in get_events, |
| 293 | # so we override it to return the original list of events. |
| 294 | class Collector(EventCollector): |
| 295 | def get_events(self): |
| 296 | return self.events |
| 297 | |
| 298 | content = """<!-- not a comment --> ¬-an-entity-ref; |
| 299 | <a href="" /> </p><p> & <span></span></style> |
| 300 | '</script' + '>' </html> </head> </scripter>!""" |
| 301 | for element in [' script', 'script ', ' script ', |
| 302 | '\nscript', 'script\n', '\nscript\n']: |
| 303 | s = u'<script>{content}</{element}>'.format(element=element, |
| 304 | content=content) |
| 305 | self._run_check(s, [("starttag", "script", []), |
| 306 | ("data", content), |
| 307 | ("endtag", "script")], |
| 308 | collector=Collector) |
| 309 | |
Victor Stinner | 554a3b8 | 2010-05-24 21:33:24 +0000 | [diff] [blame] | 310 | def test_malformatted_charref(self): |
| 311 | self._run_check("<p>&#bad;</p>", [ |
| 312 | ("starttag", "p", []), |
| 313 | ("data", "&#bad;"), |
| 314 | ("endtag", "p"), |
| 315 | ]) |
| 316 | |
Senthil Kumaran | 3f60f09 | 2010-12-28 16:05:07 +0000 | [diff] [blame] | 317 | def test_unescape_function(self): |
| 318 | parser = HTMLParser.HTMLParser() |
| 319 | self.assertEqual(parser.unescape('&#bad;'),'&#bad;') |
| 320 | self.assertEqual(parser.unescape('&'),'&') |
| 321 | |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 322 | |
Ezio Melotti | 7459291 | 2011-11-08 02:07:18 +0200 | [diff] [blame] | 323 | |
| 324 | class AttributesTestCase(TestCaseBase): |
| 325 | |
| 326 | def test_attr_syntax(self): |
| 327 | output = [ |
| 328 | ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)]) |
| 329 | ] |
| 330 | self._run_check("""<a b='v' c="v" d=v e>""", output) |
| 331 | self._run_check("""<a b = 'v' c = "v" d = v e>""", output) |
| 332 | self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output) |
| 333 | self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output) |
| 334 | |
| 335 | def test_attr_values(self): |
| 336 | self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", |
| 337 | [("starttag", "a", [("b", "xxx\n\txxx"), |
| 338 | ("c", "yyy\t\nyyy"), |
| 339 | ("d", "\txyz\n")])]) |
| 340 | self._run_check("""<a b='' c="">""", |
| 341 | [("starttag", "a", [("b", ""), ("c", "")])]) |
| 342 | # Regression test for SF patch #669683. |
| 343 | self._run_check("<e a=rgb(1,2,3)>", |
| 344 | [("starttag", "e", [("a", "rgb(1,2,3)")])]) |
| 345 | # Regression test for SF bug #921657. |
| 346 | self._run_check( |
| 347 | "<a href=mailto:xyz@example.com>", |
| 348 | [("starttag", "a", [("href", "mailto:xyz@example.com")])]) |
| 349 | |
| 350 | def test_attr_nonascii(self): |
| 351 | # see issue 7311 |
| 352 | self._run_check( |
| 353 | u"<img src=/foo/bar.png alt=\u4e2d\u6587>", |
| 354 | [("starttag", "img", [("src", "/foo/bar.png"), |
| 355 | ("alt", u"\u4e2d\u6587")])]) |
| 356 | self._run_check( |
| 357 | u"<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>", |
| 358 | [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"), |
| 359 | ("href", u"\u30c6\u30b9\u30c8.html")])]) |
| 360 | self._run_check( |
| 361 | u'<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">', |
| 362 | [("starttag", "a", [("title", u"\u30c6\u30b9\u30c8"), |
| 363 | ("href", u"\u30c6\u30b9\u30c8.html")])]) |
| 364 | |
| 365 | def test_attr_entity_replacement(self): |
| 366 | self._run_check( |
| 367 | "<a b='&><"''>", |
| 368 | [("starttag", "a", [("b", "&><\"'")])]) |
| 369 | |
| 370 | def test_attr_funky_names(self): |
| 371 | self._run_check( |
| 372 | "<a a.b='v' c:d=v e-f=v>", |
| 373 | [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])]) |
Ezio Melotti | 0f1571c | 2011-11-14 18:04:05 +0200 | [diff] [blame] | 374 | self._run_check( |
| 375 | "<a $><b $=%><c \=/>", |
| 376 | [("starttag", "a", [("$", None)]), |
| 377 | ("starttag", "b", [("$", "%")]), |
| 378 | ("starttag", "c", [("\\", "/")])]) |
Ezio Melotti | 7459291 | 2011-11-08 02:07:18 +0200 | [diff] [blame] | 379 | |
| 380 | def test_entityrefs_in_attributes(self): |
| 381 | self._run_check( |
| 382 | "<html foo='€&aa&unsupported;'>", |
| 383 | [("starttag", "html", [("foo", u"\u20AC&aa&unsupported;")])]) |
| 384 | |
Ezio Melotti | 0f1571c | 2011-11-14 18:04:05 +0200 | [diff] [blame] | 385 | def test_entities_in_attribute_value(self): |
| 386 | # see #1200313 |
| 387 | for entity in ['&', '&', '&', '&']: |
| 388 | self._run_check('<a href="%s">' % entity, |
| 389 | [("starttag", "a", [("href", "&")])]) |
| 390 | self._run_check("<a href='%s'>" % entity, |
| 391 | [("starttag", "a", [("href", "&")])]) |
| 392 | self._run_check("<a href=%s>" % entity, |
| 393 | [("starttag", "a", [("href", "&")])]) |
| 394 | |
| 395 | def test_malformed_attributes(self): |
| 396 | # see #13357 |
| 397 | html = ( |
| 398 | "<a href=test'style='color:red;bad1'>test - bad1</a>" |
| 399 | "<a href=test'+style='color:red;ba2'>test - bad2</a>" |
| 400 | "<a href=test' style='color:red;bad3'>test - bad3</a>" |
| 401 | "<a href = test' style='color:red;bad4' >test - bad4</a>" |
| 402 | ) |
| 403 | expected = [ |
| 404 | ('starttag', 'a', [('href', "test'style='color:red;bad1'")]), |
| 405 | ('data', 'test - bad1'), ('endtag', 'a'), |
| 406 | ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]), |
| 407 | ('data', 'test - bad2'), ('endtag', 'a'), |
| 408 | ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad3'")]), |
| 409 | ('data', 'test - bad3'), ('endtag', 'a'), |
| 410 | ('starttag', 'a', [('href', u"test'\xa0style='color:red;bad4'")]), |
| 411 | ('data', 'test - bad4'), ('endtag', 'a') |
| 412 | ] |
| 413 | self._run_check(html, expected) |
| 414 | |
| 415 | def test_malformed_adjacent_attributes(self): |
| 416 | # see #12629 |
| 417 | self._run_check('<x><y z=""o"" /></x>', |
| 418 | [('starttag', 'x', []), |
| 419 | ('startendtag', 'y', [('z', ''), ('o""', None)]), |
| 420 | ('endtag', 'x')]) |
| 421 | self._run_check('<x><y z="""" /></x>', |
| 422 | [('starttag', 'x', []), |
| 423 | ('startendtag', 'y', [('z', ''), ('""', None)]), |
| 424 | ('endtag', 'x')]) |
| 425 | |
| 426 | # see #755670 for the following 3 tests |
| 427 | def test_adjacent_attributes(self): |
| 428 | self._run_check('<a width="100%"cellspacing=0>', |
| 429 | [("starttag", "a", |
| 430 | [("width", "100%"), ("cellspacing","0")])]) |
| 431 | |
| 432 | self._run_check('<a id="foo"class="bar">', |
| 433 | [("starttag", "a", |
| 434 | [("id", "foo"), ("class","bar")])]) |
| 435 | |
| 436 | def test_missing_attribute_value(self): |
| 437 | self._run_check('<a v=>', |
| 438 | [("starttag", "a", [("v", "")])]) |
| 439 | |
| 440 | def test_javascript_attribute_value(self): |
| 441 | self._run_check("<a href=javascript:popup('/popup/help.html')>", |
| 442 | [("starttag", "a", |
| 443 | [("href", "javascript:popup('/popup/help.html')")])]) |
| 444 | |
| 445 | def test_end_tag_in_attribute_value(self): |
| 446 | # see #1745761 |
| 447 | self._run_check("<a href='http://www.example.org/\">;'>spam</a>", |
| 448 | [("starttag", "a", |
| 449 | [("href", "http://www.example.org/\">;")]), |
| 450 | ("data", "spam"), ("endtag", "a")]) |
| 451 | |
| 452 | |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 453 | def test_main(): |
Ezio Melotti | 7459291 | 2011-11-08 02:07:18 +0200 | [diff] [blame] | 454 | test_support.run_unittest(HTMLParserTestCase, AttributesTestCase) |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | if __name__ == "__main__": |
| 458 | test_main() |