blob: e982218dba11fe3e1f2ac12faf7840c4bbed2e04 [file] [log] [blame]
Fred Drakebd3090d2001-05-18 15:32:59 +00001"""Tests for HTMLParser.py."""
2
Mark Dickinsonf64dcf32008-05-21 13:51:18 +00003import html.parser
Fred Drake029acfb2001-08-20 21:24:19 +00004import pprint
Fred Drakebd3090d2001-05-18 15:32:59 +00005import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Fred Drakebd3090d2001-05-18 15:32:59 +00007
8
Mark Dickinsonf64dcf32008-05-21 13:51:18 +00009class EventCollector(html.parser.HTMLParser):
Fred Drakebd3090d2001-05-18 15:32:59 +000010
11 def __init__(self):
12 self.events = []
13 self.append = self.events.append
Mark Dickinsonf64dcf32008-05-21 13:51:18 +000014 html.parser.HTMLParser.__init__(self)
Fred Drakebd3090d2001-05-18 15:32:59 +000015
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 Drakec20a6982001-09-04 15:13:04 +000062 def unknown_decl(self, decl):
63 self.append(("unknown decl", decl))
64
Fred Drakebd3090d2001-05-18 15:32:59 +000065
66class 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
73class TestCaseBase(unittest.TestCase):
74
Fred Drakec20a6982001-09-04 15:13:04 +000075 def _run_check(self, source, expected_events, collector=EventCollector):
Fred Drakebd3090d2001-05-18 15:32:59 +000076 parser = collector()
Fred Drakebd3090d2001-05-18 15:32:59 +000077 for s in source:
78 parser.feed(s)
Fred Drakebd3090d2001-05-18 15:32:59 +000079 parser.close()
Fred Drake029acfb2001-08-20 21:24:19 +000080 events = parser.get_events()
Fred Drakec20a6982001-09-04 15:13:04 +000081 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 Drakebd3090d2001-05-18 15:32:59 +000085
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):
Mark Dickinsonf64dcf32008-05-21 13:51:18 +000091 parser = html.parser.HTMLParser()
Fred Drakebd3090d2001-05-18 15:32:59 +000092 parser.feed(source)
93 parser.close()
Mark Dickinsonf64dcf32008-05-21 13:51:18 +000094 self.assertRaises(html.parser.HTMLParseError, parse)
Fred Drakebd3090d2001-05-18 15:32:59 +000095
96
97class HTMLParserTestCase(TestCaseBase):
98
Fred Drake84bb9d82001-08-03 19:53:01 +000099 def test_processing_instruction_only(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000100 self._run_check("<?processing instruction>", [
101 ("pi", "processing instruction"),
102 ])
Fred Drakefafd56f2003-04-17 22:19:26 +0000103 self._run_check("<?processing instruction ?>", [
104 ("pi", "processing instruction ?"),
105 ])
Fred Drakebd3090d2001-05-18 15:32:59 +0000106
Fred Drake84bb9d82001-08-03 19:53:01 +0000107 def test_simple_html(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000108 self._run_check("""
109<!DOCTYPE html PUBLIC 'foo'>
110<HTML>&entity;&#32;
111<!--comment1a
112-></foo><bar>&lt;<?pi?></foo<bar
113comment1b-->
114<Img sRc='Bar' isMAP>sample
115text
Fred Drake84bb9d82001-08-03 19:53:01 +0000116&#x201C;
Georg Brandld09def32006-03-09 13:27:14 +0000117<!--comment2a-- --comment2b--><!>
Fred Drakebd3090d2001-05-18 15:32:59 +0000118</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>&lt;<?pi?></foo<bar\ncomment1b"),
128 ("data", "\n"),
129 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
130 ("data", "sample\ntext\n"),
Fred Drake84bb9d82001-08-03 19:53:01 +0000131 ("charref", "x201C"),
132 ("data", "\n"),
Fred Drakebd3090d2001-05-18 15:32:59 +0000133 ("comment", "comment2a-- --comment2b"),
134 ("data", "\n"),
135 ("endtag", "html"),
136 ("data", "\n"),
137 ])
138
Victor Stinnere021f4b2010-05-24 21:46:25 +0000139 def test_malformatted_charref(self):
140 self._run_check("<p>&#bad;</p>", [
141 ("starttag", "p", []),
142 ("data", "&#bad;"),
143 ("endtag", "p"),
144 ])
145
Fred Drake073148c2001-12-03 16:44:09 +0000146 def test_unclosed_entityref(self):
147 self._run_check("&entityref foo", [
148 ("entityref", "entityref"),
149 ("data", " foo"),
150 ])
151
Fred Drake029acfb2001-08-20 21:24:19 +0000152 def test_doctype_decl(self):
153 inside = """\
154DOCTYPE html [
155 <!ELEMENT html - O EMPTY>
156 <!ATTLIST html
Fred Drakec20a6982001-09-04 15:13:04 +0000157 version CDATA #IMPLIED
158 profile CDATA 'DublinCore'>
159 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
160 <!ENTITY myEntity 'internal parsed entity'>
161 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
162 <!ENTITY % paramEntity 'name|name|name'>
163 %paramEntity;
Fred Drake029acfb2001-08-20 21:24:19 +0000164 <!-- comment -->
165]"""
166 self._run_check("<!%s>" % inside, [
167 ("decl", inside),
168 ])
169
Fred Drake84bb9d82001-08-03 19:53:01 +0000170 def test_bad_nesting(self):
171 # Strangely, this *is* supposed to test that overlapping
172 # elements are allowed. HTMLParser is more geared toward
173 # lexing the input that parsing the structure.
Fred Drakebd3090d2001-05-18 15:32:59 +0000174 self._run_check("<a><b></a></b>", [
175 ("starttag", "a", []),
176 ("starttag", "b", []),
177 ("endtag", "a"),
178 ("endtag", "b"),
179 ])
180
Fred Drake029acfb2001-08-20 21:24:19 +0000181 def test_bare_ampersands(self):
182 self._run_check("this text & contains & ampersands &", [
183 ("data", "this text & contains & ampersands &"),
184 ])
185
186 def test_bare_pointy_brackets(self):
187 self._run_check("this < text > contains < bare>pointy< brackets", [
188 ("data", "this < text > contains < bare>pointy< brackets"),
189 ])
190
Fred Drake84bb9d82001-08-03 19:53:01 +0000191 def test_attr_syntax(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000192 output = [
193 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
194 ]
195 self._run_check("""<a b='v' c="v" d=v e>""", output)
196 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
197 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
198 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
199
Fred Drake84bb9d82001-08-03 19:53:01 +0000200 def test_attr_values(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000201 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
202 [("starttag", "a", [("b", "xxx\n\txxx"),
203 ("c", "yyy\t\nyyy"),
204 ("d", "\txyz\n")])
205 ])
206 self._run_check("""<a b='' c="">""", [
207 ("starttag", "a", [("b", ""), ("c", "")]),
208 ])
Fred Drake0834d772003-03-14 16:21:57 +0000209 # Regression test for SF patch #669683.
210 self._run_check("<e a=rgb(1,2,3)>", [
211 ("starttag", "e", [("a", "rgb(1,2,3)")]),
212 ])
Tim Peters27f88362004-07-08 04:22:35 +0000213 # Regression test for SF bug #921657.
Andrew M. Kuchlingb7d8ce02004-06-05 15:31:45 +0000214 self._run_check("<a href=mailto:xyz@example.com>", [
215 ("starttag", "a", [("href", "mailto:xyz@example.com")]),
216 ])
Fred Drakebd3090d2001-05-18 15:32:59 +0000217
Fred Drake84bb9d82001-08-03 19:53:01 +0000218 def test_attr_entity_replacement(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000219 self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [
220 ("starttag", "a", [("b", "&><\"'")]),
221 ])
222
Fred Drake84bb9d82001-08-03 19:53:01 +0000223 def test_attr_funky_names(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000224 self._run_check("""<a a.b='v' c:d=v e-f=v>""", [
225 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
226 ])
227
Fred Drakec20a6982001-09-04 15:13:04 +0000228 def test_illegal_declarations(self):
Fred Drake7cf613d2001-09-04 16:26:03 +0000229 self._parse_error('<!spacer type="block" height="25">')
Fred Drakec20a6982001-09-04 15:13:04 +0000230
Fred Drake84bb9d82001-08-03 19:53:01 +0000231 def test_starttag_end_boundary(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000232 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
233 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
234
Fred Drake84bb9d82001-08-03 19:53:01 +0000235 def test_buffer_artefacts(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000236 output = [("starttag", "a", [("b", "<")])]
237 self._run_check(["<a b='<'>"], output)
238 self._run_check(["<a ", "b='<'>"], output)
239 self._run_check(["<a b", "='<'>"], output)
240 self._run_check(["<a b=", "'<'>"], output)
241 self._run_check(["<a b='<", "'>"], output)
242 self._run_check(["<a b='<'", ">"], output)
243
244 output = [("starttag", "a", [("b", ">")])]
245 self._run_check(["<a b='>'>"], output)
246 self._run_check(["<a ", "b='>'>"], output)
247 self._run_check(["<a b", "='>'>"], output)
248 self._run_check(["<a b=", "'>'>"], output)
249 self._run_check(["<a b='>", "'>"], output)
250 self._run_check(["<a b='>'", ">"], output)
251
Fred Drake75d9a622004-09-08 22:57:01 +0000252 output = [("comment", "abc")]
253 self._run_check(["", "<!--abc-->"], output)
254 self._run_check(["<", "!--abc-->"], output)
255 self._run_check(["<!", "--abc-->"], output)
256 self._run_check(["<!-", "-abc-->"], output)
257 self._run_check(["<!--", "abc-->"], output)
258 self._run_check(["<!--a", "bc-->"], output)
259 self._run_check(["<!--ab", "c-->"], output)
260 self._run_check(["<!--abc", "-->"], output)
261 self._run_check(["<!--abc-", "->"], output)
262 self._run_check(["<!--abc--", ">"], output)
263 self._run_check(["<!--abc-->", ""], output)
264
Fred Drake84bb9d82001-08-03 19:53:01 +0000265 def test_starttag_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000266 self._parse_error("</>")
267 self._parse_error("</$>")
268 self._parse_error("</")
269 self._parse_error("</a")
Fred Drakebd3090d2001-05-18 15:32:59 +0000270 self._parse_error("<a<a>")
271 self._parse_error("</a<a>")
Fred Drakebd3090d2001-05-18 15:32:59 +0000272 self._parse_error("<!")
273 self._parse_error("<a $>")
274 self._parse_error("<a")
275 self._parse_error("<a foo='bar'")
276 self._parse_error("<a foo='bar")
277 self._parse_error("<a foo='>'")
278 self._parse_error("<a foo='>")
279 self._parse_error("<a foo=>")
280
Fred Drake84bb9d82001-08-03 19:53:01 +0000281 def test_declaration_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000282 self._parse_error("<!DOCTYPE foo $ >")
283
Fred Drake84bb9d82001-08-03 19:53:01 +0000284 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000285 self._run_check("<p/>", [
286 ("startendtag", "p", []),
287 ])
288 self._run_check("<p></p>", [
289 ("starttag", "p", []),
290 ("endtag", "p"),
291 ])
292 self._run_check("<p><img src='foo' /></p>", [
293 ("starttag", "p", []),
294 ("startendtag", "img", [("src", "foo")]),
295 ("endtag", "p"),
296 ])
297
Fred Drake84bb9d82001-08-03 19:53:01 +0000298 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000299 s = """<foo:bar \n one="1"\ttwo=2 >"""
300 self._run_check_extra(s, [
301 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
302 ("starttag_text", s)])
303
Fred Drake84bb9d82001-08-03 19:53:01 +0000304 def test_cdata_content(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000305 s = """<script> <!-- not a comment --> &not-an-entity-ref; </script>"""
306 self._run_check(s, [
307 ("starttag", "script", []),
308 ("data", " <!-- not a comment --> &not-an-entity-ref; "),
309 ("endtag", "script"),
310 ])
311 s = """<script> <not a='start tag'> </script>"""
312 self._run_check(s, [
313 ("starttag", "script", []),
314 ("data", " <not a='start tag'> "),
315 ("endtag", "script"),
316 ])
317
Guido van Rossumd8faa362007-04-27 19:54:29 +0000318 def test_entityrefs_in_attributes(self):
319 self._run_check("<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>", [
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000320 ("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000321 ])
322
Fred Drakebd3090d2001-05-18 15:32:59 +0000323
Fred Drakee8220492001-09-24 20:19:08 +0000324def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000325 support.run_unittest(HTMLParserTestCase)
Fred Drakee8220492001-09-24 20:19:08 +0000326
327
328if __name__ == "__main__":
329 test_main()