from test
import test_support
class EventCollector(sgmllib
.SGMLParser
):
self
.append
= self
.events
.append
sgmllib
.SGMLParser
.__init
__(self
)
# Normalize the list of events so that buffer artefacts don't
# separate runs of contiguous characters.
for event
in self
.events
:
if type == prevtype
== "data":
L
[-1] = ("data", L
[-1][1] + event
[1])
def unknown_starttag(self
, tag
, attrs
):
self
.append(("starttag", tag
, attrs
))
def unknown_endtag(self
, tag
):
self
.append(("endtag", tag
))
def handle_comment(self
, data
):
self
.append(("comment", data
))
def handle_charref(self
, data
):
self
.append(("charref", data
))
def handle_data(self
, data
):
self
.append(("data", data
))
def handle_decl(self
, decl
):
self
.append(("decl", decl
))
def handle_entityref(self
, data
):
self
.append(("entityref", data
))
def handle_pi(self
, data
):
self
.append(("pi", data
))
def unknown_decl(self
, decl
):
self
.append(("unknown decl", decl
))
class CDATAEventCollector(EventCollector
):
def start_cdata(self
, attrs
):
self
.append(("starttag", "cdata", attrs
))
class SGMLParserTestCase(unittest
.TestCase
):
collector
= EventCollector
def get_events(self
, source
):
parser
= self
.collector()
#self.events = parser.events
return parser
.get_events()
def check_events(self
, source
, expected_events
):
events
= self
.get_events(source
)
#print >>sys.stderr, pprint.pformat(self.events)
if events
!= expected_events
:
self
.fail("received events did not match expected events\n"
"Expected:\n" + pprint
.pformat(expected_events
) +
"\nReceived:\n" + pprint
.pformat(events
))
def check_parse_error(self
, source
):
parser
= EventCollector()
except sgmllib
.SGMLParseError
:
self
.fail("expected SGMLParseError for %r\nReceived:\n%s"
% (source
, pprint
.pformat(parser
.get_events())))
def test_doctype_decl_internal(self
):
DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'
SYSTEM 'http://www.w3.org/TR/html401/strict.dtd' [
<!ELEMENT html - O EMPTY>
profile CDATA 'DublinCore'>
<!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
<!ENTITY myEntity 'internal parsed entity'>
<!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
<!ENTITY % paramEntity 'name|name|name'>
self
.check_events(["<!%s>" % inside
], [
def test_doctype_decl_external(self
):
inside
= "DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'"
self
.check_events("<!%s>" % inside
, [
def test_underscore_in_attrname(self
):
"""Make sure attribute names with underscores are accepted"""
self
.check_events("<a has_under _under>", [
("starttag", "a", [("has_under", "has_under"),
def test_underscore_in_tagname(self
):
"""Make sure tag names with underscores are accepted"""
self
.check_events("<has_under></has_under>", [
("starttag", "has_under", []),
def test_quotes_in_unquoted_attrs(self
):
"""Be sure quotes in unquoted attributes are made part of the value"""
self
.check_events("<a href=foo'bar\"baz>", [
("starttag", "a", [("href", "foo'bar\"baz")]),
def test_xhtml_empty_tag(self
):
"""Handling of XHTML-style empty start tags"""
self
.check_events("<br />text<i></i>", [
def test_processing_instruction_only(self
):
self
.check_events("<?processing instruction>", [
("pi", "processing instruction"),
def test_bad_nesting(self
):
self
.check_events("<a><b></a></b>", [
def test_bare_ampersands(self
):
self
.check_events("this text & contains & ampersands &", [
("data", "this text & contains & ampersands &"),
def test_bare_pointy_brackets(self
):
self
.check_events("this < text > contains < bare>pointy< brackets", [
("data", "this < text > contains < bare>pointy< brackets"),
def test_attr_syntax(self
):
("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", "e")])
self
.check_events("""<a b='v' c="v" d=v e>""", output
)
self
.check_events("""<a b = 'v' c = "v" d = v e>""", output
)
self
.check_events("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output
)
self
.check_events("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output
)
def test_attr_values(self
):
self
.check_events("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
[("starttag", "a", [("b", "xxx\n\txxx"),
self
.check_events("""<a b='' c="">""", [
("starttag", "a", [("b", ""), ("c", "")]),
# URL construction stuff from RFC 1808:
url
= "http://example.com:8080/path/to/file?%s%s%s" % (
self
.check_events("""<e a=%s>""" % url
, [
("starttag", "e", [("a", url
)]),
# Regression test for SF patch #669683.
self
.check_events("<e a=rgb(1,2,3)>", [
("starttag", "e", [("a", "rgb(1,2,3)")]),
def test_attr_funky_names(self
):
self
.check_events("""<a a.b='v' c:d=v e-f=v>""", [
("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),
def test_illegal_declarations(self
):
s
= 'abc<!spacer type="block" height="25">def'
("unknown decl", 'spacer type="block" height="25"'),
def test_weird_starttags(self
):
self
.check_events("<a<a>", [
self
.check_events("</a<a>", [
def test_declaration_junk_chars(self
):
self
.check_parse_error("<!DOCTYPE foo $ >")
def test_get_starttag_text(self
):
s
= """<foobar \n one="1"\ttwo=2 >"""
("starttag", "foobar", [("one", "1"), ("two", "2")]),
def test_cdata_content(self
):
s
= ("<cdata> <!-- not a comment --> ¬-an-entity-ref; </cdata>"
"<notcdata> <!-- comment --> </notcdata>")
self
.collector
= CDATAEventCollector
("starttag", "cdata", []),
("data", " <!-- not a comment --> ¬-an-entity-ref; "),
("starttag", "notcdata", []),
("comment", " comment "),
s
= """<cdata> <not a='start tag'> </cdata>"""
("starttag", "cdata", []),
("data", " <not a='start tag'> "),
def test_illegal_declarations(self
):
s
= 'abc<!spacer type="block" height="25">def'
("unknown decl", 'spacer type="block" height="25"'),
def test_enumerated_attr_type(self
):
s
= "<!DOCTYPE doc [<!ATTLIST doc attr (a | b) >]>"
('decl', 'DOCTYPE doc [<!ATTLIST doc attr (a | b) >]'),
# XXX These tests have been disabled by prefixing their names with
# an underscore. The first two exercise outstanding bugs in the
# sgmllib module, and the third exhibits questionable behavior
# that needs to be carefully considered before changing it.
def _test_starttag_end_boundary(self
):
self
.check_events("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
self
.check_events("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
def _test_buffer_artefacts(self
):
output
= [("starttag", "a", [("b", "<")])]
self
.check_events(["<a b='<'>"], output
)
self
.check_events(["<a ", "b='<'>"], output
)
self
.check_events(["<a b", "='<'>"], output
)
self
.check_events(["<a b=", "'<'>"], output
)
self
.check_events(["<a b='<", "'>"], output
)
self
.check_events(["<a b='<'", ">"], output
)
output
= [("starttag", "a", [("b", ">")])]
self
.check_events(["<a b='>'>"], output
)
self
.check_events(["<a ", "b='>'>"], output
)
self
.check_events(["<a b", "='>'>"], output
)
self
.check_events(["<a b=", "'>'>"], output
)
self
.check_events(["<a b='>", "'>"], output
)
self
.check_events(["<a b='>'", ">"], output
)
output
= [("comment", "abc")]
self
._run
_check
(["", "<!--abc-->"], output
)
self
._run
_check
(["<", "!--abc-->"], output
)
self
._run
_check
(["<!", "--abc-->"], output
)
self
._run
_check
(["<!-", "-abc-->"], output
)
self
._run
_check
(["<!--", "abc-->"], output
)
self
._run
_check
(["<!--a", "bc-->"], output
)
self
._run
_check
(["<!--ab", "c-->"], output
)
self
._run
_check
(["<!--abc", "-->"], output
)
self
._run
_check
(["<!--abc-", "->"], output
)
self
._run
_check
(["<!--abc--", ">"], output
)
self
._run
_check
(["<!--abc-->", ""], output
)
def _test_starttag_junk_chars(self
):
self
.check_parse_error("<")
self
.check_parse_error("<>")
self
.check_parse_error("</$>")
self
.check_parse_error("</")
self
.check_parse_error("</a")
self
.check_parse_error("<$")
self
.check_parse_error("<$>")
self
.check_parse_error("<!")
self
.check_parse_error("<a $>")
self
.check_parse_error("<a")
self
.check_parse_error("<a foo='bar'")
self
.check_parse_error("<a foo='bar")
self
.check_parse_error("<a foo='>'")
self
.check_parse_error("<a foo='>")
self
.check_parse_error("<a foo=>")
test_support
.run_unittest(SGMLParserTestCase
)
if __name__
== "__main__":