Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 1 | """Tests for HTMLParser.py.""" |
| 2 | |
Mark Dickinson | f64dcf3 | 2008-05-21 13:51:18 +0000 | [diff] [blame] | 3 | import html.parser |
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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 7 | |
| 8 | |
Mark Dickinson | f64dcf3 | 2008-05-21 13:51:18 +0000 | [diff] [blame] | 9 | class EventCollector(html.parser.HTMLParser): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 10 | |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 11 | def __init__(self, *args, **kw): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 12 | self.events = [] |
| 13 | self.append = self.events.append |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 14 | html.parser.HTMLParser.__init__(self, *args, **kw) |
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 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 75 | def get_collector(self): |
| 76 | raise NotImplementedError |
| 77 | |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 78 | def _run_check(self, source, expected_events, collector=None): |
| 79 | if collector is None: |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 80 | collector = self.get_collector() |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 81 | parser = collector |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 82 | for s in source: |
| 83 | parser.feed(s) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 84 | parser.close() |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 85 | events = parser.get_events() |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 86 | 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 Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 90 | |
| 91 | def _run_check_extra(self, source, events): |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 92 | self._run_check(source, events, EventCollectorExtra()) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 93 | |
| 94 | def _parse_error(self, source): |
| 95 | def parse(source=source): |
Mark Dickinson | f64dcf3 | 2008-05-21 13:51:18 +0000 | [diff] [blame] | 96 | parser = html.parser.HTMLParser() |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 97 | parser.feed(source) |
| 98 | parser.close() |
Mark Dickinson | f64dcf3 | 2008-05-21 13:51:18 +0000 | [diff] [blame] | 99 | self.assertRaises(html.parser.HTMLParseError, parse) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 100 | |
| 101 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 102 | class HTMLParserStrictTestCase(TestCaseBase): |
| 103 | |
| 104 | def get_collector(self): |
| 105 | return EventCollector(strict=True) |
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_processing_instruction_only(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 108 | self._run_check("<?processing instruction>", [ |
| 109 | ("pi", "processing instruction"), |
| 110 | ]) |
Fred Drake | fafd56f | 2003-04-17 22:19:26 +0000 | [diff] [blame] | 111 | self._run_check("<?processing instruction ?>", [ |
| 112 | ("pi", "processing instruction ?"), |
| 113 | ]) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 114 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 115 | def test_simple_html(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 116 | self._run_check(""" |
| 117 | <!DOCTYPE html PUBLIC 'foo'> |
| 118 | <HTML>&entity;  |
| 119 | <!--comment1a |
| 120 | -></foo><bar><<?pi?></foo<bar |
| 121 | comment1b--> |
| 122 | <Img sRc='Bar' isMAP>sample |
| 123 | text |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 124 | “ |
Georg Brandl | d09def3 | 2006-03-09 13:27:14 +0000 | [diff] [blame] | 125 | <!--comment2a-- --comment2b--><!> |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 126 | </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><<?pi?></foo<bar\ncomment1b"), |
| 136 | ("data", "\n"), |
| 137 | ("starttag", "img", [("src", "Bar"), ("ismap", None)]), |
| 138 | ("data", "sample\ntext\n"), |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 139 | ("charref", "x201C"), |
| 140 | ("data", "\n"), |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 141 | ("comment", "comment2a-- --comment2b"), |
| 142 | ("data", "\n"), |
| 143 | ("endtag", "html"), |
| 144 | ("data", "\n"), |
| 145 | ]) |
| 146 | |
Victor Stinner | e021f4b | 2010-05-24 21:46:25 +0000 | [diff] [blame] | 147 | def test_malformatted_charref(self): |
| 148 | self._run_check("<p>&#bad;</p>", [ |
| 149 | ("starttag", "p", []), |
| 150 | ("data", "&#bad;"), |
| 151 | ("endtag", "p"), |
| 152 | ]) |
| 153 | |
Fred Drake | 073148c | 2001-12-03 16:44:09 +0000 | [diff] [blame] | 154 | def test_unclosed_entityref(self): |
| 155 | self._run_check("&entityref foo", [ |
| 156 | ("entityref", "entityref"), |
| 157 | ("data", " foo"), |
| 158 | ]) |
| 159 | |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 160 | def test_doctype_decl(self): |
| 161 | inside = """\ |
| 162 | DOCTYPE html [ |
| 163 | <!ELEMENT html - O EMPTY> |
| 164 | <!ATTLIST html |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 165 | 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 Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 172 | <!-- comment --> |
| 173 | ]""" |
| 174 | self._run_check("<!%s>" % inside, [ |
| 175 | ("decl", inside), |
| 176 | ]) |
| 177 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 178 | 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 Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 182 | self._run_check("<a><b></a></b>", [ |
| 183 | ("starttag", "a", []), |
| 184 | ("starttag", "b", []), |
| 185 | ("endtag", "a"), |
| 186 | ("endtag", "b"), |
| 187 | ]) |
| 188 | |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 189 | 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 Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 199 | def test_illegal_declarations(self): |
Fred Drake | 7cf613d | 2001-09-04 16:26:03 +0000 | [diff] [blame] | 200 | self._parse_error('<!spacer type="block" height="25">') |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 201 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 202 | def test_starttag_end_boundary(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 203 | self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])]) |
| 204 | self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])]) |
| 205 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 206 | def test_buffer_artefacts(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 207 | 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 Drake | 75d9a62 | 2004-09-08 22:57:01 +0000 | [diff] [blame] | 223 | 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 Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 236 | def test_starttag_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 237 | self._parse_error("</>") |
| 238 | self._parse_error("</$>") |
| 239 | self._parse_error("</") |
| 240 | self._parse_error("</a") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 241 | self._parse_error("<a<a>") |
| 242 | self._parse_error("</a<a>") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 243 | self._parse_error("<!") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 244 | 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 Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 249 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 250 | def test_declaration_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 251 | self._parse_error("<!DOCTYPE foo $ >") |
| 252 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 253 | def test_startendtag(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 254 | 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 Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 267 | def test_get_starttag_text(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 268 | 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 Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 273 | def test_cdata_content(self): |
Ezio Melotti | 7de56f6 | 2011-11-01 14:12:22 +0200 | [diff] [blame] | 274 | contents = [ |
| 275 | '<!-- not a comment --> ¬-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 Melotti | 15cb489 | 2011-11-18 18:01:49 +0200 | [diff] [blame] | 304 | 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 Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 312 | |
Ezio Melotti | 15cb489 | 2011-11-18 18:01:49 +0200 | [diff] [blame] | 313 | content = """<!-- not a comment --> ¬-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 Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 325 | |
Ezio Melotti | 62f3d03 | 2011-12-19 07:29:03 +0200 | [diff] [blame] | 326 | def test_condcoms(self): |
| 327 | html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->' |
| 328 | '<!--[if IE 8]>condcoms<![endif]-->' |
| 329 | '<!--[if lte IE 7]>pretty?<![endif]-->') |
| 330 | expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"), |
| 331 | ('comment', '[if IE 8]>condcoms<![endif]'), |
| 332 | ('comment', '[if lte IE 7]>pretty?<![endif]')] |
| 333 | self._run_check(html, expected) |
| 334 | |
| 335 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 336 | class HTMLParserTolerantTestCase(HTMLParserStrictTestCase): |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 337 | |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 338 | def get_collector(self): |
| 339 | return EventCollector(strict=False) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 340 | |
| 341 | def test_tolerant_parsing(self): |
| 342 | self._run_check('<html <html>te>>xt&a<<bc</a></html>\n' |
| 343 | '<img src="URL><//img></html</html>', [ |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 344 | ('starttag', 'html', [('<html', None)]), |
| 345 | ('data', 'te>>xt'), |
| 346 | ('entityref', 'a'), |
| 347 | ('data', '<<bc'), |
| 348 | ('endtag', 'a'), |
| 349 | ('endtag', 'html'), |
| 350 | ('data', '\n<img src="URL><//img></html'), |
| 351 | ('endtag', 'html')]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 352 | |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 353 | def test_with_unquoted_attributes(self): |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 354 | # see #12008 |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 355 | html = ("<html><body bgcolor=d0ca90 text='181008'>" |
| 356 | "<table cellspacing=0 cellpadding=1 width=100% ><tr>" |
| 357 | "<td align=left><font size=-1>" |
| 358 | "- <a href=/rabota/><span class=en> software-and-i</span></a>" |
| 359 | "- <a href='/1/'><span class=en> library</span></a></table>") |
| 360 | expected = [ |
| 361 | ('starttag', 'html', []), |
| 362 | ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]), |
| 363 | ('starttag', 'table', |
| 364 | [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]), |
| 365 | ('starttag', 'tr', []), |
| 366 | ('starttag', 'td', [('align', 'left')]), |
| 367 | ('starttag', 'font', [('size', '-1')]), |
| 368 | ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]), |
| 369 | ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'), |
| 370 | ('endtag', 'span'), ('endtag', 'a'), |
| 371 | ('data', '- '), ('starttag', 'a', [('href', '/1/')]), |
| 372 | ('starttag', 'span', [('class', 'en')]), ('data', ' library'), |
| 373 | ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table') |
| 374 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 375 | self._run_check(html, expected) |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 376 | |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 377 | def test_comma_between_attributes(self): |
| 378 | self._run_check('<form action="/xxx.php?a=1&b=2&", ' |
| 379 | 'method="post">', [ |
| 380 | ('starttag', 'form', |
| 381 | [('action', '/xxx.php?a=1&b=2&'), |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 382 | (',', None), ('method', 'post')])]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 383 | |
| 384 | def test_weird_chars_in_unquoted_attribute_values(self): |
| 385 | self._run_check('<form action=bogus|&#()value>', [ |
| 386 | ('starttag', 'form', |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 387 | [('action', 'bogus|&#()value')])]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 388 | |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 389 | def test_correct_detection_of_start_tags(self): |
| 390 | # see #13273 |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 391 | html = ('<div style="" ><b>The <a href="some_url">rain</a> ' |
| 392 | '<br /> in <span>Spain</span></b></div>') |
| 393 | expected = [ |
| 394 | ('starttag', 'div', [('style', '')]), |
| 395 | ('starttag', 'b', []), |
| 396 | ('data', 'The '), |
| 397 | ('starttag', 'a', [('href', 'some_url')]), |
| 398 | ('data', 'rain'), |
| 399 | ('endtag', 'a'), |
| 400 | ('data', ' '), |
| 401 | ('startendtag', 'br', []), |
| 402 | ('data', ' in '), |
| 403 | ('starttag', 'span', []), |
| 404 | ('data', 'Spain'), |
| 405 | ('endtag', 'span'), |
| 406 | ('endtag', 'b'), |
| 407 | ('endtag', 'div') |
| 408 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 409 | self._run_check(html, expected) |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 410 | |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 411 | html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>' |
| 412 | expected = [ |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 413 | ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]), |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 414 | ('starttag', 'b', []), |
| 415 | ('data', 'The '), |
| 416 | ('starttag', 'a', [('href', 'some_url')]), |
| 417 | ('data', 'rain'), |
| 418 | ('endtag', 'a'), |
| 419 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 420 | self._run_check(html, expected) |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 421 | |
Senthil Kumaran | 164540f | 2010-12-28 15:55:16 +0000 | [diff] [blame] | 422 | def test_unescape_function(self): |
| 423 | p = html.parser.HTMLParser() |
| 424 | self.assertEqual(p.unescape('&#bad;'),'&#bad;') |
| 425 | self.assertEqual(p.unescape('&'),'&') |
Ezio Melotti | d9e0b06 | 2011-09-05 17:11:06 +0300 | [diff] [blame] | 426 | # see #12888 |
| 427 | self.assertEqual(p.unescape('{ ' * 1050), '{ ' * 1050) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 428 | |
Ezio Melotti | 62f3d03 | 2011-12-19 07:29:03 +0200 | [diff] [blame] | 429 | def test_broken_condcoms(self): |
| 430 | # these condcoms are missing the '--' after '<!' and before the '>' |
| 431 | html = ('<![if !(IE)]>broken condcom<![endif]>' |
| 432 | '<![if ! IE]><link href="favicon.tiff"/><![endif]>' |
| 433 | '<![if !IE 6]><img src="firefox.png" /><![endif]>' |
| 434 | '<![if !ie 6]><b>foo</b><![endif]>' |
| 435 | '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>') |
| 436 | # According to the HTML5 specs sections "8.2.4.44 Bogus comment state" |
| 437 | # and "8.2.4.45 Markup declaration open state", comment tokens should |
| 438 | # be emitted instead of 'unknown decl', but calling unknown_decl |
| 439 | # provides more flexibility. |
| 440 | # See also Lib/_markupbase.py:parse_declaration |
| 441 | expected = [ |
| 442 | ('unknown decl', 'if !(IE)'), |
| 443 | ('data', 'broken condcom'), |
| 444 | ('unknown decl', 'endif'), |
| 445 | ('unknown decl', 'if ! IE'), |
| 446 | ('startendtag', 'link', [('href', 'favicon.tiff')]), |
| 447 | ('unknown decl', 'endif'), |
| 448 | ('unknown decl', 'if !IE 6'), |
| 449 | ('startendtag', 'img', [('src', 'firefox.png')]), |
| 450 | ('unknown decl', 'endif'), |
| 451 | ('unknown decl', 'if !ie 6'), |
| 452 | ('starttag', 'b', []), |
| 453 | ('data', 'foo'), |
| 454 | ('endtag', 'b'), |
| 455 | ('unknown decl', 'endif'), |
| 456 | ('unknown decl', 'if (!IE)|(lt IE 9)'), |
| 457 | ('startendtag', 'img', [('src', 'mammoth.bmp')]), |
| 458 | ('unknown decl', 'endif') |
| 459 | ] |
| 460 | self._run_check(html, expected) |
| 461 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 462 | |
Ezio Melotti | b245ed1 | 2011-11-14 18:13:22 +0200 | [diff] [blame] | 463 | class AttributesStrictTestCase(TestCaseBase): |
| 464 | |
| 465 | def get_collector(self): |
| 466 | return EventCollector(strict=True) |
| 467 | |
| 468 | def test_attr_syntax(self): |
| 469 | output = [ |
| 470 | ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)]) |
| 471 | ] |
| 472 | self._run_check("""<a b='v' c="v" d=v e>""", output) |
| 473 | self._run_check("""<a b = 'v' c = "v" d = v e>""", output) |
| 474 | self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output) |
| 475 | self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output) |
| 476 | |
| 477 | def test_attr_values(self): |
| 478 | self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", |
| 479 | [("starttag", "a", [("b", "xxx\n\txxx"), |
| 480 | ("c", "yyy\t\nyyy"), |
| 481 | ("d", "\txyz\n")])]) |
| 482 | self._run_check("""<a b='' c="">""", |
| 483 | [("starttag", "a", [("b", ""), ("c", "")])]) |
| 484 | # Regression test for SF patch #669683. |
| 485 | self._run_check("<e a=rgb(1,2,3)>", |
| 486 | [("starttag", "e", [("a", "rgb(1,2,3)")])]) |
| 487 | # Regression test for SF bug #921657. |
| 488 | self._run_check( |
| 489 | "<a href=mailto:xyz@example.com>", |
| 490 | [("starttag", "a", [("href", "mailto:xyz@example.com")])]) |
| 491 | |
| 492 | def test_attr_nonascii(self): |
| 493 | # see issue 7311 |
| 494 | self._run_check( |
| 495 | "<img src=/foo/bar.png alt=\u4e2d\u6587>", |
| 496 | [("starttag", "img", [("src", "/foo/bar.png"), |
| 497 | ("alt", "\u4e2d\u6587")])]) |
| 498 | self._run_check( |
| 499 | "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>", |
| 500 | [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), |
| 501 | ("href", "\u30c6\u30b9\u30c8.html")])]) |
| 502 | self._run_check( |
| 503 | '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">', |
| 504 | [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), |
| 505 | ("href", "\u30c6\u30b9\u30c8.html")])]) |
| 506 | |
| 507 | def test_attr_entity_replacement(self): |
| 508 | self._run_check( |
| 509 | "<a b='&><"''>", |
| 510 | [("starttag", "a", [("b", "&><\"'")])]) |
| 511 | |
| 512 | def test_attr_funky_names(self): |
| 513 | self._run_check( |
| 514 | "<a a.b='v' c:d=v e-f=v>", |
| 515 | [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])]) |
| 516 | |
| 517 | def test_entityrefs_in_attributes(self): |
| 518 | self._run_check( |
| 519 | "<html foo='€&aa&unsupported;'>", |
| 520 | [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])]) |
| 521 | |
| 522 | |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 523 | |
| 524 | class AttributesTolerantTestCase(AttributesStrictTestCase): |
| 525 | |
| 526 | def get_collector(self): |
| 527 | return EventCollector(strict=False) |
| 528 | |
| 529 | def test_attr_funky_names2(self): |
| 530 | self._run_check( |
| 531 | "<a $><b $=%><c \=/>", |
| 532 | [("starttag", "a", [("$", None)]), |
| 533 | ("starttag", "b", [("$", "%")]), |
| 534 | ("starttag", "c", [("\\", "/")])]) |
| 535 | |
| 536 | def test_entities_in_attribute_value(self): |
| 537 | # see #1200313 |
| 538 | for entity in ['&', '&', '&', '&']: |
| 539 | self._run_check('<a href="%s">' % entity, |
| 540 | [("starttag", "a", [("href", "&")])]) |
| 541 | self._run_check("<a href='%s'>" % entity, |
| 542 | [("starttag", "a", [("href", "&")])]) |
| 543 | self._run_check("<a href=%s>" % entity, |
| 544 | [("starttag", "a", [("href", "&")])]) |
| 545 | |
| 546 | def test_malformed_attributes(self): |
| 547 | # see #13357 |
| 548 | html = ( |
| 549 | "<a href=test'style='color:red;bad1'>test - bad1</a>" |
| 550 | "<a href=test'+style='color:red;ba2'>test - bad2</a>" |
| 551 | "<a href=test' style='color:red;bad3'>test - bad3</a>" |
| 552 | "<a href = test' style='color:red;bad4' >test - bad4</a>" |
| 553 | ) |
| 554 | expected = [ |
| 555 | ('starttag', 'a', [('href', "test'style='color:red;bad1'")]), |
| 556 | ('data', 'test - bad1'), ('endtag', 'a'), |
| 557 | ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]), |
| 558 | ('data', 'test - bad2'), ('endtag', 'a'), |
| 559 | ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]), |
| 560 | ('data', 'test - bad3'), ('endtag', 'a'), |
| 561 | ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]), |
| 562 | ('data', 'test - bad4'), ('endtag', 'a') |
| 563 | ] |
| 564 | self._run_check(html, expected) |
| 565 | |
| 566 | def test_malformed_adjacent_attributes(self): |
| 567 | # see #12629 |
| 568 | self._run_check('<x><y z=""o"" /></x>', |
| 569 | [('starttag', 'x', []), |
| 570 | ('startendtag', 'y', [('z', ''), ('o""', None)]), |
| 571 | ('endtag', 'x')]) |
| 572 | self._run_check('<x><y z="""" /></x>', |
| 573 | [('starttag', 'x', []), |
| 574 | ('startendtag', 'y', [('z', ''), ('""', None)]), |
| 575 | ('endtag', 'x')]) |
| 576 | |
| 577 | # see #755670 for the following 3 tests |
| 578 | def test_adjacent_attributes(self): |
| 579 | self._run_check('<a width="100%"cellspacing=0>', |
| 580 | [("starttag", "a", |
| 581 | [("width", "100%"), ("cellspacing","0")])]) |
| 582 | |
| 583 | self._run_check('<a id="foo"class="bar">', |
| 584 | [("starttag", "a", |
| 585 | [("id", "foo"), ("class","bar")])]) |
| 586 | |
| 587 | def test_missing_attribute_value(self): |
| 588 | self._run_check('<a v=>', |
| 589 | [("starttag", "a", [("v", "")])]) |
| 590 | |
| 591 | def test_javascript_attribute_value(self): |
| 592 | self._run_check("<a href=javascript:popup('/popup/help.html')>", |
| 593 | [("starttag", "a", |
| 594 | [("href", "javascript:popup('/popup/help.html')")])]) |
| 595 | |
| 596 | def test_end_tag_in_attribute_value(self): |
| 597 | # see #1745761 |
| 598 | self._run_check("<a href='http://www.example.org/\">;'>spam</a>", |
| 599 | [("starttag", "a", |
| 600 | [("href", "http://www.example.org/\">;")]), |
| 601 | ("data", "spam"), ("endtag", "a")]) |
| 602 | |
| 603 | |
| 604 | |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 605 | def test_main(): |
Ezio Melotti | b245ed1 | 2011-11-14 18:13:22 +0200 | [diff] [blame] | 606 | support.run_unittest(HTMLParserStrictTestCase, HTMLParserTolerantTestCase, |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 607 | AttributesStrictTestCase, AttributesTolerantTestCase) |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 608 | |
| 609 | |
| 610 | if __name__ == "__main__": |
| 611 | test_main() |