1 """Tests for HTMLParser.py."""
7 from test
import test_support
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"),
104 self
._run
_check
("<?processing instruction ?>", [
105 ("pi", "processing instruction ?"),
108 def test_simple_html(self
):
110 <!DOCTYPE html PUBLIC 'foo'>
113 -></foo><bar><<?pi?></foo<bar
115 <Img sRc='Bar' isMAP>sample
118 <!--comment2a-- --comment2b-->
122 ("decl", "DOCTYPE html PUBLIC 'foo'"),
124 ("starttag", "html", []),
125 ("entityref", "entity"),
128 ("comment", "comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b"),
130 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
131 ("data", "sample\ntext\n"),
132 ("charref", "x201C"),
134 ("comment", "comment2a-- --comment2b"),
140 def test_unclosed_entityref(self
):
141 self
._run
_check
("&entityref foo", [
142 ("entityref", "entityref"),
146 def test_doctype_decl(self
):
149 <!ELEMENT html - O EMPTY>
151 version CDATA #IMPLIED
152 profile CDATA 'DublinCore'>
153 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
154 <!ENTITY myEntity 'internal parsed entity'>
155 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
156 <!ENTITY % paramEntity 'name|name|name'>
160 self
._run
_check
("<!%s>" % inside
, [
164 def test_bad_nesting(self
):
165 # Strangely, this *is* supposed to test that overlapping
166 # elements are allowed. HTMLParser is more geared toward
167 # lexing the input that parsing the structure.
168 self
._run
_check
("<a><b></a></b>", [
169 ("starttag", "a", []),
170 ("starttag", "b", []),
175 def test_bare_ampersands(self
):
176 self
._run
_check
("this text & contains & ampersands &", [
177 ("data", "this text & contains & ampersands &"),
180 def test_bare_pointy_brackets(self
):
181 self
._run
_check
("this < text > contains < bare>pointy< brackets", [
182 ("data", "this < text > contains < bare>pointy< brackets"),
185 def test_attr_syntax(self
):
187 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
189 self
._run
_check
("""<a b='v' c="v" d=v e>""", output
)
190 self
._run
_check
("""<a b = 'v' c = "v" d = v e>""", output
)
191 self
._run
_check
("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
192 self
._run
_check
("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
194 def test_attr_values(self
):
195 self
._run
_check
("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
196 [("starttag", "a", [("b", "xxx\n\txxx"),
200 self
._run
_check
("""<a b='' c="">""", [
201 ("starttag", "a", [("b", ""), ("c", "")]),
203 # Regression test for SF patch #669683.
204 self
._run
_check
("<e a=rgb(1,2,3)>", [
205 ("starttag", "e", [("a", "rgb(1,2,3)")]),
208 def test_attr_entity_replacement(self
):
209 self
._run
_check
("""<a b='&><"''>""", [
210 ("starttag", "a", [("b", "&><\"'")]),
213 def test_attr_funky_names(self
):
214 self
._run
_check
("""<a a.b='v' c:d=v e-f=v>""", [
215 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
218 def test_illegal_declarations(self
):
219 self
._parse
_error
('<!spacer type="block" height="25">')
221 def test_starttag_end_boundary(self
):
222 self
._run
_check
("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
223 self
._run
_check
("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
225 def test_buffer_artefacts(self
):
226 output
= [("starttag", "a", [("b", "<")])]
227 self
._run
_check
(["<a b='<'>"], output
)
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
)
234 output
= [("starttag", "a", [("b", ">")])]
235 self
._run
_check
(["<a b='>'>"], output
)
236 self
._run
_check
(["<a ", "b='>'>"], output
)
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
)
242 def test_starttag_junk_chars(self
):
243 self
._parse
_error
("</>")
244 self
._parse
_error
("</$>")
245 self
._parse
_error
("</")
246 self
._parse
_error
("</a")
247 self
._parse
_error
("<a<a>")
248 self
._parse
_error
("</a<a>")
249 self
._parse
_error
("<!")
250 self
._parse
_error
("<a $>")
251 self
._parse
_error
("<a")
252 self
._parse
_error
("<a foo='bar'")
253 self
._parse
_error
("<a foo='bar")
254 self
._parse
_error
("<a foo='>'")
255 self
._parse
_error
("<a foo='>")
256 self
._parse
_error
("<a foo=>")
258 def test_declaration_junk_chars(self
):
259 self
._parse
_error
("<!DOCTYPE foo $ >")
261 def test_startendtag(self
):
262 self
._run
_check
("<p/>", [
263 ("startendtag", "p", []),
265 self
._run
_check
("<p></p>", [
266 ("starttag", "p", []),
269 self
._run
_check
("<p><img src='foo' /></p>", [
270 ("starttag", "p", []),
271 ("startendtag", "img", [("src", "foo")]),
275 def test_get_starttag_text(self
):
276 s
= """<foo:bar \n one="1"\ttwo=2 >"""
277 self
._run
_check
_extra
(s
, [
278 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
279 ("starttag_text", s
)])
281 def test_cdata_content(self
):
282 s
= """<script> <!-- not a comment --> ¬-an-entity-ref; </script>"""
284 ("starttag", "script", []),
285 ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
286 ("endtag", "script"),
288 s
= """<script> <not a='start tag'> </script>"""
290 ("starttag", "script", []),
291 ("data", " <not a='start tag'> "),
292 ("endtag", "script"),
297 test_support
.run_unittest(HTMLParserTestCase
)
300 if __name__
== "__main__":