blob: bb6e0b0fe528dee83653f88b8344cb68633e4eb9 [file] [log] [blame]
Fred Drakebd3090d2001-05-18 15:32:59 +00001"""Tests for HTMLParser.py."""
2
3import HTMLParser
Fred Drake029acfb2001-08-20 21:24:19 +00004import pprint
Fred Drakebd3090d2001-05-18 15:32:59 +00005import sys
6import test_support
7import unittest
8
9
10class EventCollector(HTMLParser.HTMLParser):
11
12 def __init__(self):
13 self.events = []
14 self.append = self.events.append
15 HTMLParser.HTMLParser.__init__(self)
16
17 def get_events(self):
18 # Normalize the list of events so that buffer artefacts don't
19 # separate runs of contiguous characters.
20 L = []
21 prevtype = None
22 for event in self.events:
23 type = event[0]
24 if type == prevtype == "data":
25 L[-1] = ("data", L[-1][1] + event[1])
26 else:
27 L.append(event)
28 prevtype = type
29 self.events = L
30 return L
31
32 # structure markup
33
34 def handle_starttag(self, tag, attrs):
35 self.append(("starttag", tag, attrs))
36
37 def handle_startendtag(self, tag, attrs):
38 self.append(("startendtag", tag, attrs))
39
40 def handle_endtag(self, tag):
41 self.append(("endtag", tag))
42
43 # all other markup
44
45 def handle_comment(self, data):
46 self.append(("comment", data))
47
48 def handle_charref(self, data):
49 self.append(("charref", data))
50
51 def handle_data(self, data):
52 self.append(("data", data))
53
54 def handle_decl(self, data):
55 self.append(("decl", data))
56
57 def handle_entityref(self, data):
58 self.append(("entityref", data))
59
60 def handle_pi(self, data):
61 self.append(("pi", data))
62
63
64class EventCollectorExtra(EventCollector):
65
66 def handle_starttag(self, tag, attrs):
67 EventCollector.handle_starttag(self, tag, attrs)
68 self.append(("starttag_text", self.get_starttag_text()))
69
70
71class TestCaseBase(unittest.TestCase):
72
73 # Constant pieces of source and events
74 prologue = ""
75 epilogue = ""
76 initial_events = []
77 final_events = []
78
79 def _run_check(self, source, events, collector=EventCollector):
80 parser = collector()
81 parser.feed(self.prologue)
82 for s in source:
83 parser.feed(s)
84 for c in self.epilogue:
85 parser.feed(c)
86 parser.close()
Fred Drake029acfb2001-08-20 21:24:19 +000087 events = parser.get_events()
88 self.assertEqual(events,
89 self.initial_events + events + self.final_events,
90 "got events:\n" + pprint.pformat(events))
Fred Drakebd3090d2001-05-18 15:32:59 +000091
92 def _run_check_extra(self, source, events):
93 self._run_check(source, events, EventCollectorExtra)
94
95 def _parse_error(self, source):
96 def parse(source=source):
97 parser = HTMLParser.HTMLParser()
98 parser.feed(source)
99 parser.close()
100 self.assertRaises(HTMLParser.HTMLParseError, parse)
101
102
103class HTMLParserTestCase(TestCaseBase):
104
Fred Drake84bb9d82001-08-03 19:53:01 +0000105 def test_processing_instruction_only(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000106 self._run_check("<?processing instruction>", [
107 ("pi", "processing instruction"),
108 ])
109
Fred Drake84bb9d82001-08-03 19:53:01 +0000110 def test_simple_html(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000111 self._run_check("""
112<!DOCTYPE html PUBLIC 'foo'>
113<HTML>&entity;&#32;
114<!--comment1a
115-></foo><bar>&lt;<?pi?></foo<bar
116comment1b-->
117<Img sRc='Bar' isMAP>sample
118text
Fred Drake84bb9d82001-08-03 19:53:01 +0000119&#x201C;
Fred Drakebd3090d2001-05-18 15:32:59 +0000120<!--comment2a-- --comment2b-->
121</Html>
122""", [
123 ("data", "\n"),
124 ("decl", "DOCTYPE html PUBLIC 'foo'"),
125 ("data", "\n"),
126 ("starttag", "html", []),
127 ("entityref", "entity"),
128 ("charref", "32"),
129 ("data", "\n"),
130 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
131 ("data", "\n"),
132 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
133 ("data", "sample\ntext\n"),
Fred Drake84bb9d82001-08-03 19:53:01 +0000134 ("charref", "x201C"),
135 ("data", "\n"),
Fred Drakebd3090d2001-05-18 15:32:59 +0000136 ("comment", "comment2a-- --comment2b"),
137 ("data", "\n"),
138 ("endtag", "html"),
139 ("data", "\n"),
140 ])
141
Fred Drake029acfb2001-08-20 21:24:19 +0000142 def test_doctype_decl(self):
143 inside = """\
144DOCTYPE html [
145 <!ELEMENT html - O EMPTY>
146 <!ATTLIST html
147 version CDATA #IMPLIED '4.0'>
148 <!-- comment -->
149]"""
150 self._run_check("<!%s>" % inside, [
151 ("decl", inside),
152 ])
153
Fred Drake84bb9d82001-08-03 19:53:01 +0000154 def test_bad_nesting(self):
155 # Strangely, this *is* supposed to test that overlapping
156 # elements are allowed. HTMLParser is more geared toward
157 # lexing the input that parsing the structure.
Fred Drakebd3090d2001-05-18 15:32:59 +0000158 self._run_check("<a><b></a></b>", [
159 ("starttag", "a", []),
160 ("starttag", "b", []),
161 ("endtag", "a"),
162 ("endtag", "b"),
163 ])
164
Fred Drake029acfb2001-08-20 21:24:19 +0000165 def test_bare_ampersands(self):
166 self._run_check("this text & contains & ampersands &", [
167 ("data", "this text & contains & ampersands &"),
168 ])
169
170 def test_bare_pointy_brackets(self):
171 self._run_check("this < text > contains < bare>pointy< brackets", [
172 ("data", "this < text > contains < bare>pointy< brackets"),
173 ])
174
Fred Drake84bb9d82001-08-03 19:53:01 +0000175 def test_attr_syntax(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000176 output = [
177 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
178 ]
179 self._run_check("""<a b='v' c="v" d=v e>""", output)
180 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
181 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
182 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
183
Fred Drake84bb9d82001-08-03 19:53:01 +0000184 def test_attr_values(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000185 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
186 [("starttag", "a", [("b", "xxx\n\txxx"),
187 ("c", "yyy\t\nyyy"),
188 ("d", "\txyz\n")])
189 ])
190 self._run_check("""<a b='' c="">""", [
191 ("starttag", "a", [("b", ""), ("c", "")]),
192 ])
193
Fred Drake84bb9d82001-08-03 19:53:01 +0000194 def test_attr_entity_replacement(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000195 self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [
196 ("starttag", "a", [("b", "&><\"'")]),
197 ])
198
Fred Drake84bb9d82001-08-03 19:53:01 +0000199 def test_attr_funky_names(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000200 self._run_check("""<a a.b='v' c:d=v e-f=v>""", [
201 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
202 ])
203
Fred Drake84bb9d82001-08-03 19:53:01 +0000204 def test_starttag_end_boundary(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000205 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
206 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
207
Fred Drake84bb9d82001-08-03 19:53:01 +0000208 def test_buffer_artefacts(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000209 output = [("starttag", "a", [("b", "<")])]
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 self._run_check(["<a b='<", "'>"], output)
215 self._run_check(["<a b='<'", ">"], output)
216
217 output = [("starttag", "a", [("b", ">")])]
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 self._run_check(["<a b='>", "'>"], output)
223 self._run_check(["<a b='>'", ">"], output)
224
Fred Drake84bb9d82001-08-03 19:53:01 +0000225 def test_starttag_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000226 self._parse_error("</>")
227 self._parse_error("</$>")
228 self._parse_error("</")
229 self._parse_error("</a")
Fred Drakebd3090d2001-05-18 15:32:59 +0000230 self._parse_error("<a<a>")
231 self._parse_error("</a<a>")
Fred Drakebd3090d2001-05-18 15:32:59 +0000232 self._parse_error("<!")
233 self._parse_error("<a $>")
234 self._parse_error("<a")
235 self._parse_error("<a foo='bar'")
236 self._parse_error("<a foo='bar")
237 self._parse_error("<a foo='>'")
238 self._parse_error("<a foo='>")
239 self._parse_error("<a foo=>")
240
Fred Drake84bb9d82001-08-03 19:53:01 +0000241 def test_declaration_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000242 self._parse_error("<!DOCTYPE foo $ >")
243
Fred Drake84bb9d82001-08-03 19:53:01 +0000244 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000245 self._run_check("<p/>", [
246 ("startendtag", "p", []),
247 ])
248 self._run_check("<p></p>", [
249 ("starttag", "p", []),
250 ("endtag", "p"),
251 ])
252 self._run_check("<p><img src='foo' /></p>", [
253 ("starttag", "p", []),
254 ("startendtag", "img", [("src", "foo")]),
255 ("endtag", "p"),
256 ])
257
Fred Drake84bb9d82001-08-03 19:53:01 +0000258 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000259 s = """<foo:bar \n one="1"\ttwo=2 >"""
260 self._run_check_extra(s, [
261 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
262 ("starttag_text", s)])
263
Fred Drake84bb9d82001-08-03 19:53:01 +0000264 def test_cdata_content(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000265 s = """<script> <!-- not a comment --> &not-an-entity-ref; </script>"""
266 self._run_check(s, [
267 ("starttag", "script", []),
268 ("data", " <!-- not a comment --> &not-an-entity-ref; "),
269 ("endtag", "script"),
270 ])
271 s = """<script> <not a='start tag'> </script>"""
272 self._run_check(s, [
273 ("starttag", "script", []),
274 ("data", " <not a='start tag'> "),
275 ("endtag", "script"),
276 ])
277
278
279test_support.run_unittest(HTMLParserTestCase)