4 from test
import test_support
7 class EventCollector(sgmllib
.SGMLParser
):
11 self
.append
= self
.events
.append
12 sgmllib
.SGMLParser
.__init
__(self
)
15 # Normalize the list of events so that buffer artefacts don't
16 # separate runs of contiguous characters.
19 for event
in self
.events
:
21 if type == prevtype
== "data":
22 L
[-1] = ("data", L
[-1][1] + event
[1])
31 def unknown_starttag(self
, tag
, attrs
):
32 self
.append(("starttag", tag
, attrs
))
34 def unknown_endtag(self
, tag
):
35 self
.append(("endtag", tag
))
39 def handle_comment(self
, data
):
40 self
.append(("comment", data
))
42 def handle_charref(self
, data
):
43 self
.append(("charref", data
))
45 def handle_data(self
, data
):
46 self
.append(("data", data
))
48 def handle_decl(self
, decl
):
49 self
.append(("decl", decl
))
51 def handle_entityref(self
, data
):
52 self
.append(("entityref", data
))
54 def handle_pi(self
, data
):
55 self
.append(("pi", data
))
57 def unknown_decl(self
, decl
):
58 self
.append(("unknown decl", decl
))
61 class CDATAEventCollector(EventCollector
):
62 def start_cdata(self
, attrs
):
63 self
.append(("starttag", "cdata", attrs
))
67 class SGMLParserTestCase(unittest
.TestCase
):
69 collector
= EventCollector
71 def get_events(self
, source
):
72 parser
= self
.collector()
78 #self.events = parser.events
80 return parser
.get_events()
82 def check_events(self
, source
, expected_events
):
84 events
= self
.get_events(source
)
87 #print >>sys.stderr, pprint.pformat(self.events)
89 if events
!= expected_events
:
90 self
.fail("received events did not match expected events\n"
91 "Expected:\n" + pprint
.pformat(expected_events
) +
92 "\nReceived:\n" + pprint
.pformat(events
))
94 def check_parse_error(self
, source
):
95 parser
= EventCollector()
99 except sgmllib
.SGMLParseError
:
102 self
.fail("expected SGMLParseError for %r\nReceived:\n%s"
103 % (source
, pprint
.pformat(parser
.get_events())))
105 def test_doctype_decl_internal(self
):
107 DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'
108 SYSTEM 'http://www.w3.org/TR/html401/strict.dtd' [
109 <!ELEMENT html - O EMPTY>
111 version CDATA #IMPLIED
112 profile CDATA 'DublinCore'>
113 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
114 <!ENTITY myEntity 'internal parsed entity'>
115 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
116 <!ENTITY % paramEntity 'name|name|name'>
120 self
.check_events(["<!%s>" % inside
], [
124 def test_doctype_decl_external(self
):
125 inside
= "DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'"
126 self
.check_events("<!%s>" % inside
, [
130 def test_underscore_in_attrname(self
):
132 """Make sure attribute names with underscores are accepted"""
133 self
.check_events("<a has_under _under>", [
134 ("starttag", "a", [("has_under", "has_under"),
135 ("_under", "_under")]),
138 def test_underscore_in_tagname(self
):
140 """Make sure tag names with underscores are accepted"""
141 self
.check_events("<has_under></has_under>", [
142 ("starttag", "has_under", []),
143 ("endtag", "has_under"),
146 def test_quotes_in_unquoted_attrs(self
):
148 """Be sure quotes in unquoted attributes are made part of the value"""
149 self
.check_events("<a href=foo'bar\"baz>", [
150 ("starttag", "a", [("href", "foo'bar\"baz")]),
153 def test_xhtml_empty_tag(self
):
154 """Handling of XHTML-style empty start tags"""
155 self
.check_events("<br />text<i></i>", [
156 ("starttag", "br", []),
158 ("starttag", "i", []),
162 def test_processing_instruction_only(self
):
163 self
.check_events("<?processing instruction>", [
164 ("pi", "processing instruction"),
167 def test_bad_nesting(self
):
168 self
.check_events("<a><b></a></b>", [
169 ("starttag", "a", []),
170 ("starttag", "b", []),
175 def test_bare_ampersands(self
):
176 self
.check_events("this text & contains & ampersands &", [
177 ("data", "this text & contains & ampersands &"),
180 def test_bare_pointy_brackets(self
):
181 self
.check_events("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", "e")])
189 self
.check_events("""<a b='v' c="v" d=v e>""", output
)
190 self
.check_events("""<a b = 'v' c = "v" d = v e>""", output
)
191 self
.check_events("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
192 self
.check_events("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
194 def test_attr_values(self
):
195 self
.check_events("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
196 [("starttag", "a", [("b", "xxx\n\txxx"),
200 self
.check_events("""<a b='' c="">""", [
201 ("starttag", "a", [("b", ""), ("c", "")]),
203 # Regression test for SF patch #669683.
204 self
.check_events("<e a=rgb(1,2,3)>", [
205 ("starttag", "e", [("a", "rgb(1,2,3)")]),
208 def test_attr_funky_names(self
):
209 self
.check_events("""<a a.b='v' c:d=v e-f=v>""", [
210 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
213 def test_illegal_declarations(self
):
214 s
= 'abc<!spacer type="block" height="25">def'
215 self
.check_events(s
, [
217 ("unknown decl", 'spacer type="block" height="25"'),
221 def test_weird_starttags(self
):
222 self
.check_events("<a<a>", [
223 ("starttag", "a", []),
224 ("starttag", "a", []),
226 self
.check_events("</a<a>", [
228 ("starttag", "a", []),
231 def test_declaration_junk_chars(self
):
232 self
.check_parse_error("<!DOCTYPE foo $ >")
234 def test_get_starttag_text(self
):
235 s
= """<foobar \n one="1"\ttwo=2 >"""
236 self
.check_events(s
, [
237 ("starttag", "foobar", [("one", "1"), ("two", "2")]),
240 def test_cdata_content(self
):
241 s
= ("<cdata> <!-- not a comment --> ¬-an-entity-ref; </cdata>"
242 "<notcdata> <!-- comment --> </notcdata>")
243 self
.collector
= CDATAEventCollector
244 self
.check_events(s
, [
245 ("starttag", "cdata", []),
246 ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
248 ("starttag", "notcdata", []),
250 ("comment", " comment "),
252 ("endtag", "notcdata"),
254 s
= """<cdata> <not a='start tag'> </cdata>"""
255 self
.check_events(s
, [
256 ("starttag", "cdata", []),
257 ("data", " <not a='start tag'> "),
261 def test_illegal_declarations(self
):
262 s
= 'abc<!spacer type="block" height="25">def'
263 self
.check_events(s
, [
265 ("unknown decl", 'spacer type="block" height="25"'),
269 def test_enumerated_attr_type(self
):
270 s
= "<!DOCTYPE doc [<!ATTLIST doc attr (a | b) >]>"
271 self
.check_events(s
, [
272 ('decl', 'DOCTYPE doc [<!ATTLIST doc attr (a | b) >]'),
275 # XXX These tests have been disabled by prefixing their names with
276 # an underscore. The first two exercise outstanding bugs in the
277 # sgmllib module, and the third exhibits questionable behavior
278 # that needs to be carefully considered before changing it.
280 def _test_starttag_end_boundary(self
):
281 self
.check_events("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
282 self
.check_events("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
284 def _test_buffer_artefacts(self
):
285 output
= [("starttag", "a", [("b", "<")])]
286 self
.check_events(["<a b='<'>"], output
)
287 self
.check_events(["<a ", "b='<'>"], output
)
288 self
.check_events(["<a b", "='<'>"], output
)
289 self
.check_events(["<a b=", "'<'>"], output
)
290 self
.check_events(["<a b='<", "'>"], output
)
291 self
.check_events(["<a b='<'", ">"], output
)
293 output
= [("starttag", "a", [("b", ">")])]
294 self
.check_events(["<a b='>'>"], output
)
295 self
.check_events(["<a ", "b='>'>"], output
)
296 self
.check_events(["<a b", "='>'>"], output
)
297 self
.check_events(["<a b=", "'>'>"], output
)
298 self
.check_events(["<a b='>", "'>"], output
)
299 self
.check_events(["<a b='>'", ">"], output
)
301 def _test_starttag_junk_chars(self
):
302 self
.check_parse_error("<")
303 self
.check_parse_error("<>")
304 self
.check_parse_error("</$>")
305 self
.check_parse_error("</")
306 self
.check_parse_error("</a")
307 self
.check_parse_error("<$")
308 self
.check_parse_error("<$>")
309 self
.check_parse_error("<!")
310 self
.check_parse_error("<a $>")
311 self
.check_parse_error("<a")
312 self
.check_parse_error("<a foo='bar'")
313 self
.check_parse_error("<a foo='bar")
314 self
.check_parse_error("<a foo='>'")
315 self
.check_parse_error("<a foo='>")
316 self
.check_parse_error("<a foo=>")
320 test_support
.run_unittest(SGMLParserTestCase
)
323 if __name__
== "__main__":