]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_htmlparser.py
1 """Tests for HTMLParser.py."""
6 from test
import test_support
9 class EventCollector(HTMLParser
.HTMLParser
):
13 self
.append
= self
.events
.append
14 HTMLParser
.HTMLParser
.__init
__(self
)
17 # Normalize the list of events so that buffer artefacts don't
18 # separate runs of contiguous characters.
21 for event
in self
.events
:
23 if type == prevtype
== "data":
24 L
[-1] = ("data", L
[-1][1] + event
[1])
33 def handle_starttag(self
, tag
, attrs
):
34 self
.append(("starttag", tag
, attrs
))
36 def handle_startendtag(self
, tag
, attrs
):
37 self
.append(("startendtag", tag
, attrs
))
39 def handle_endtag(self
, tag
):
40 self
.append(("endtag", tag
))
44 def handle_comment(self
, data
):
45 self
.append(("comment", data
))
47 def handle_charref(self
, data
):
48 self
.append(("charref", data
))
50 def handle_data(self
, data
):
51 self
.append(("data", data
))
53 def handle_decl(self
, data
):
54 self
.append(("decl", data
))
56 def handle_entityref(self
, data
):
57 self
.append(("entityref", data
))
59 def handle_pi(self
, data
):
60 self
.append(("pi", data
))
62 def unknown_decl(self
, decl
):
63 self
.append(("unknown decl", decl
))
66 class EventCollectorExtra(EventCollector
):
68 def handle_starttag(self
, tag
, attrs
):
69 EventCollector
.handle_starttag(self
, tag
, attrs
)
70 self
.append(("starttag_text", self
.get_starttag_text()))
73 class TestCaseBase(unittest
.TestCase
):
75 def _run_check(self
, source
, expected_events
, collector
=EventCollector
):
80 events
= parser
.get_events()
81 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
))
86 def _run_check_extra(self
, source
, events
):
87 self
._run
_check
(source
, events
, EventCollectorExtra
)
89 def _parse_error(self
, source
):
90 def parse(source
=source
):
91 parser
= HTMLParser
.HTMLParser()
94 self
.assertRaises(HTMLParser
.HTMLParseError
, parse
)
97 class HTMLParserTestCase(TestCaseBase
):
99 def test_processing_instruction_only(self
):
100 self
._run
_check
("<?processing instruction>", [
101 ("pi", "processing instruction"),
103 self
._run
_check
("<?processing instruction ?>", [
104 ("pi", "processing instruction ?"),
107 def test_simple_html(self
):
109 <!DOCTYPE html PUBLIC 'foo'>
112 -></foo><bar><<?pi?></foo<bar
114 <Img sRc='Bar' isMAP>sample
117 <!--comment2a-- --comment2b--><!>
121 ("decl", "DOCTYPE html PUBLIC 'foo'"),
123 ("starttag", "html", []),
124 ("entityref", "entity"),
127 ("comment", "comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b"),
129 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
130 ("data", "sample\ntext\n"),
131 ("charref", "x201C"),
133 ("comment", "comment2a-- --comment2b"),
139 def test_unclosed_entityref(self
):
140 self
._run
_check
("&entityref foo", [
141 ("entityref", "entityref"),
145 def test_doctype_decl(self
):
148 <!ELEMENT html - O EMPTY>
150 version CDATA #IMPLIED
151 profile CDATA 'DublinCore'>
152 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
153 <!ENTITY myEntity 'internal parsed entity'>
154 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
155 <!ENTITY % paramEntity 'name|name|name'>
159 self
._run
_check
("<!%s>" % inside
, [
163 def test_bad_nesting(self
):
164 # Strangely, this *is* supposed to test that overlapping
165 # elements are allowed. HTMLParser is more geared toward
166 # lexing the input that parsing the structure.
167 self
._run
_check
("<a><b></a></b>", [
168 ("starttag", "a", []),
169 ("starttag", "b", []),
174 def test_bare_ampersands(self
):
175 self
._run
_check
("this text & contains & ampersands &", [
176 ("data", "this text & contains & ampersands &"),
179 def test_bare_pointy_brackets(self
):
180 self
._run
_check
("this < text > contains < bare>pointy< brackets", [
181 ("data", "this < text > contains < bare>pointy< brackets"),
184 def test_attr_syntax(self
):
186 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
188 self
._run
_check
("""<a b='v' c="v" d=v e>""", output
)
189 self
._run
_check
("""<a b = 'v' c = "v" d = v e>""", output
)
190 self
._run
_check
("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
191 self
._run
_check
("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
193 def test_attr_values(self
):
194 self
._run
_check
("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
195 [("starttag", "a", [("b", "xxx\n\txxx"),
199 self
._run
_check
("""<a b='' c="">""", [
200 ("starttag", "a", [("b", ""), ("c", "")]),
202 # Regression test for SF patch #669683.
203 self
._run
_check
("<e a=rgb(1,2,3)>", [
204 ("starttag", "e", [("a", "rgb(1,2,3)")]),
206 # Regression test for SF bug #921657.
207 self
._run
_check
("<a href=mailto:xyz@example.com>", [
208 ("starttag", "a", [("href", "mailto:xyz@example.com")]),
211 def test_attr_nonascii(self
):
213 self
._run
_check
(u
"<img src=/foo/bar.png alt=\u4e2d\u6587>", [
214 ("starttag", "img", [("src", "/foo/bar.png"),
215 ("alt", u
"\u4e2d\u6587")]),
217 self
._run
_check
(u
"<a title='\u30c6\u30b9\u30c8' "
218 u
"href='\u30c6\u30b9\u30c8.html'>", [
219 ("starttag", "a", [("title", u
"\u30c6\u30b9\u30c8"),
220 ("href", u
"\u30c6\u30b9\u30c8.html")]),
222 self
._run
_check
(u
'<a title="\u30c6\u30b9\u30c8" '
223 u
'href="\u30c6\u30b9\u30c8.html">', [
224 ("starttag", "a", [("title", u
"\u30c6\u30b9\u30c8"),
225 ("href", u
"\u30c6\u30b9\u30c8.html")]),
228 def test_attr_entity_replacement(self
):
229 self
._run
_check
("""<a b='&><"''>""", [
230 ("starttag", "a", [("b", "&><\"'")]),
233 def test_attr_funky_names(self
):
234 self
._run
_check
("""<a a.b='v' c:d=v e-f=v>""", [
235 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
238 def test_illegal_declarations(self
):
239 self
._parse
_error
('<!spacer type="block" height="25">')
241 def test_starttag_end_boundary(self
):
242 self
._run
_check
("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
243 self
._run
_check
("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
245 def test_buffer_artefacts(self
):
246 output
= [("starttag", "a", [("b", "<")])]
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 self
._run
_check
(["<a b='<", "'>"], output
)
252 self
._run
_check
(["<a b='<'", ">"], output
)
254 output
= [("starttag", "a", [("b", ">")])]
255 self
._run
_check
(["<a b='>'>"], output
)
256 self
._run
_check
(["<a ", "b='>'>"], output
)
257 self
._run
_check
(["<a b", "='>'>"], output
)
258 self
._run
_check
(["<a b=", "'>'>"], output
)
259 self
._run
_check
(["<a b='>", "'>"], output
)
260 self
._run
_check
(["<a b='>'", ">"], output
)
262 output
= [("comment", "abc")]
263 self
._run
_check
(["", "<!--abc-->"], output
)
264 self
._run
_check
(["<", "!--abc-->"], output
)
265 self
._run
_check
(["<!", "--abc-->"], output
)
266 self
._run
_check
(["<!-", "-abc-->"], output
)
267 self
._run
_check
(["<!--", "abc-->"], output
)
268 self
._run
_check
(["<!--a", "bc-->"], output
)
269 self
._run
_check
(["<!--ab", "c-->"], output
)
270 self
._run
_check
(["<!--abc", "-->"], output
)
271 self
._run
_check
(["<!--abc-", "->"], output
)
272 self
._run
_check
(["<!--abc--", ">"], output
)
273 self
._run
_check
(["<!--abc-->", ""], output
)
275 def test_starttag_junk_chars(self
):
276 self
._parse
_error
("</>")
277 self
._parse
_error
("</$>")
278 self
._parse
_error
("</")
279 self
._parse
_error
("</a")
280 self
._parse
_error
("<a<a>")
281 self
._parse
_error
("</a<a>")
282 self
._parse
_error
("<!")
283 self
._parse
_error
("<a $>")
284 self
._parse
_error
("<a")
285 self
._parse
_error
("<a foo='bar'")
286 self
._parse
_error
("<a foo='bar")
287 self
._parse
_error
("<a foo='>'")
288 self
._parse
_error
("<a foo='>")
289 self
._parse
_error
("<a foo=>")
291 def test_declaration_junk_chars(self
):
292 self
._parse
_error
("<!DOCTYPE foo $ >")
294 def test_startendtag(self
):
295 self
._run
_check
("<p/>", [
296 ("startendtag", "p", []),
298 self
._run
_check
("<p></p>", [
299 ("starttag", "p", []),
302 self
._run
_check
("<p><img src='foo' /></p>", [
303 ("starttag", "p", []),
304 ("startendtag", "img", [("src", "foo")]),
308 def test_get_starttag_text(self
):
309 s
= """<foo:bar \n one="1"\ttwo=2 >"""
310 self
._run
_check
_extra
(s
, [
311 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
312 ("starttag_text", s
)])
314 def test_cdata_content(self
):
315 s
= """<script> <!-- not a comment --> ¬-an-entity-ref; </script>"""
317 ("starttag", "script", []),
318 ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
319 ("endtag", "script"),
321 s
= """<script> <not a='start tag'> </script>"""
323 ("starttag", "script", []),
324 ("data", " <not a='start tag'> "),
325 ("endtag", "script"),
328 def test_entityrefs_in_attributes(self
):
329 self
._run
_check
("<html foo='€&aa&unsupported;'>", [
330 ("starttag", "html", [("foo", u
"\u20AC&aa&unsupported;")])
333 def test_malformatted_charref(self
):
334 self
._run
_check
("<p>&#bad;</p>", [
335 ("starttag", "p", []),
340 def test_unescape_function(self
):
341 parser
= HTMLParser
.HTMLParser()
342 self
.assertEqual(parser
.unescape('&#bad;'),'&#bad;')
343 self
.assertEqual(parser
.unescape('&'),'&')
347 test_support
.run_unittest(HTMLParserTestCase
)
350 if __name__
== "__main__":