1 """Tests for HTMLParser.py."""
10 class EventCollector(HTMLParser
.HTMLParser
):
14 self
.append
= self
.events
.append
15 HTMLParser
.HTMLParser
.__init
__(self
)
18 # Normalize the list of events so that buffer artefacts don't
19 # separate runs of contiguous characters.
22 for event
in self
.events
:
24 if type == prevtype
== "data":
25 L
[-1] = ("data", L
[-1][1] + event
[1])
34 def handle_starttag(self
, tag
, attrs
):
35 self
.append(("starttag", tag
, attrs
))
37 def handle_startendtag(self
, tag
, attrs
):
38 self
.append(("startendtag", tag
, attrs
))
40 def handle_endtag(self
, tag
):
41 self
.append(("endtag", tag
))
45 def handle_comment(self
, data
):
46 self
.append(("comment", data
))
48 def handle_charref(self
, data
):
49 self
.append(("charref", data
))
51 def handle_data(self
, data
):
52 self
.append(("data", data
))
54 def handle_decl(self
, data
):
55 self
.append(("decl", data
))
57 def handle_entityref(self
, data
):
58 self
.append(("entityref", data
))
60 def handle_pi(self
, data
):
61 self
.append(("pi", data
))
63 def unknown_decl(self
, decl
):
64 self
.append(("unknown decl", decl
))
67 class EventCollectorExtra(EventCollector
):
69 def handle_starttag(self
, tag
, attrs
):
70 EventCollector
.handle_starttag(self
, tag
, attrs
)
71 self
.append(("starttag_text", self
.get_starttag_text()))
74 class TestCaseBase(unittest
.TestCase
):
76 def _run_check(self
, source
, expected_events
, collector
=EventCollector
):
81 events
= parser
.get_events()
82 if events
!= expected_events
:
83 self
.fail("received events did not match expected events\n"
84 "Expected:\n" + pprint
.pformat(expected_events
) +
85 "\nReceived:\n" + pprint
.pformat(events
))
87 def _run_check_extra(self
, source
, events
):
88 self
._run
_check
(source
, events
, EventCollectorExtra
)
90 def _parse_error(self
, source
):
91 def parse(source
=source
):
92 parser
= HTMLParser
.HTMLParser()
95 self
.assertRaises(HTMLParser
.HTMLParseError
, parse
)
98 class HTMLParserTestCase(TestCaseBase
):
100 def test_processing_instruction_only(self
):
101 self
._run
_check
("<?processing instruction>", [
102 ("pi", "processing instruction"),
105 def test_simple_html(self
):
107 <!DOCTYPE html PUBLIC 'foo'>
110 -></foo><bar><<?pi?></foo<bar
112 <Img sRc='Bar' isMAP>sample
115 <!--comment2a-- --comment2b-->
119 ("decl", "DOCTYPE html PUBLIC 'foo'"),
121 ("starttag", "html", []),
122 ("entityref", "entity"),
125 ("comment", "comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b"),
127 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
128 ("data", "sample\ntext\n"),
129 ("charref", "x201C"),
131 ("comment", "comment2a-- --comment2b"),
137 def test_unclosed_entityref(self
):
138 self
._run
_check
("&entityref foo", [
139 ("entityref", "entityref"),
143 def test_doctype_decl(self
):
146 <!ELEMENT html - O EMPTY>
148 version CDATA #IMPLIED
149 profile CDATA 'DublinCore'>
150 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
151 <!ENTITY myEntity 'internal parsed entity'>
152 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
153 <!ENTITY % paramEntity 'name|name|name'>
157 self
._run
_check
("<!%s>" % inside
, [
161 def test_bad_nesting(self
):
162 # Strangely, this *is* supposed to test that overlapping
163 # elements are allowed. HTMLParser is more geared toward
164 # lexing the input that parsing the structure.
165 self
._run
_check
("<a><b></a></b>", [
166 ("starttag", "a", []),
167 ("starttag", "b", []),
172 def test_bare_ampersands(self
):
173 self
._run
_check
("this text & contains & ampersands &", [
174 ("data", "this text & contains & ampersands &"),
177 def test_bare_pointy_brackets(self
):
178 self
._run
_check
("this < text > contains < bare>pointy< brackets", [
179 ("data", "this < text > contains < bare>pointy< brackets"),
182 def test_attr_syntax(self
):
184 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
186 self
._run
_check
("""<a b='v' c="v" d=v e>""", output
)
187 self
._run
_check
("""<a b = 'v' c = "v" d = v e>""", output
)
188 self
._run
_check
("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
189 self
._run
_check
("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
191 def test_attr_values(self
):
192 self
._run
_check
("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
193 [("starttag", "a", [("b", "xxx\n\txxx"),
197 self
._run
_check
("""<a b='' c="">""", [
198 ("starttag", "a", [("b", ""), ("c", "")]),
201 def test_attr_entity_replacement(self
):
202 self
._run
_check
("""<a b='&><"''>""", [
203 ("starttag", "a", [("b", "&><\"'")]),
206 def test_attr_funky_names(self
):
207 self
._run
_check
("""<a a.b='v' c:d=v e-f=v>""", [
208 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
211 def test_illegal_declarations(self
):
212 self
._parse
_error
('<!spacer type="block" height="25">')
214 def test_starttag_end_boundary(self
):
215 self
._run
_check
("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
216 self
._run
_check
("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
218 def test_buffer_artefacts(self
):
219 output
= [("starttag", "a", [("b", "<")])]
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 self
._run
_check
(["<a b='<", "'>"], output
)
225 self
._run
_check
(["<a b='<'", ">"], output
)
227 output
= [("starttag", "a", [("b", ">")])]
228 self
._run
_check
(["<a b='>'>"], output
)
229 self
._run
_check
(["<a ", "b='>'>"], output
)
230 self
._run
_check
(["<a b", "='>'>"], output
)
231 self
._run
_check
(["<a b=", "'>'>"], output
)
232 self
._run
_check
(["<a b='>", "'>"], output
)
233 self
._run
_check
(["<a b='>'", ">"], output
)
235 def test_starttag_junk_chars(self
):
236 self
._parse
_error
("</>")
237 self
._parse
_error
("</$>")
238 self
._parse
_error
("</")
239 self
._parse
_error
("</a")
240 self
._parse
_error
("<a<a>")
241 self
._parse
_error
("</a<a>")
242 self
._parse
_error
("<!")
243 self
._parse
_error
("<a $>")
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='>")
249 self
._parse
_error
("<a foo=>")
251 def test_declaration_junk_chars(self
):
252 self
._parse
_error
("<!DOCTYPE foo $ >")
254 def test_startendtag(self
):
255 self
._run
_check
("<p/>", [
256 ("startendtag", "p", []),
258 self
._run
_check
("<p></p>", [
259 ("starttag", "p", []),
262 self
._run
_check
("<p><img src='foo' /></p>", [
263 ("starttag", "p", []),
264 ("startendtag", "img", [("src", "foo")]),
268 def test_get_starttag_text(self
):
269 s
= """<foo:bar \n one="1"\ttwo=2 >"""
270 self
._run
_check
_extra
(s
, [
271 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
272 ("starttag_text", s
)])
274 def test_cdata_content(self
):
275 s
= """<script> <!-- not a comment --> ¬-an-entity-ref; </script>"""
277 ("starttag", "script", []),
278 ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
279 ("endtag", "script"),
281 s
= """<script> <not a='start tag'> </script>"""
283 ("starttag", "script", []),
284 ("data", " <not a='start tag'> "),
285 ("endtag", "script"),
290 test_support
.run_unittest(HTMLParserTestCase
)
293 if __name__
== "__main__":