blob: e0e212c27a5453d9913ce10225c05227ee78ebaa [file] [log] [blame]
Fred Drakebd3090d2001-05-18 15:32:59 +00001"""Tests for HTMLParser.py."""
2
3import HTMLParser
4import sys
5import test_support
6import unittest
7
8
9class EventCollector(HTMLParser.HTMLParser):
10
11 def __init__(self):
12 self.events = []
13 self.append = self.events.append
14 HTMLParser.HTMLParser.__init__(self)
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
62
63class EventCollectorExtra(EventCollector):
64
65 def handle_starttag(self, tag, attrs):
66 EventCollector.handle_starttag(self, tag, attrs)
67 self.append(("starttag_text", self.get_starttag_text()))
68
69
70class TestCaseBase(unittest.TestCase):
71
72 # Constant pieces of source and events
73 prologue = ""
74 epilogue = ""
75 initial_events = []
76 final_events = []
77
78 def _run_check(self, source, events, collector=EventCollector):
79 parser = collector()
80 parser.feed(self.prologue)
81 for s in source:
82 parser.feed(s)
83 for c in self.epilogue:
84 parser.feed(c)
85 parser.close()
86 self.assert_(parser.get_events() ==
87 self.initial_events + events + self.final_events,
88 parser.get_events())
89
90 def _run_check_extra(self, source, events):
91 self._run_check(source, events, EventCollectorExtra)
92
93 def _parse_error(self, source):
94 def parse(source=source):
95 parser = HTMLParser.HTMLParser()
96 parser.feed(source)
97 parser.close()
98 self.assertRaises(HTMLParser.HTMLParseError, parse)
99
100
101class HTMLParserTestCase(TestCaseBase):
102
Fred Drake84bb9d82001-08-03 19:53:01 +0000103 def test_processing_instruction_only(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000104 self._run_check("<?processing instruction>", [
105 ("pi", "processing instruction"),
106 ])
107
Fred Drake84bb9d82001-08-03 19:53:01 +0000108 def test_simple_html(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000109 self._run_check("""
110<!DOCTYPE html PUBLIC 'foo'>
111<HTML>&entity;&#32;
112<!--comment1a
113-></foo><bar>&lt;<?pi?></foo<bar
114comment1b-->
115<Img sRc='Bar' isMAP>sample
116text
Fred Drake84bb9d82001-08-03 19:53:01 +0000117&#x201C;
Fred Drakebd3090d2001-05-18 15:32:59 +0000118<!--comment2a-- --comment2b-->
119</Html>
120""", [
121 ("data", "\n"),
122 ("decl", "DOCTYPE html PUBLIC 'foo'"),
123 ("data", "\n"),
124 ("starttag", "html", []),
125 ("entityref", "entity"),
126 ("charref", "32"),
127 ("data", "\n"),
128 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
129 ("data", "\n"),
130 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
131 ("data", "sample\ntext\n"),
Fred Drake84bb9d82001-08-03 19:53:01 +0000132 ("charref", "x201C"),
133 ("data", "\n"),
Fred Drakebd3090d2001-05-18 15:32:59 +0000134 ("comment", "comment2a-- --comment2b"),
135 ("data", "\n"),
136 ("endtag", "html"),
137 ("data", "\n"),
138 ])
139
Fred Drake84bb9d82001-08-03 19:53:01 +0000140 def test_bad_nesting(self):
141 # Strangely, this *is* supposed to test that overlapping
142 # elements are allowed. HTMLParser is more geared toward
143 # lexing the input that parsing the structure.
Fred Drakebd3090d2001-05-18 15:32:59 +0000144 self._run_check("<a><b></a></b>", [
145 ("starttag", "a", []),
146 ("starttag", "b", []),
147 ("endtag", "a"),
148 ("endtag", "b"),
149 ])
150
Fred Drake84bb9d82001-08-03 19:53:01 +0000151 def test_attr_syntax(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000152 output = [
153 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
154 ]
155 self._run_check("""<a b='v' c="v" d=v e>""", output)
156 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
157 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
158 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
159
Fred Drake84bb9d82001-08-03 19:53:01 +0000160 def test_attr_values(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000161 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
162 [("starttag", "a", [("b", "xxx\n\txxx"),
163 ("c", "yyy\t\nyyy"),
164 ("d", "\txyz\n")])
165 ])
166 self._run_check("""<a b='' c="">""", [
167 ("starttag", "a", [("b", ""), ("c", "")]),
168 ])
169
Fred Drake84bb9d82001-08-03 19:53:01 +0000170 def test_attr_entity_replacement(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000171 self._run_check("""<a b='&amp;&gt;&lt;&quot;&apos;'>""", [
172 ("starttag", "a", [("b", "&><\"'")]),
173 ])
174
Fred Drake84bb9d82001-08-03 19:53:01 +0000175 def test_attr_funky_names(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000176 self._run_check("""<a a.b='v' c:d=v e-f=v>""", [
177 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
178 ])
179
Fred Drake84bb9d82001-08-03 19:53:01 +0000180 def test_starttag_end_boundary(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000181 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
182 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
183
Fred Drake84bb9d82001-08-03 19:53:01 +0000184 def test_buffer_artefacts(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000185 output = [("starttag", "a", [("b", "<")])]
186 self._run_check(["<a b='<'>"], output)
187 self._run_check(["<a ", "b='<'>"], output)
188 self._run_check(["<a b", "='<'>"], output)
189 self._run_check(["<a b=", "'<'>"], output)
190 self._run_check(["<a b='<", "'>"], output)
191 self._run_check(["<a b='<'", ">"], output)
192
193 output = [("starttag", "a", [("b", ">")])]
194 self._run_check(["<a b='>'>"], output)
195 self._run_check(["<a ", "b='>'>"], output)
196 self._run_check(["<a b", "='>'>"], output)
197 self._run_check(["<a b=", "'>'>"], output)
198 self._run_check(["<a b='>", "'>"], output)
199 self._run_check(["<a b='>'", ">"], output)
200
Fred Drake84bb9d82001-08-03 19:53:01 +0000201 def test_starttag_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000202 self._parse_error("<")
203 self._parse_error("<>")
204 self._parse_error("</>")
205 self._parse_error("</$>")
206 self._parse_error("</")
207 self._parse_error("</a")
Fred Drakebd3090d2001-05-18 15:32:59 +0000208 self._parse_error("<a<a>")
209 self._parse_error("</a<a>")
210 self._parse_error("<$")
211 self._parse_error("<$>")
212 self._parse_error("<!")
213 self._parse_error("<a $>")
214 self._parse_error("<a")
215 self._parse_error("<a foo='bar'")
216 self._parse_error("<a foo='bar")
217 self._parse_error("<a foo='>'")
218 self._parse_error("<a foo='>")
219 self._parse_error("<a foo=>")
220
Fred Drake84bb9d82001-08-03 19:53:01 +0000221 def test_declaration_junk_chars(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000222 self._parse_error("<!DOCTYPE foo $ >")
223
Fred Drake84bb9d82001-08-03 19:53:01 +0000224 def test_startendtag(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000225 self._run_check("<p/>", [
226 ("startendtag", "p", []),
227 ])
228 self._run_check("<p></p>", [
229 ("starttag", "p", []),
230 ("endtag", "p"),
231 ])
232 self._run_check("<p><img src='foo' /></p>", [
233 ("starttag", "p", []),
234 ("startendtag", "img", [("src", "foo")]),
235 ("endtag", "p"),
236 ])
237
Fred Drake84bb9d82001-08-03 19:53:01 +0000238 def test_get_starttag_text(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000239 s = """<foo:bar \n one="1"\ttwo=2 >"""
240 self._run_check_extra(s, [
241 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
242 ("starttag_text", s)])
243
Fred Drake84bb9d82001-08-03 19:53:01 +0000244 def test_cdata_content(self):
Fred Drakebd3090d2001-05-18 15:32:59 +0000245 s = """<script> <!-- not a comment --> &not-an-entity-ref; </script>"""
246 self._run_check(s, [
247 ("starttag", "script", []),
248 ("data", " <!-- not a comment --> &not-an-entity-ref; "),
249 ("endtag", "script"),
250 ])
251 s = """<script> <not a='start tag'> </script>"""
252 self._run_check(s, [
253 ("starttag", "script", []),
254 ("data", " <not a='start tag'> "),
255 ("endtag", "script"),
256 ])
257
258
259test_support.run_unittest(HTMLParserTestCase)