annotate Lib/test/test_htmlparser.py @ 74006:88198b93ff2f

Issue #13560: Add PyUnicode_EncodeLocale() * Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not available * Document my last changes in Misc/NEWS
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 17 Dec 2011 04:13:41 +0100
parents 0a32e7e3aa1f
children 4ddbb756b602
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """Tests for HTMLParser.py."""
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2
47205
7aa9ad658014 Change test_htmlparser to reflect the HTMLParser -> html.parser
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
3 import html.parser
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
4 import pprint
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45471
diff changeset
6 from test import support
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
47205
7aa9ad658014 Change test_htmlparser to reflect the HTMLParser -> html.parser
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
9 class EventCollector(html.parser.HTMLParser):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
11 def __init__(self, *args, **kw):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 self.events = []
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 self.append = self.events.append
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
14 html.parser.HTMLParser.__init__(self, *args, **kw)
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 def get_events(self):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 # Normalize the list of events so that buffer artefacts don't
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 # separate runs of contiguous characters.
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 L = []
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 prevtype = None
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 for event in self.events:
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 type = event[0]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 if type == prevtype == "data":
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 L[-1] = ("data", L[-1][1] + event[1])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 else:
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 L.append(event)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 prevtype = type
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 self.events = L
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 return L
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 # structure markup
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 def handle_starttag(self, tag, attrs):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 self.append(("starttag", tag, attrs))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 def handle_startendtag(self, tag, attrs):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 self.append(("startendtag", tag, attrs))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 def handle_endtag(self, tag):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 self.append(("endtag", tag))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 # all other markup
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 def handle_comment(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 self.append(("comment", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 def handle_charref(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 self.append(("charref", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 def handle_data(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 self.append(("data", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 def handle_decl(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 self.append(("decl", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 def handle_entityref(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 self.append(("entityref", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 def handle_pi(self, data):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 self.append(("pi", data))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
62 def unknown_decl(self, decl):
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
63 self.append(("unknown decl", decl))
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
64
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 class EventCollectorExtra(EventCollector):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 def handle_starttag(self, tag, attrs):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 EventCollector.handle_starttag(self, tag, attrs)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 self.append(("starttag_text", self.get_starttag_text()))
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 class TestCaseBase(unittest.TestCase):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
75 def get_collector(self):
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
76 raise NotImplementedError
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
77
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
78 def _run_check(self, source, expected_events, collector=None):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
79 if collector is None:
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
80 collector = self.get_collector()
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
81 parser = collector
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 for s in source:
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 parser.feed(s)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 parser.close()
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
85 events = parser.get_events()
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
86 if events != expected_events:
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
87 self.fail("received events did not match expected events\n"
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
88 "Expected:\n" + pprint.pformat(expected_events) +
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
89 "\nReceived:\n" + pprint.pformat(events))
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 def _run_check_extra(self, source, events):
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
92 self._run_check(source, events, EventCollectorExtra())
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 def _parse_error(self, source):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 def parse(source=source):
47205
7aa9ad658014 Change test_htmlparser to reflect the HTMLParser -> html.parser
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
96 parser = html.parser.HTMLParser()
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 parser.feed(source)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 parser.close()
47205
7aa9ad658014 Change test_htmlparser to reflect the HTMLParser -> html.parser
Mark Dickinson <dickinsm@gmail.com>
parents: 47201
diff changeset
99 self.assertRaises(html.parser.HTMLParseError, parse)
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
102 class HTMLParserStrictTestCase(TestCaseBase):
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
103
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
104 def get_collector(self):
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
105 return EventCollector(strict=True)
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
107 def test_processing_instruction_only(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 self._run_check("<?processing instruction>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 ("pi", "processing instruction"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 ])
28660
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
111 self._run_check("<?processing instruction ?>", [
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
112 ("pi", "processing instruction ?"),
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
113 ])
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
115 def test_simple_html(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 self._run_check("""
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 <!DOCTYPE html PUBLIC 'foo'>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 <HTML>&entity;&#32;
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 <!--comment1a
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 -></foo><bar>&lt;<?pi?></foo<bar
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 comment1b-->
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 <Img sRc='Bar' isMAP>sample
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 text
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
124 &#x201C;
36914
f1eaae7f962d Bug #1442874: handle "<!>", the empty SGML comment
Georg Brandl <georg@python.org>
parents: 33432
diff changeset
125 <!--comment2a-- --comment2b--><!>
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 </Html>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 """, [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 ("decl", "DOCTYPE html PUBLIC 'foo'"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 ("starttag", "html", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132 ("entityref", "entity"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 ("charref", "32"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 ("comment", "comment1a\n-></foo><bar>&lt;<?pi?></foo<bar\ncomment1b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 ("starttag", "img", [("src", "Bar"), ("ismap", None)]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 ("data", "sample\ntext\n"),
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
139 ("charref", "x201C"),
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
140 ("data", "\n"),
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 ("comment", "comment2a-- --comment2b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 ("endtag", "html"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146
61889
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
147 def test_malformatted_charref(self):
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
148 self._run_check("<p>&#bad;</p>", [
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
149 ("starttag", "p", []),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
150 ("data", "&#bad;"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
151 ("endtag", "p"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
152 ])
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
153
21157
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
154 def test_unclosed_entityref(self):
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
155 self._run_check("&entityref foo", [
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
156 ("entityref", "entityref"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
157 ("data", " foo"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
158 ])
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
159
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
160 def test_doctype_decl(self):
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
161 inside = """\
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
162 DOCTYPE html [
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
163 <!ELEMENT html - O EMPTY>
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
164 <!ATTLIST html
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
165 version CDATA #IMPLIED
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
166 profile CDATA 'DublinCore'>
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
167 <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
168 <!ENTITY myEntity 'internal parsed entity'>
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
169 <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
170 <!ENTITY % paramEntity 'name|name|name'>
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
171 %paramEntity;
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
172 <!-- comment -->
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
173 ]"""
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
174 self._run_check("<!%s>" % inside, [
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
175 ("decl", inside),
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
176 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
177
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
178 def test_bad_nesting(self):
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
179 # Strangely, this *is* supposed to test that overlapping
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
180 # elements are allowed. HTMLParser is more geared toward
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
181 # lexing the input that parsing the structure.
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 self._run_check("<a><b></a></b>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 ("starttag", "a", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 ("starttag", "b", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 ("endtag", "a"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 ("endtag", "b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
189 def test_bare_ampersands(self):
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
190 self._run_check("this text & contains & ampersands &", [
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
191 ("data", "this text & contains & ampersands &"),
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
192 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
193
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
194 def test_bare_pointy_brackets(self):
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
195 self._run_check("this < text > contains < bare>pointy< brackets", [
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
196 ("data", "this < text > contains < bare>pointy< brackets"),
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
197 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
198
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
199 def test_illegal_declarations(self):
19687
3731ad1d9033 HTMLParser is allowed to be more strict than sgmllib, so let's not
Fred Drake <fdrake@acm.org>
parents: 19683
diff changeset
200 self._parse_error('<!spacer type="block" height="25">')
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
201
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
202 def test_starttag_end_boundary(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
206 def test_buffer_artefacts(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 output = [("starttag", "a", [("b", "<")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208 self._run_check(["<a b='<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209 self._run_check(["<a ", "b='<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 self._run_check(["<a b", "='<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 self._run_check(["<a b=", "'<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212 self._run_check(["<a b='<", "'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213 self._run_check(["<a b='<'", ">"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 output = [("starttag", "a", [("b", ">")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
216 self._run_check(["<a b='>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 self._run_check(["<a ", "b='>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218 self._run_check(["<a b", "='>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 self._run_check(["<a b=", "'>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 self._run_check(["<a b='>", "'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 self._run_check(["<a b='>'", ">"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222
33432
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
223 output = [("comment", "abc")]
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
224 self._run_check(["", "<!--abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
225 self._run_check(["<", "!--abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
226 self._run_check(["<!", "--abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
227 self._run_check(["<!-", "-abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
228 self._run_check(["<!--", "abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
229 self._run_check(["<!--a", "bc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
230 self._run_check(["<!--ab", "c-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
231 self._run_check(["<!--abc", "-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
232 self._run_check(["<!--abc-", "->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
233 self._run_check(["<!--abc--", ">"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
234 self._run_check(["<!--abc-->", ""], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
235
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
236 def test_starttag_junk_chars(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 self._parse_error("</>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 self._parse_error("</$>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239 self._parse_error("</")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
240 self._parse_error("</a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 self._parse_error("<a<a>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 self._parse_error("</a<a>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243 self._parse_error("<!")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
244 self._parse_error("<a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245 self._parse_error("<a foo='bar'")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
246 self._parse_error("<a foo='bar")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
247 self._parse_error("<a foo='>'")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
248 self._parse_error("<a foo='>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
249
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
250 def test_declaration_junk_chars(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
251 self._parse_error("<!DOCTYPE foo $ >")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
252
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
253 def test_startendtag(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
254 self._run_check("<p/>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
255 ("startendtag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
256 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
257 self._run_check("<p></p>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
260 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 self._run_check("<p><img src='foo' /></p>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 ("startendtag", "img", [("src", "foo")]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
267 def test_get_starttag_text(self):
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 s = """<foo:bar \n one="1"\ttwo=2 >"""
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 self._run_check_extra(s, [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270 ("starttag", "foo:bar", [("one", "1"), ("two", "2")]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 ("starttag_text", s)])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
273 def test_cdata_content(self):
73274
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
274 contents = [
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
275 '<!-- not a comment --> &not-an-entity-ref;',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
276 "<not a='start tag'>",
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
277 '<a href="" /> <p> <span></span>',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
278 'foo = "</scr" + "ipt>";',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
279 'foo = "</SCRIPT" + ">";',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
280 'foo = <\n/script> ',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
281 '<!-- document.write("</scr" + "ipt>"); -->',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
282 ('\n//<![CDATA[\n'
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
283 'document.write(\'<s\'+\'cript type="text/javascript" '
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
284 'src="http://www.example.org/r=\'+new '
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
285 'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
286 '\n<!-- //\nvar foo = 3.14;\n// -->\n',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
287 'foo = "</sty" + "le>";',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
288 '<!-- \u2603 -->',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
289 # these two should be invalid according to the HTML 5 spec,
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
290 # section 8.1.2.2
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
291 #'foo = </\nscript>',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
292 #'foo = </ script>',
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
293 ]
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
294 elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style']
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
295 for content in contents:
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
296 for element in elements:
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
297 element_lower = element.lower()
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
298 s = '<{element}>{content}</{element}>'.format(element=element,
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
299 content=content)
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
300 self._run_check(s, [("starttag", element_lower, []),
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
301 ("data", content),
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
302 ("endtag", element_lower)])
a6f2244b251f #670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73159
diff changeset
303
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
304 def test_cdata_with_closing_tags(self):
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
305 # see issue #13358
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
306 # make sure that HTMLParser calls handle_data only once for each CDATA.
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
307 # The normal event collector normalizes the events in get_events,
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
308 # so we override it to return the original list of events.
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
309 class Collector(EventCollector):
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
310 def get_events(self):
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
311 return self.events
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
313 content = """<!-- not a comment --> &not-an-entity-ref;
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
314 <a href="" /> </p><p> <span></span></style>
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
315 '</script' + '>'"""
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
316 for element in [' script', 'script ', ' script ',
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
317 '\nscript', 'script\n', '\nscript\n']:
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
318 element_lower = element.lower().strip()
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
319 s = '<script>{content}</{element}>'.format(element=element,
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
320 content=content)
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
321 self._run_check(s, [("starttag", element_lower, []),
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
322 ("data", content),
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
323 ("endtag", element_lower)],
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
324 collector=Collector())
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
326 class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
327
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
328 def get_collector(self):
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
329 return EventCollector(strict=False)
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
330
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
331 def test_tolerant_parsing(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
332 self._run_check('<html <html>te>>xt&a<<bc</a></html>\n'
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
333 '<img src="URL><//img></html</html>', [
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
334 ('starttag', 'html', [('<html', None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
335 ('data', 'te>>xt'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
336 ('entityref', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
337 ('data', '<<bc'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
338 ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
339 ('endtag', 'html'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
340 ('data', '\n<img src="URL><//img></html'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
341 ('endtag', 'html')])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
342
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
343 def test_with_unquoted_attributes(self):
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
344 # see #12008
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
345 html = ("<html><body bgcolor=d0ca90 text='181008'>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
346 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
347 "<td align=left><font size=-1>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
348 "- <a href=/rabota/><span class=en> software-and-i</span></a>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
349 "- <a href='/1/'><span class=en> library</span></a></table>")
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
350 expected = [
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
351 ('starttag', 'html', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
352 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
353 ('starttag', 'table',
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
354 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
355 ('starttag', 'tr', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
356 ('starttag', 'td', [('align', 'left')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
357 ('starttag', 'font', [('size', '-1')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
358 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
359 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
360 ('endtag', 'span'), ('endtag', 'a'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
361 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
362 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
363 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
364 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
365 self._run_check(html, expected)
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
366
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
367 def test_comma_between_attributes(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
368 self._run_check('<form action="/xxx.php?a=1&amp;b=2&amp", '
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
369 'method="post">', [
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
370 ('starttag', 'form',
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
371 [('action', '/xxx.php?a=1&b=2&amp'),
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
372 (',', None), ('method', 'post')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
373
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
374 def test_weird_chars_in_unquoted_attribute_values(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
375 self._run_check('<form action=bogus|&#()value>', [
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
376 ('starttag', 'form',
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
377 [('action', 'bogus|&#()value')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
378
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
379 def test_correct_detection_of_start_tags(self):
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
380 # see #13273
73159
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
381 html = ('<div style="" ><b>The <a href="some_url">rain</a> '
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
382 '<br /> in <span>Spain</span></b></div>')
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
383 expected = [
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
384 ('starttag', 'div', [('style', '')]),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
385 ('starttag', 'b', []),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
386 ('data', 'The '),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
387 ('starttag', 'a', [('href', 'some_url')]),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
388 ('data', 'rain'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
389 ('endtag', 'a'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
390 ('data', ' '),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
391 ('startendtag', 'br', []),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
392 ('data', ' in '),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
393 ('starttag', 'span', []),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
394 ('data', 'Spain'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
395 ('endtag', 'span'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
396 ('endtag', 'b'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
397 ('endtag', 'div')
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
398 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
399 self._run_check(html, expected)
73159
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
400
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
401 html = '<div style="", foo = "bar" ><b>The <a href="some_url">rain</a>'
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
402 expected = [
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
403 ('starttag', 'div', [('style', ''), (',', None), ('foo', 'bar')]),
73159
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
404 ('starttag', 'b', []),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
405 ('data', 'The '),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
406 ('starttag', 'a', [('href', 'some_url')]),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
407 ('data', 'rain'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
408 ('endtag', 'a'),
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
409 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
410 self._run_check(html, expected)
73159
41d41776aa6d #13273: fix a bug that prevented HTMLParser to properly detect some tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72270
diff changeset
411
67189
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
412 def test_unescape_function(self):
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
413 p = html.parser.HTMLParser()
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
414 self.assertEqual(p.unescape('&#bad;'),'&#bad;')
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
415 self.assertEqual(p.unescape('&#0038;'),'&')
72270
9896fc2a8167 #12888: Fix a bug in HTMLParser.unescape that prevented it to escape more than 128 entities. Patch by Peter Otten.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69189
diff changeset
416 # see #12888
9896fc2a8167 #12888: Fix a bug in HTMLParser.unescape that prevented it to escape more than 128 entities. Patch by Peter Otten.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69189
diff changeset
417 self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
418
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
419
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
420 class AttributesStrictTestCase(TestCaseBase):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
421
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
422 def get_collector(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
423 return EventCollector(strict=True)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
424
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
425 def test_attr_syntax(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
426 output = [
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
427 ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", None)])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
428 ]
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
429 self._run_check("""<a b='v' c="v" d=v e>""", output)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
430 self._run_check("""<a b = 'v' c = "v" d = v e>""", output)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
431 self._run_check("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
432 self._run_check("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
433
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
434 def test_attr_values(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
435 self._run_check("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
436 [("starttag", "a", [("b", "xxx\n\txxx"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
437 ("c", "yyy\t\nyyy"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
438 ("d", "\txyz\n")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
439 self._run_check("""<a b='' c="">""",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
440 [("starttag", "a", [("b", ""), ("c", "")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
441 # Regression test for SF patch #669683.
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
442 self._run_check("<e a=rgb(1,2,3)>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
443 [("starttag", "e", [("a", "rgb(1,2,3)")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
444 # Regression test for SF bug #921657.
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
445 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
446 "<a href=mailto:xyz@example.com>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
447 [("starttag", "a", [("href", "mailto:xyz@example.com")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
448
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
449 def test_attr_nonascii(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
450 # see issue 7311
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
451 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
452 "<img src=/foo/bar.png alt=\u4e2d\u6587>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
453 [("starttag", "img", [("src", "/foo/bar.png"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
454 ("alt", "\u4e2d\u6587")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
455 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
456 "<a title='\u30c6\u30b9\u30c8' href='\u30c6\u30b9\u30c8.html'>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
457 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
458 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
459 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
460 '<a title="\u30c6\u30b9\u30c8" href="\u30c6\u30b9\u30c8.html">',
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
461 [("starttag", "a", [("title", "\u30c6\u30b9\u30c8"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
462 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
463
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
464 def test_attr_entity_replacement(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
465 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
466 "<a b='&amp;&gt;&lt;&quot;&apos;'>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
467 [("starttag", "a", [("b", "&><\"'")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
468
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
469 def test_attr_funky_names(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
470 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
471 "<a a.b='v' c:d=v e-f=v>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
472 [("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
473
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
474 def test_entityrefs_in_attributes(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
475 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
476 "<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>",
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
477 [("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
478
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
479
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
480
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
481 class AttributesTolerantTestCase(AttributesStrictTestCase):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
482
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
483 def get_collector(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
484 return EventCollector(strict=False)
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
485
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
486 def test_attr_funky_names2(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
487 self._run_check(
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
488 "<a $><b $=%><c \=/>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
489 [("starttag", "a", [("$", None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
490 ("starttag", "b", [("$", "%")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
491 ("starttag", "c", [("\\", "/")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
492
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
493 def test_entities_in_attribute_value(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
494 # see #1200313
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
495 for entity in ['&', '&amp;', '&#38;', '&#x26;']:
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
496 self._run_check('<a href="%s">' % entity,
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
497 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
498 self._run_check("<a href='%s'>" % entity,
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
499 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
500 self._run_check("<a href=%s>" % entity,
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
501 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
502
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
503 def test_malformed_attributes(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
504 # see #13357
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
505 html = (
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
506 "<a href=test'style='color:red;bad1'>test - bad1</a>"
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
507 "<a href=test'+style='color:red;ba2'>test - bad2</a>"
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
508 "<a href=test'&nbsp;style='color:red;bad3'>test - bad3</a>"
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
509 "<a href = test'&nbsp;style='color:red;bad4' >test - bad4</a>"
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
510 )
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
511 expected = [
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
512 ('starttag', 'a', [('href', "test'style='color:red;bad1'")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
513 ('data', 'test - bad1'), ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
514 ('starttag', 'a', [('href', "test'+style='color:red;ba2'")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
515 ('data', 'test - bad2'), ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
516 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad3'")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
517 ('data', 'test - bad3'), ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
518 ('starttag', 'a', [('href', "test'\xa0style='color:red;bad4'")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
519 ('data', 'test - bad4'), ('endtag', 'a')
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
520 ]
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
521 self._run_check(html, expected)
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
522
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
523 def test_malformed_adjacent_attributes(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
524 # see #12629
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
525 self._run_check('<x><y z=""o"" /></x>',
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
526 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
527 ('startendtag', 'y', [('z', ''), ('o""', None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
528 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
529 self._run_check('<x><y z="""" /></x>',
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
530 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
531 ('startendtag', 'y', [('z', ''), ('""', None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
532 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
533
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
534 # see #755670 for the following 3 tests
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
535 def test_adjacent_attributes(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
536 self._run_check('<a width="100%"cellspacing=0>',
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
537 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
538 [("width", "100%"), ("cellspacing","0")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
539
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
540 self._run_check('<a id="foo"class="bar">',
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
541 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
542 [("id", "foo"), ("class","bar")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
543
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
544 def test_missing_attribute_value(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
545 self._run_check('<a v=>',
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
546 [("starttag", "a", [("v", "")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
548 def test_javascript_attribute_value(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
549 self._run_check("<a href=javascript:popup('/popup/help.html')>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
550 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
551 [("href", "javascript:popup('/popup/help.html')")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
552
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
553 def test_end_tag_in_attribute_value(self):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
554 # see #1745761
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
555 self._run_check("<a href='http://www.example.org/\">;'>spam</a>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
556 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
557 [("href", "http://www.example.org/\">;")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
558 ("data", "spam"), ("endtag", "a")])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
559
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
560
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
561
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
562 def test_main():
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
563 support.run_unittest(HTMLParserStrictTestCase, HTMLParserTolerantTestCase,
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
564 AttributesStrictTestCase, AttributesTolerantTestCase)
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
565
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
566
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
567 if __name__ == "__main__":
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
568 test_main()