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): |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 96 | parser = self.get_collector() |
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 | “ |
Ezio Melotti | f4ab491 | 2012-02-13 15:50:37 +0200 | [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 | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 160 | def test_bad_nesting(self): |
| 161 | # Strangely, this *is* supposed to test that overlapping |
| 162 | # elements are allowed. HTMLParser is more geared toward |
| 163 | # lexing the input that parsing the structure. |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 164 | self._run_check("<a><b></a></b>", [ |
| 165 | ("starttag", "a", []), |
| 166 | ("starttag", "b", []), |
| 167 | ("endtag", "a"), |
| 168 | ("endtag", "b"), |
| 169 | ]) |
| 170 | |
Fred Drake | 029acfb | 2001-08-20 21:24:19 +0000 | [diff] [blame] | 171 | def test_bare_ampersands(self): |
| 172 | self._run_check("this text & contains & ampersands &", [ |
| 173 | ("data", "this text & contains & ampersands &"), |
| 174 | ]) |
| 175 | |
| 176 | def test_bare_pointy_brackets(self): |
| 177 | self._run_check("this < text > contains < bare>pointy< brackets", [ |
| 178 | ("data", "this < text > contains < bare>pointy< brackets"), |
| 179 | ]) |
| 180 | |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 181 | def test_illegal_declarations(self): |
Fred Drake | 7cf613d | 2001-09-04 16:26:03 +0000 | [diff] [blame] | 182 | self._parse_error('<!spacer type="block" height="25">') |
Fred Drake | c20a698 | 2001-09-04 15:13:04 +0000 | [diff] [blame] | 183 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 184 | def test_starttag_end_boundary(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 185 | self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])]) |
| 186 | self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])]) |
| 187 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 188 | def test_buffer_artefacts(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 189 | output = [("starttag", "a", [("b", "<")])] |
| 190 | self._run_check(["<a b='<'>"], output) |
| 191 | self._run_check(["<a ", "b='<'>"], output) |
| 192 | self._run_check(["<a b", "='<'>"], output) |
| 193 | self._run_check(["<a b=", "'<'>"], output) |
| 194 | self._run_check(["<a b='<", "'>"], output) |
| 195 | self._run_check(["<a b='<'", ">"], output) |
| 196 | |
| 197 | output = [("starttag", "a", [("b", ">")])] |
| 198 | self._run_check(["<a b='>'>"], output) |
| 199 | self._run_check(["<a ", "b='>'>"], output) |
| 200 | self._run_check(["<a b", "='>'>"], output) |
| 201 | self._run_check(["<a b=", "'>'>"], output) |
| 202 | self._run_check(["<a b='>", "'>"], output) |
| 203 | self._run_check(["<a b='>'", ">"], output) |
| 204 | |
Fred Drake | 75d9a62 | 2004-09-08 22:57:01 +0000 | [diff] [blame] | 205 | output = [("comment", "abc")] |
| 206 | self._run_check(["", "<!--abc-->"], output) |
| 207 | self._run_check(["<", "!--abc-->"], output) |
| 208 | self._run_check(["<!", "--abc-->"], output) |
| 209 | self._run_check(["<!-", "-abc-->"], output) |
| 210 | self._run_check(["<!--", "abc-->"], output) |
| 211 | self._run_check(["<!--a", "bc-->"], output) |
| 212 | self._run_check(["<!--ab", "c-->"], output) |
| 213 | self._run_check(["<!--abc", "-->"], output) |
| 214 | self._run_check(["<!--abc-", "->"], output) |
| 215 | self._run_check(["<!--abc--", ">"], output) |
| 216 | self._run_check(["<!--abc-->", ""], output) |
| 217 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 218 | def test_starttag_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 219 | self._parse_error("</>") |
| 220 | self._parse_error("</$>") |
| 221 | self._parse_error("</") |
| 222 | self._parse_error("</a") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 223 | self._parse_error("<a<a>") |
| 224 | self._parse_error("</a<a>") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 225 | self._parse_error("<!") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 226 | self._parse_error("<a") |
| 227 | self._parse_error("<a foo='bar'") |
| 228 | self._parse_error("<a foo='bar") |
| 229 | self._parse_error("<a foo='>'") |
| 230 | self._parse_error("<a foo='>") |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 231 | |
Ezio Melotti | f4ab491 | 2012-02-13 15:50:37 +0200 | [diff] [blame] | 232 | def test_valid_doctypes(self): |
| 233 | # from http://www.w3.org/QA/2002/04/valid-dtd-list.html |
| 234 | dtds = ['HTML', # HTML5 doctype |
| 235 | ('HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ' |
| 236 | '"http://www.w3.org/TR/html4/strict.dtd"'), |
| 237 | ('HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ' |
| 238 | '"http://www.w3.org/TR/html4/loose.dtd"'), |
| 239 | ('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' |
| 240 | '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'), |
| 241 | ('html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" ' |
| 242 | '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'), |
| 243 | ('math PUBLIC "-//W3C//DTD MathML 2.0//EN" ' |
| 244 | '"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"'), |
| 245 | ('html PUBLIC "-//W3C//DTD ' |
| 246 | 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" ' |
| 247 | '"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"'), |
| 248 | ('svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' |
| 249 | '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"'), |
| 250 | 'html PUBLIC "-//IETF//DTD HTML 2.0//EN"', |
| 251 | 'html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"'] |
| 252 | for dtd in dtds: |
| 253 | self._run_check("<!DOCTYPE %s>" % dtd, |
| 254 | [('decl', 'DOCTYPE ' + dtd)]) |
| 255 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 256 | def test_declaration_junk_chars(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 257 | self._parse_error("<!DOCTYPE foo $ >") |
| 258 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 259 | def test_startendtag(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 260 | self._run_check("<p/>", [ |
| 261 | ("startendtag", "p", []), |
| 262 | ]) |
| 263 | self._run_check("<p></p>", [ |
| 264 | ("starttag", "p", []), |
| 265 | ("endtag", "p"), |
| 266 | ]) |
| 267 | self._run_check("<p><img src='foo' /></p>", [ |
| 268 | ("starttag", "p", []), |
| 269 | ("startendtag", "img", [("src", "foo")]), |
| 270 | ("endtag", "p"), |
| 271 | ]) |
| 272 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 273 | def test_get_starttag_text(self): |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 274 | s = """<foo:bar \n one="1"\ttwo=2 >""" |
| 275 | self._run_check_extra(s, [ |
| 276 | ("starttag", "foo:bar", [("one", "1"), ("two", "2")]), |
| 277 | ("starttag_text", s)]) |
| 278 | |
Fred Drake | 84bb9d8 | 2001-08-03 19:53:01 +0000 | [diff] [blame] | 279 | def test_cdata_content(self): |
Ezio Melotti | 7de56f6 | 2011-11-01 14:12:22 +0200 | [diff] [blame] | 280 | contents = [ |
| 281 | '<!-- not a comment --> ¬-an-entity-ref;', |
| 282 | "<not a='start tag'>", |
| 283 | '<a href="" /> <p> <span></span>', |
| 284 | 'foo = "</scr" + "ipt>";', |
| 285 | 'foo = "</SCRIPT" + ">";', |
| 286 | 'foo = <\n/script> ', |
| 287 | '<!-- document.write("</scr" + "ipt>"); -->', |
| 288 | ('\n//<![CDATA[\n' |
| 289 | 'document.write(\'<s\'+\'cript type="text/javascript" ' |
| 290 | 'src="http://www.example.org/r=\'+new ' |
| 291 | 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'), |
| 292 | '\n<!-- //\nvar foo = 3.14;\n// -->\n', |
| 293 | 'foo = "</sty" + "le>";', |
| 294 | '<!-- \u2603 -->', |
| 295 | # these two should be invalid according to the HTML 5 spec, |
| 296 | # section 8.1.2.2 |
| 297 | #'foo = </\nscript>', |
| 298 | #'foo = </ script>', |
| 299 | ] |
| 300 | elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style'] |
| 301 | for content in contents: |
| 302 | for element in elements: |
| 303 | element_lower = element.lower() |
| 304 | s = '<{element}>{content}</{element}>'.format(element=element, |
| 305 | content=content) |
| 306 | self._run_check(s, [("starttag", element_lower, []), |
| 307 | ("data", content), |
| 308 | ("endtag", element_lower)]) |
| 309 | |
Ezio Melotti | 15cb489 | 2011-11-18 18:01:49 +0200 | [diff] [blame] | 310 | def test_cdata_with_closing_tags(self): |
| 311 | # see issue #13358 |
| 312 | # make sure that HTMLParser calls handle_data only once for each CDATA. |
| 313 | # The normal event collector normalizes the events in get_events, |
| 314 | # so we override it to return the original list of events. |
| 315 | class Collector(EventCollector): |
| 316 | def get_events(self): |
| 317 | return self.events |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 318 | |
Ezio Melotti | 15cb489 | 2011-11-18 18:01:49 +0200 | [diff] [blame] | 319 | content = """<!-- not a comment --> ¬-an-entity-ref; |
| 320 | <a href="" /> </p><p> <span></span></style> |
| 321 | '</script' + '>'""" |
| 322 | for element in [' script', 'script ', ' script ', |
| 323 | '\nscript', 'script\n', '\nscript\n']: |
| 324 | element_lower = element.lower().strip() |
| 325 | s = '<script>{content}</{element}>'.format(element=element, |
| 326 | content=content) |
| 327 | self._run_check(s, [("starttag", element_lower, []), |
| 328 | ("data", content), |
| 329 | ("endtag", element_lower)], |
| 330 | collector=Collector()) |
Fred Drake | bd3090d | 2001-05-18 15:32:59 +0000 | [diff] [blame] | 331 | |
Ezio Melotti | fa3702d | 2012-02-10 10:45:44 +0200 | [diff] [blame] | 332 | def test_comments(self): |
| 333 | html = ("<!-- I'm a valid comment -->" |
| 334 | '<!--me too!-->' |
| 335 | '<!------>' |
| 336 | '<!---->' |
| 337 | '<!----I have many hyphens---->' |
| 338 | '<!-- I have a > in the middle -->' |
| 339 | '<!-- and I have -- in the middle! -->') |
| 340 | expected = [('comment', " I'm a valid comment "), |
| 341 | ('comment', 'me too!'), |
| 342 | ('comment', '--'), |
| 343 | ('comment', ''), |
| 344 | ('comment', '--I have many hyphens--'), |
| 345 | ('comment', ' I have a > in the middle '), |
| 346 | ('comment', ' and I have -- in the middle! ')] |
| 347 | self._run_check(html, expected) |
| 348 | |
Ezio Melotti | 62f3d03 | 2011-12-19 07:29:03 +0200 | [diff] [blame] | 349 | def test_condcoms(self): |
| 350 | html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->' |
| 351 | '<!--[if IE 8]>condcoms<![endif]-->' |
| 352 | '<!--[if lte IE 7]>pretty?<![endif]-->') |
| 353 | expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"), |
| 354 | ('comment', '[if IE 8]>condcoms<![endif]'), |
| 355 | ('comment', '[if lte IE 7]>pretty?<![endif]')] |
| 356 | self._run_check(html, expected) |
| 357 | |
| 358 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 359 | class HTMLParserTolerantTestCase(HTMLParserStrictTestCase): |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 360 | |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 361 | def get_collector(self): |
| 362 | return EventCollector(strict=False) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 363 | |
| 364 | def test_tolerant_parsing(self): |
| 365 | self._run_check('<html <html>te>>xt&a<<bc</a></html>\n' |
| 366 | '<img src="URL><//img></html</html>', [ |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 367 | ('starttag', 'html', [('<html', None)]), |
| 368 | ('data', 'te>>xt'), |
| 369 | ('entityref', 'a'), |
| 370 | ('data', '<<bc'), |
| 371 | ('endtag', 'a'), |
| 372 | ('endtag', 'html'), |
Ezio Melotti | 5211ffe | 2012-02-13 11:24:50 +0200 | [diff] [blame] | 373 | ('data', '\n<img src="URL>'), |
| 374 | ('comment', '/img'), |
| 375 | ('endtag', 'html<')]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 376 | |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 377 | def test_starttag_junk_chars(self): |
| 378 | self._run_check("</>", []) |
| 379 | self._run_check("</$>", [('comment', '$')]) |
| 380 | self._run_check("</", [('data', '</')]) |
| 381 | self._run_check("</a", [('data', '</a')]) |
| 382 | # XXX this might be wrong |
| 383 | self._run_check("<a<a>", [('data', '<a'), ('starttag', 'a', [])]) |
| 384 | self._run_check("</a<a>", [('endtag', 'a<a')]) |
| 385 | self._run_check("<!", [('data', '<!')]) |
| 386 | self._run_check("<a", [('data', '<a')]) |
| 387 | self._run_check("<a foo='bar'", [('data', "<a foo='bar'")]) |
| 388 | self._run_check("<a foo='bar", [('data', "<a foo='bar")]) |
| 389 | self._run_check("<a foo='>'", [('data', "<a foo='>'")]) |
| 390 | self._run_check("<a foo='>", [('data', "<a foo='>")]) |
| 391 | |
Ezio Melotti | 29877e8 | 2012-02-21 09:25:00 +0200 | [diff] [blame] | 392 | def test_slashes_in_starttag(self): |
| 393 | self._run_check('<a foo="var"/>', [('startendtag', 'a', [('foo', 'var')])]) |
| 394 | html = ('<img width=902 height=250px ' |
| 395 | 'src="/sites/default/files/images/homepage/foo.jpg" ' |
| 396 | '/*what am I doing here*/ />') |
| 397 | expected = [( |
| 398 | 'startendtag', 'img', |
| 399 | [('width', '902'), ('height', '250px'), |
| 400 | ('src', '/sites/default/files/images/homepage/foo.jpg'), |
| 401 | ('*what', None), ('am', None), ('i', None), |
| 402 | ('doing', None), ('here*', None)] |
| 403 | )] |
| 404 | self._run_check(html, expected) |
| 405 | html = ('<a / /foo/ / /=/ / /bar/ / />' |
| 406 | '<a / /foo/ / /=/ / /bar/ / >') |
| 407 | expected = [ |
| 408 | ('startendtag', 'a', [('foo', None), ('=', None), ('bar', None)]), |
| 409 | ('starttag', 'a', [('foo', None), ('=', None), ('bar', None)]) |
| 410 | ] |
| 411 | self._run_check(html, expected) |
Ezio Melotti | 0780b6b | 2012-04-18 19:18:22 -0600 | [diff] [blame] | 412 | #see issue #14538 |
| 413 | html = ('<meta><meta / ><meta // ><meta / / >' |
| 414 | '<meta/><meta /><meta //><meta//>') |
| 415 | expected = [ |
| 416 | ('starttag', 'meta', []), ('starttag', 'meta', []), |
| 417 | ('starttag', 'meta', []), ('starttag', 'meta', []), |
| 418 | ('startendtag', 'meta', []), ('startendtag', 'meta', []), |
| 419 | ('startendtag', 'meta', []), ('startendtag', 'meta', []), |
| 420 | ] |
| 421 | self._run_check(html, expected) |
Ezio Melotti | 29877e8 | 2012-02-21 09:25:00 +0200 | [diff] [blame] | 422 | |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 423 | def test_declaration_junk_chars(self): |
Ezio Melotti | f4ab491 | 2012-02-13 15:50:37 +0200 | [diff] [blame] | 424 | self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')]) |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 425 | |
| 426 | def test_illegal_declarations(self): |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 427 | self._run_check('<!spacer type="block" height="25">', |
| 428 | [('comment', 'spacer type="block" height="25"')]) |
| 429 | |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 430 | def test_with_unquoted_attributes(self): |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 431 | # see #12008 |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 432 | html = ("<html><body bgcolor=d0ca90 text='181008'>" |
| 433 | "<table cellspacing=0 cellpadding=1 width=100% ><tr>" |
| 434 | "<td align=left><font size=-1>" |
| 435 | "- <a href=/rabota/><span class=en> software-and-i</span></a>" |
| 436 | "- <a href='/1/'><span class=en> library</span></a></table>") |
| 437 | expected = [ |
| 438 | ('starttag', 'html', []), |
| 439 | ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]), |
| 440 | ('starttag', 'table', |
| 441 | [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]), |
| 442 | ('starttag', 'tr', []), |
| 443 | ('starttag', 'td', [('align', 'left')]), |
| 444 | ('starttag', 'font', [('size', '-1')]), |
| 445 | ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]), |
| 446 | ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'), |
| 447 | ('endtag', 'span'), ('endtag', 'a'), |
| 448 | ('data', '- '), ('starttag', 'a', [('href', '/1/')]), |
| 449 | ('starttag', 'span', [('class', 'en')]), ('data', ' library'), |
| 450 | ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table') |
| 451 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 452 | self._run_check(html, expected) |
Ezio Melotti | 18b0e5b | 2011-11-01 14:42:54 +0200 | [diff] [blame] | 453 | |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 454 | def test_comma_between_attributes(self): |
| 455 | self._run_check('<form action="/xxx.php?a=1&b=2&", ' |
| 456 | 'method="post">', [ |
| 457 | ('starttag', 'form', |
| 458 | [('action', '/xxx.php?a=1&b=2&'), |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 459 | (',', None), ('method', 'post')])]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 460 | |
| 461 | def test_weird_chars_in_unquoted_attribute_values(self): |
| 462 | self._run_check('<form action=bogus|&#()value>', [ |
| 463 | ('starttag', 'form', |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 464 | [('action', 'bogus|&#()value')])]) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 465 | |
Ezio Melotti | 5211ffe | 2012-02-13 11:24:50 +0200 | [diff] [blame] | 466 | def test_invalid_end_tags(self): |
| 467 | # A collection of broken end tags. <br> is used as separator. |
| 468 | # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state |
| 469 | # and #13993 |
| 470 | html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>' |
| 471 | '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>') |
| 472 | expected = [('starttag', 'br', []), |
| 473 | # < is part of the name, / is discarded, p is an attribute |
| 474 | ('endtag', 'label<'), |
| 475 | ('starttag', 'br', []), |
| 476 | # text and attributes are discarded |
| 477 | ('endtag', 'div'), |
| 478 | ('starttag', 'br', []), |
| 479 | # comment because the first char after </ is not a-zA-Z |
| 480 | ('comment', '<h4'), |
| 481 | ('starttag', 'br', []), |
| 482 | # attributes are discarded |
| 483 | ('endtag', 'li'), |
| 484 | ('starttag', 'br', []), |
| 485 | # everything till ul (included) is discarded |
| 486 | ('endtag', 'li'), |
| 487 | ('starttag', 'br', []), |
| 488 | # </> is ignored |
| 489 | ('starttag', 'br', [])] |
| 490 | self._run_check(html, expected) |
| 491 | |
| 492 | def test_broken_invalid_end_tag(self): |
| 493 | # This is technically wrong (the "> shouldn't be included in the 'data') |
| 494 | # but is probably not worth fixing it (in addition to all the cases of |
| 495 | # the previous test, it would require a full attribute parsing). |
| 496 | # see #13993 |
| 497 | html = '<b>This</b attr=">"> confuses the parser' |
| 498 | expected = [('starttag', 'b', []), |
| 499 | ('data', 'This'), |
| 500 | ('endtag', 'b'), |
| 501 | ('data', '"> confuses the parser')] |
| 502 | self._run_check(html, expected) |
| 503 | |
Ezio Melotti | b9a48f7 | 2011-11-01 15:00:59 +0200 | [diff] [blame] | 504 | def test_correct_detection_of_start_tags(self): |
| 505 | # see #13273 |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 506 | html = ('<div style="" ><b>The <a href="some_url">rain</a> ' |
| 507 | '<br /> in <span>Spain</span></b></div>') |
| 508 | expected = [ |
| 509 | ('starttag', 'div', [('style', '')]), |
| 510 | ('starttag', 'b', []), |
| 511 | ('data', 'The '), |
| 512 | ('starttag', 'a', [('href', 'some_url')]), |
| 513 | ('data', 'rain'), |
| 514 | ('endtag', 'a'), |
| 515 | ('data', ' '), |
| 516 | ('startendtag', 'br', []), |
| 517 | ('data', ' in '), |
| 518 | ('starttag', 'span', []), |
| 519 | ('data', 'Spain'), |
| 520 | ('endtag', 'span'), |
| 521 | ('endtag', 'b'), |
| 522 | ('endtag', 'div') |
| 523 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 524 | self._run_check(html, expected) |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 525 | |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 526 | html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>' |
| 527 | expected = [ |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 528 | ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]), |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 529 | ('starttag', 'b', []), |
| 530 | ('data', 'The '), |
| 531 | ('starttag', 'a', [('href', 'some_url')]), |
| 532 | ('data', 'rain'), |
| 533 | ('endtag', 'a'), |
| 534 | ] |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 535 | self._run_check(html, expected) |
Ezio Melotti | f50ffa9 | 2011-10-28 13:21:09 +0300 | [diff] [blame] | 536 | |
Senthil Kumaran | 164540f | 2010-12-28 15:55:16 +0000 | [diff] [blame] | 537 | def test_unescape_function(self): |
Ezio Melotti | 86f6712 | 2012-02-13 14:11:27 +0200 | [diff] [blame] | 538 | p = self.get_collector() |
Senthil Kumaran | 164540f | 2010-12-28 15:55:16 +0000 | [diff] [blame] | 539 | self.assertEqual(p.unescape('&#bad;'),'&#bad;') |
| 540 | self.assertEqual(p.unescape('&'),'&') |
Ezio Melotti | d9e0b06 | 2011-09-05 17:11:06 +0300 | [diff] [blame] | 541 | # see #12888 |
| 542 | self.assertEqual(p.unescape('{ ' * 1050), '{ ' * 1050) |
R. David Murray | b579dba | 2010-12-03 04:06:39 +0000 | [diff] [blame] | 543 | |
Ezio Melotti | fa3702d | 2012-02-10 10:45:44 +0200 | [diff] [blame] | 544 | def test_broken_comments(self): |
| 545 | html = ('<! not really a comment >' |
| 546 | '<! not a comment either -->' |
| 547 | '<! -- close enough -->' |
Ezio Melotti | f4ab491 | 2012-02-13 15:50:37 +0200 | [diff] [blame] | 548 | '<!><!<-- this was an empty comment>' |
Ezio Melotti | fa3702d | 2012-02-10 10:45:44 +0200 | [diff] [blame] | 549 | '<!!! another bogus comment !!!>') |
| 550 | expected = [ |
| 551 | ('comment', ' not really a comment '), |
| 552 | ('comment', ' not a comment either --'), |
| 553 | ('comment', ' -- close enough --'), |
Ezio Melotti | f4ab491 | 2012-02-13 15:50:37 +0200 | [diff] [blame] | 554 | ('comment', ''), |
| 555 | ('comment', '<-- this was an empty comment'), |
Ezio Melotti | fa3702d | 2012-02-10 10:45:44 +0200 | [diff] [blame] | 556 | ('comment', '!! another bogus comment !!!'), |
| 557 | ] |
| 558 | self._run_check(html, expected) |
| 559 | |
Ezio Melotti | 62f3d03 | 2011-12-19 07:29:03 +0200 | [diff] [blame] | 560 | def test_broken_condcoms(self): |
| 561 | # these condcoms are missing the '--' after '<!' and before the '>' |
| 562 | html = ('<![if !(IE)]>broken condcom<![endif]>' |
| 563 | '<![if ! IE]><link href="favicon.tiff"/><![endif]>' |
| 564 | '<![if !IE 6]><img src="firefox.png" /><![endif]>' |
| 565 | '<![if !ie 6]><b>foo</b><![endif]>' |
| 566 | '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>') |
| 567 | # According to the HTML5 specs sections "8.2.4.44 Bogus comment state" |
| 568 | # and "8.2.4.45 Markup declaration open state", comment tokens should |
| 569 | # be emitted instead of 'unknown decl', but calling unknown_decl |
| 570 | # provides more flexibility. |
| 571 | # See also Lib/_markupbase.py:parse_declaration |
| 572 | expected = [ |
| 573 | ('unknown decl', 'if !(IE)'), |
| 574 | ('data', 'broken condcom'), |
| 575 | ('unknown decl', 'endif'), |
| 576 | ('unknown decl', 'if ! IE'), |
| 577 | ('startendtag', 'link', [('href', 'favicon.tiff')]), |
| 578 | ('unknown decl', 'endif'), |
| 579 | ('unknown decl', 'if !IE 6'), |
| 580 | ('startendtag', 'img', [('src', 'firefox.png')]), |
| 581 | ('unknown decl', 'endif'), |
| 582 | ('unknown decl', 'if !ie 6'), |
| 583 | ('starttag', 'b', []), |
| 584 | ('data', 'foo'), |
| 585 | ('endtag', 'b'), |
| 586 | ('unknown decl', 'endif'), |
| 587 | ('unknown decl', 'if (!IE)|(lt IE 9)'), |
| 588 | ('startendtag', 'img', [('src', 'mammoth.bmp')]), |
| 589 | ('unknown decl', 'endif') |
| 590 | ] |
| 591 | self._run_check(html, expected) |
| 592 | |
Ezio Melotti | c1e73c3 | 2011-11-01 18:57:15 +0200 | [diff] [blame] | 593 | |
Ezio Melotti | b245ed1 | 2011-11-14 18:13:22 +0200 | [diff] [blame] | 594 | class AttributesStrictTestCase(TestCaseBase): |
| 595 | |
| 596 | def get_collector(self): |
| 597 | return EventCollector(strict=True) |
| 598 | |
| 599 | def test_attr_syntax(self): |
| 600 | output = [ |
| 601 | ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)]) |
| 602 | ] |
| 603 | self._run_check("""<a b='v' c="v" d=v e>""", output) |
| 604 | self._run_check("""<a b = 'v' c = "v" d = v e>""", output) |
| 605 | self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output) |
| 606 | self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output) |
| 607 | |
| 608 | def test_attr_values(self): |
| 609 | self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""", |
| 610 | [("starttag", "a", [("b", "xxx\n\txxx"), |
| 611 | ("c", "yyy\t\nyyy"), |
| 612 | ("d", "\txyz\n")])]) |
| 613 | self._run_check("""<a b='' c="">""", |
| 614 | [("starttag", "a", [("b", ""), ("c", "")])]) |
| 615 | # Regression test for SF patch #669683. |
| 616 | self._run_check("<e a=rgb(1,2,3)>", |
| 617 | [("starttag", "e", [("a", "rgb(1,2,3)")])]) |
| 618 | # Regression test for SF bug #921657. |
| 619 | self._run_check( |
| 620 | "<a href=mailto:xyz@example.com>", |
| 621 | [("starttag", "a", [("href", "mailto:xyz@example.com")])]) |
| 622 | |
| 623 | def test_attr_nonascii(self): |
| 624 | # see issue 7311 |
| 625 | self._run_check( |
| 626 | "<img src=/foo/bar.png alt=\u4e2d\u6587>", |
| 627 | [("starttag", "img", [("src", "/foo/bar.png"), |
| 628 | ("alt", "\u4e2d\u6587")])]) |
| 629 | self._run_check( |
| 630 | "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>", |
| 631 | [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), |
| 632 | ("href", "\u30c6\u30b9\u30c8.html")])]) |
| 633 | self._run_check( |
| 634 | '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">', |
| 635 | [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"), |
| 636 | ("href", "\u30c6\u30b9\u30c8.html")])]) |
| 637 | |
| 638 | def test_attr_entity_replacement(self): |
| 639 | self._run_check( |
| 640 | "<a b='&><"''>", |
| 641 | [("starttag", "a", [("b", "&><\"'")])]) |
| 642 | |
| 643 | def test_attr_funky_names(self): |
| 644 | self._run_check( |
| 645 | "<a a.b='v' c:d=v e-f=v>", |
| 646 | [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])]) |
| 647 | |
| 648 | def test_entityrefs_in_attributes(self): |
| 649 | self._run_check( |
| 650 | "<html foo='€&aa&unsupported;'>", |
| 651 | [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])]) |
| 652 | |
| 653 | |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 654 | |
| 655 | class AttributesTolerantTestCase(AttributesStrictTestCase): |
| 656 | |
| 657 | def get_collector(self): |
| 658 | return EventCollector(strict=False) |
| 659 | |
| 660 | def test_attr_funky_names2(self): |
| 661 | self._run_check( |
| 662 | "<a $><b $=%><c \=/>", |
| 663 | [("starttag", "a", [("$", None)]), |
| 664 | ("starttag", "b", [("$", "%")]), |
| 665 | ("starttag", "c", [("\\", "/")])]) |
| 666 | |
| 667 | def test_entities_in_attribute_value(self): |
| 668 | # see #1200313 |
| 669 | for entity in ['&', '&', '&', '&']: |
| 670 | self._run_check('<a href="%s">' % entity, |
| 671 | [("starttag", "a", [("href", "&")])]) |
| 672 | self._run_check("<a href='%s'>" % entity, |
| 673 | [("starttag", "a", [("href", "&")])]) |
| 674 | self._run_check("<a href=%s>" % entity, |
| 675 | [("starttag", "a", [("href", "&")])]) |
| 676 | |
| 677 | def test_malformed_attributes(self): |
| 678 | # see #13357 |
| 679 | html = ( |
| 680 | "<a href=test'style='color:red;bad1'>test - bad1</a>" |
| 681 | "<a href=test'+style='color:red;ba2'>test - bad2</a>" |
| 682 | "<a href=test' style='color:red;bad3'>test - bad3</a>" |
| 683 | "<a href = test' style='color:red;bad4' >test - bad4</a>" |
| 684 | ) |
| 685 | expected = [ |
| 686 | ('starttag', 'a', [('href', "test'style='color:red;bad1'")]), |
| 687 | ('data', 'test - bad1'), ('endtag', 'a'), |
| 688 | ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]), |
| 689 | ('data', 'test - bad2'), ('endtag', 'a'), |
| 690 | ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]), |
| 691 | ('data', 'test - bad3'), ('endtag', 'a'), |
| 692 | ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]), |
| 693 | ('data', 'test - bad4'), ('endtag', 'a') |
| 694 | ] |
| 695 | self._run_check(html, expected) |
| 696 | |
| 697 | def test_malformed_adjacent_attributes(self): |
| 698 | # see #12629 |
| 699 | self._run_check('<x><y z=""o"" /></x>', |
| 700 | [('starttag', 'x', []), |
| 701 | ('startendtag', 'y', [('z', ''), ('o""', None)]), |
| 702 | ('endtag', 'x')]) |
| 703 | self._run_check('<x><y z="""" /></x>', |
| 704 | [('starttag', 'x', []), |
| 705 | ('startendtag', 'y', [('z', ''), ('""', None)]), |
| 706 | ('endtag', 'x')]) |
| 707 | |
| 708 | # see #755670 for the following 3 tests |
| 709 | def test_adjacent_attributes(self): |
| 710 | self._run_check('<a width="100%"cellspacing=0>', |
| 711 | [("starttag", "a", |
| 712 | [("width", "100%"), ("cellspacing","0")])]) |
| 713 | |
| 714 | self._run_check('<a id="foo"class="bar">', |
| 715 | [("starttag", "a", |
| 716 | [("id", "foo"), ("class","bar")])]) |
| 717 | |
| 718 | def test_missing_attribute_value(self): |
| 719 | self._run_check('<a v=>', |
| 720 | [("starttag", "a", [("v", "")])]) |
| 721 | |
| 722 | def test_javascript_attribute_value(self): |
| 723 | self._run_check("<a href=javascript:popup('/popup/help.html')>", |
| 724 | [("starttag", "a", |
| 725 | [("href", "javascript:popup('/popup/help.html')")])]) |
| 726 | |
| 727 | def test_end_tag_in_attribute_value(self): |
| 728 | # see #1745761 |
| 729 | self._run_check("<a href='http://www.example.org/\">;'>spam</a>", |
| 730 | [("starttag", "a", |
| 731 | [("href", "http://www.example.org/\">;")]), |
| 732 | ("data", "spam"), ("endtag", "a")]) |
| 733 | |
| 734 | |
| 735 | |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 736 | def test_main(): |
Ezio Melotti | b245ed1 | 2011-11-14 18:13:22 +0200 | [diff] [blame] | 737 | support.run_unittest(HTMLParserStrictTestCase, HTMLParserTolerantTestCase, |
Ezio Melotti | c2fe577 | 2011-11-14 18:53:33 +0200 | [diff] [blame] | 738 | AttributesStrictTestCase, AttributesTolerantTestCase) |
Fred Drake | e822049 | 2001-09-24 20:19:08 +0000 | [diff] [blame] | 739 | |
| 740 | |
| 741 | if __name__ == "__main__": |
| 742 | test_main() |