123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- import pprint
- import re
- import unittest
- from test import test_support
- sgmllib = test_support.import_module('sgmllib', deprecated=True)
- class EventCollector(sgmllib.SGMLParser):
- def __init__(self):
- self.events = []
- self.append = self.events.append
- sgmllib.SGMLParser.__init__(self)
- def get_events(self):
-
-
- L = []
- prevtype = None
- for event in self.events:
- type = event[0]
- if type == prevtype == "data":
- L[-1] = ("data", L[-1][1] + event[1])
- else:
- L.append(event)
- prevtype = type
- self.events = L
- return L
-
- 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))
- self.setliteral()
- class HTMLEntityCollector(EventCollector):
- entity_or_charref = re.compile('(?:&([a-zA-Z][-.a-zA-Z0-9]*)'
- '|&#(x[0-9a-zA-Z]+|[0-9]+))(;?)')
- def convert_charref(self, name):
- self.append(("charref", "convert", name))
- if name[0] != "x":
- return EventCollector.convert_charref(self, name)
- def convert_codepoint(self, codepoint):
- self.append(("codepoint", "convert", codepoint))
- EventCollector.convert_codepoint(self, codepoint)
- def convert_entityref(self, name):
- self.append(("entityref", "convert", name))
- return EventCollector.convert_entityref(self, name)
-
-
-
- def handle_charref(self, data):
- self.append(("charref", data))
- sgmllib.SGMLParser.handle_charref(self, data)
- def handle_entityref(self, data):
- self.append(("entityref", data))
- sgmllib.SGMLParser.handle_entityref(self, data)
- class SGMLParserTestCase(unittest.TestCase):
- collector = EventCollector
- def get_events(self, source):
- parser = self.collector()
- try:
- for s in source:
- parser.feed(s)
- parser.close()
- except:
-
- raise
- return parser.get_events()
- def check_events(self, source, expected_events):
- try:
- events = self.get_events(source)
- except:
-
-
- raise
- 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()
- try:
- parser.feed(source)
- parser.close()
- except sgmllib.SGMLParseError:
- pass
- else:
- self.fail("expected SGMLParseError for %r\nReceived:\n%s"
- % (source, pprint.pformat(parser.get_events())))
- def test_doctype_decl_internal(self):
- inside = """\
- DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'
- SYSTEM 'http://www.w3.org/TR/html401/strict.dtd' [
- <!ELEMENT html - O EMPTY>
- <!ATTLIST html
- version CDATA #IMPLIED
- 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'>
- %paramEntity;
- <!-- comment -->
- ]"""
- self.check_events(["<!%s>" % inside], [
- ("decl", inside),
- ])
- def test_doctype_decl_external(self):
- inside = "DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'"
- self.check_events("<!%s>" % inside, [
- ("decl", 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"),
- ("_under", "_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", []),
- ("endtag", "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>", [
- ("starttag", "br", []),
- ("data", "text"),
- ("starttag", "i", []),
- ("endtag", "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>", [
- ("starttag", "a", []),
- ("starttag", "b", []),
- ("endtag", "a"),
- ("endtag", "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):
- output = [
- ("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"),
- ("c", "yyy\t\nyyy"),
- ("d", "\txyz\n")])
- ])
- self.check_events("""<a b='' c="">""", [
- ("starttag", "a", [("b", ""), ("c", "")]),
- ])
-
- safe = "$-_.+"
- extra = "!*'(),"
- reserved = ";/?:@&="
- url = "http://example.com:8080/path/to/file?%s%s%s" % (
- safe, extra, reserved)
- self.check_events("""<e a=%s>""" % url, [
- ("starttag", "e", [("a", url)]),
- ])
-
- self.check_events("<e a=rgb(1,2,3)>", [
- ("starttag", "e", [("a", "rgb(1,2,3)")]),
- ])
- def test_attr_values_entities(self):
- """Substitution of entities and charrefs in attribute values"""
-
- self.check_events("""<a b=< c=<> d=<-> e='< '
- f="&xxx;" g=' !' h='Ǵ'
- i='x?a=b&c=d;'
- j='&#42;' k='&#42;'>""",
- [("starttag", "a", [("b", "<"),
- ("c", "<>"),
- ("d", "<->"),
- ("e", "< "),
- ("f", "&xxx;"),
- ("g", " !"),
- ("h", "Ǵ"),
- ("i", "x?a=b&c=d;"),
- ("j", "*"),
- ("k", "*"),
- ])])
- def test_convert_overrides(self):
-
-
-
-
- self.collector = HTMLEntityCollector
- self.check_events(('<a title="“test”">foo</a>'
- '&foobar;*'), [
- ('entityref', 'convert', 'ldquo'),
- ('charref', 'convert', 'x201d'),
- ('starttag', 'a', [('title', '“test”')]),
- ('data', 'foo'),
- ('endtag', 'a'),
- ('entityref', 'foobar'),
- ('entityref', 'convert', 'foobar'),
- ('charref', '42'),
- ('charref', 'convert', '42'),
- ('codepoint', 'convert', 42),
- ])
- 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_attr_value_ip6_url(self):
-
- self.check_events(("<a href='http://[1080::8:800:200C:417A]/'>"
- "<a href=http://[1080::8:800:200C:417A]/>"), [
- ("starttag", "a", [("href", "http://[1080::8:800:200C:417A]/")]),
- ("starttag", "a", [("href", "http://[1080::8:800:200C:417A]/")]),
- ])
- def test_weird_starttags(self):
- self.check_events("<a<a>", [
- ("starttag", "a", []),
- ("starttag", "a", []),
- ])
- self.check_events("</a<a>", [
- ("endtag", "a"),
- ("starttag", "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 >"""
- self.check_events(s, [
- ("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
- self.check_events(s, [
- ("starttag", "cdata", []),
- ("data", " <!-- not a comment --> ¬-an-entity-ref; "),
- ("endtag", "cdata"),
- ("starttag", "notcdata", []),
- ("data", " "),
- ("comment", " comment "),
- ("data", " "),
- ("endtag", "notcdata"),
- ])
- s = """<cdata> <not a='start tag'> </cdata>"""
- self.check_events(s, [
- ("starttag", "cdata", []),
- ("data", " <not a='start tag'> "),
- ("endtag", "cdata"),
- ])
- def test_illegal_declarations(self):
- s = 'abc<!spacer type="block" height="25">def'
- self.check_events(s, [
- ("data", "abc"),
- ("unknown decl", 'spacer type="block" height="25"'),
- ("data", "def"),
- ])
- def test_enumerated_attr_type(self):
- s = "<!DOCTYPE doc [<!ATTLIST doc attr (a | b) >]>"
- self.check_events(s, [
- ('decl', 'DOCTYPE doc [<!ATTLIST doc attr (a | b) >]'),
- ])
- def test_read_chunks(self):
-
-
- CHUNK = 1024
- f = open(test_support.findfile('sgml_input.html'))
- fp = sgmllib.SGMLParser()
- while 1:
- data = f.read(CHUNK)
- fp.feed(data)
- if len(data) != CHUNK:
- break
- def test_only_decode_ascii(self):
-
- s = '<signs exclamation="!" copyright="©" quoteleft="‘">'
- self.check_events(s, [
- ('starttag', 'signs',
- [('exclamation', '!'), ('copyright', '©'),
- ('quoteleft', '‘')]),
- ])
-
-
-
-
- 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.check_events(["", "<!--abc-->"], output)
- self.check_events(["<", "!--abc-->"], output)
- self.check_events(["<!", "--abc-->"], output)
- self.check_events(["<!-", "-abc-->"], output)
- self.check_events(["<!--", "abc-->"], output)
- self.check_events(["<!--a", "bc-->"], output)
- self.check_events(["<!--ab", "c-->"], output)
- self.check_events(["<!--abc", "-->"], output)
- self.check_events(["<!--abc-", "->"], output)
- self.check_events(["<!--abc--", ">"], output)
- self.check_events(["<!--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=>")
- def test_main():
- test_support.run_unittest(SGMLParserTestCase)
- if __name__ == "__main__":
- test_main()
|