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
))
58 class CDATAEventCollector(EventCollector
):
59 def start_cdata(self
, attrs
):
60 self
.append(("starttag", "cdata", attrs
))
64 class SGMLParserTestCase(unittest
.TestCase
):
66 collector
= EventCollector
68 def check_events(self
, source
, expected_events
):
69 parser
= self
.collector()
73 events
= parser
.get_events()
74 if events
!= expected_events
:
75 self
.fail("received events did not match expected events\n"
76 "Expected:\n" + pprint
.pformat(expected_events
) +
77 "\nReceived:\n" + pprint
.pformat(events
))
79 def check_parse_error(self
, source
):
80 parser
= EventCollector()
84 except sgmllib
.SGMLParseError
:
87 self
.fail("expected SGMLParseError for %r\nReceived:\n%s"
88 % (source
, pprint
.pformat(parser
.get_events())))
90 def test_underscore_in_attrname(self
):
92 """Make sure attribute names with underscores are accepted"""
93 self
.check_events("<a has_under _under>", [
94 ("starttag", "a", [("has_under", "has_under"),
95 ("_under", "_under")]),
98 def test_underscore_in_tagname(self
):
100 """Make sure tag names with underscores are accepted"""
101 self
.check_events("<has_under></has_under>", [
102 ("starttag", "has_under", []),
103 ("endtag", "has_under"),
106 def test_quotes_in_unquoted_attrs(self
):
108 """Be sure quotes in unquoted attributes are made part of the value"""
109 self
.check_events("<a href=foo'bar\"baz>", [
110 ("starttag", "a", [("href", "foo'bar\"baz")]),
113 def test_xhtml_empty_tag(self
):
114 """Handling of XHTML-style empty start tags"""
115 self
.check_events("<br />text<i></i>", [
116 ("starttag", "br", []),
118 ("starttag", "i", []),
122 def test_processing_instruction_only(self
):
123 self
.check_events("<?processing instruction>", [
124 ("pi", "processing instruction"),
127 def test_bad_nesting(self
):
128 self
.check_events("<a><b></a></b>", [
129 ("starttag", "a", []),
130 ("starttag", "b", []),
135 def test_attr_syntax(self
):
137 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", "e")])
139 self
.check_events("""<a b='v' c="v" d=v e>""", output
)
140 self
.check_events("""<a b = 'v' c = "v" d = v e>""", output
)
141 self
.check_events("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
142 self
.check_events("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
144 def test_attr_values(self
):
145 self
.check_events("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
146 [("starttag", "a", [("b", "xxx\n\txxx"),
150 self
.check_events("""<a b='' c="">""", [
151 ("starttag", "a", [("b", ""), ("c", "")]),
154 def test_attr_funky_names(self
):
155 self
.check_events("""<a a.b='v' c:d=v e-f=v>""", [
156 ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
159 def test_weird_starttags(self
):
160 self
.check_events("<a<a>", [
161 ("starttag", "a", []),
162 ("starttag", "a", []),
164 self
.check_events("</a<a>", [
166 ("starttag", "a", []),
169 def test_declaration_junk_chars(self
):
170 self
.check_parse_error("<!DOCTYPE foo $ >")
172 def test_get_starttag_text(self
):
173 s
= """<foobar \n one="1"\ttwo=2 >"""
174 self
.check_events(s
, [
175 ("starttag", "foobar", [("one", "1"), ("two", "2")]),
178 def test_cdata_content(self
):
179 s
= ("<cdata> <!-- not a comment --> ¬-an-entity-ref; </cdata>"
180 "<notcdata> <!-- comment --> </notcdata>")
181 self
.collector
= CDATAEventCollector
182 self
.check_events(s
, [
183 ("starttag", "cdata", []),
184 ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
186 ("starttag", "notcdata", []),
188 ("comment", " comment "),
190 ("endtag", "notcdata"),
192 s
= """<cdata> <not a='start tag'> </cdata>"""
193 self
.check_events(s
, [
194 ("starttag", "cdata", []),
195 ("data", " <not a='start tag'> "),
199 # XXX These tests have been disabled by prefixing their names with
200 # an underscore. The first two exercise outstanding bugs in the
201 # sgmllib module, and the third exhibits questionable behavior
202 # that needs to be carefully considered before changing it.
204 def _test_starttag_end_boundary(self
):
205 self
.check_events("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
206 self
.check_events("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
208 def _test_buffer_artefacts(self
):
209 output
= [("starttag", "a", [("b", "<")])]
210 self
.check_events(["<a b='<'>"], output
)
211 self
.check_events(["<a ", "b='<'>"], output
)
212 self
.check_events(["<a b", "='<'>"], output
)
213 self
.check_events(["<a b=", "'<'>"], output
)
214 self
.check_events(["<a b='<", "'>"], output
)
215 self
.check_events(["<a b='<'", ">"], output
)
217 output
= [("starttag", "a", [("b", ">")])]
218 self
.check_events(["<a b='>'>"], output
)
219 self
.check_events(["<a ", "b='>'>"], output
)
220 self
.check_events(["<a b", "='>'>"], output
)
221 self
.check_events(["<a b=", "'>'>"], output
)
222 self
.check_events(["<a b='>", "'>"], output
)
223 self
.check_events(["<a b='>'", ">"], output
)
225 def _test_starttag_junk_chars(self
):
226 self
.check_parse_error("<")
227 self
.check_parse_error("<>")
228 self
.check_parse_error("</$>")
229 self
.check_parse_error("</")
230 self
.check_parse_error("</a")
231 self
.check_parse_error("<$")
232 self
.check_parse_error("<$>")
233 self
.check_parse_error("<!")
234 self
.check_parse_error("<a $>")
235 self
.check_parse_error("<a")
236 self
.check_parse_error("<a foo='bar'")
237 self
.check_parse_error("<a foo='bar")
238 self
.check_parse_error("<a foo='>'")
239 self
.check_parse_error("<a foo='>")
240 self
.check_parse_error("<a foo=>")
243 test_support
.run_unittest(SGMLParserTestCase
)