annotate Lib/test/test_htmlparser.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents ba4baaddac8d
children 8dd2f5754b2f
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):
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
96 parser = self.get_collector()
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;
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
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
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
160 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
161 # 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
162 # 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
163 # 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
164 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
165 ("starttag", "a", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 ("starttag", "b", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 ("endtag", "a"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 ("endtag", "b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
171 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
172 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
173 ("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
174 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
175
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
176 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
177 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
178 ("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
179 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
180
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
181 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
182 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
183
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
184 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
185 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
186 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
187
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
188 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
189 output = [("starttag", "a", [("b", "<")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 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
191 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
192 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
193 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
194 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
195 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
196
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 output = [("starttag", "a", [("b", ">")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 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
199 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
200 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
201 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
202 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
203 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
204
33432
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
218 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
219 self._parse_error("</>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 self._parse_error("</$>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 self._parse_error("</")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222 self._parse_error("</a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 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
224 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
225 self._parse_error("<!")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226 self._parse_error("<a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
227 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
228 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
229 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
230 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
231
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
232 def test_valid_doctypes(self):
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
233 # from http://www.w3.org/QA/2002/04/valid-dtd-list.html
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
234 dtds = ['HTML', # HTML5 doctype
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
235 ('HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
236 '"http://www.w3.org/TR/html4/strict.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
237 ('HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
238 '"http://www.w3.org/TR/html4/loose.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
239 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
240 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
241 ('html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
242 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
243 ('math PUBLIC "-//W3C//DTD MathML 2.0//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
244 '"http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
245 ('html PUBLIC "-//W3C//DTD '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
246 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
247 '"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
248 ('svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
249 '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"'),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
250 'html PUBLIC "-//IETF//DTD HTML 2.0//EN"',
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
251 'html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"']
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
252 for dtd in dtds:
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
253 self._run_check("<!DOCTYPE %s>" % dtd,
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
254 [('decl', 'DOCTYPE ' + dtd)])
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
255
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
256 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
257 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
258
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
259 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
260 self._run_check("<p/>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 ("startendtag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 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
264 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 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
268 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 ("startendtag", "img", [("src", "foo")]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 ])
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_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
274 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
275 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
276 ("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
277 ("starttag_text", s)])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
278
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
279 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
280 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
281 '<!-- 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
282 "<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
283 '<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
284 '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
285 '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
286 '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
287 '<!-- 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
288 ('\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
289 '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
290 '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
291 '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
292 '\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
293 '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
294 '<!-- \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
295 # 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
296 # 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
297 #'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
298 #'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
299 ]
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 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
301 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
302 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
303 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
304 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
305 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
306 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
307 ("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
308 ("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
309
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
310 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
311 # 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
312 # 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
313 # 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
314 # 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
315 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
316 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
317 return self.events
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
319 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
320 <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
321 '</script' + '>'"""
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
322 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
323 '\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
324 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
325 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
326 content=content)
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
327 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
328 ("data", content),
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
329 ("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
330 collector=Collector())
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
332 def test_comments(self):
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
333 html = ("<!-- I'm a valid comment -->"
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
334 '<!--me too!-->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
335 '<!------>'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
336 '<!---->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
337 '<!----I have many hyphens---->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
338 '<!-- I have a > in the middle -->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
339 '<!-- and I have -- in the middle! -->')
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
340 expected = [('comment', " I'm a valid comment "),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
341 ('comment', 'me too!'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
342 ('comment', '--'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
343 ('comment', ''),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
344 ('comment', '--I have many hyphens--'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
345 ('comment', ' I have a > in the middle '),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
346 ('comment', ' and I have -- in the middle! ')]
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
347 self._run_check(html, expected)
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
348
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
349 def test_condcoms(self):
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
350 html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
351 '<!--[if IE 8]>condcoms<![endif]-->'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
352 '<!--[if lte IE 7]>pretty?<![endif]-->')
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
353 expected = [('comment', "[if IE & !(lte IE 8)]>aren't<![endif]"),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
354 ('comment', '[if IE 8]>condcoms<![endif]'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
355 ('comment', '[if lte IE 7]>pretty?<![endif]')]
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
356 self._run_check(html, expected)
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
357
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
358
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
359 class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
360
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
361 def get_collector(self):
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
362 return EventCollector(strict=False)
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
363
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
364 def test_tolerant_parsing(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
365 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
366 '<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
367 ('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
368 ('data', 'te>>xt'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
369 ('entityref', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
370 ('data', '<<bc'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
371 ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
372 ('endtag', 'html'),
74901
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
373 ('data', '\n<img src="URL>'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
374 ('comment', '/img'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
375 ('endtag', 'html<')])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
376
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
377 def test_starttag_junk_chars(self):
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
378 self._run_check("</>", [])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
379 self._run_check("</$>", [('comment', '$')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
380 self._run_check("</", [('data', '</')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
381 self._run_check("</a", [('data', '</a')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
382 # XXX this might be wrong
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
383 self._run_check("<a<a>", [('data', '<a'), ('starttag', 'a', [])])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
384 self._run_check("</a<a>", [('endtag', 'a<a')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
385 self._run_check("<!", [('data', '<!')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
386 self._run_check("<a", [('data', '<a')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
387 self._run_check("<a foo='bar'", [('data', "<a foo='bar'")])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
388 self._run_check("<a foo='bar", [('data', "<a foo='bar")])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
389 self._run_check("<a foo='>'", [('data', "<a foo='>'")])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
390 self._run_check("<a foo='>", [('data', "<a foo='>")])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
391
75117
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
392 def test_slashes_in_starttag(self):
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
393 self._run_check('<a foo="var"/>', [('startendtag', 'a', [('foo', 'var')])])
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
394 html = ('<img width=902 height=250px '
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
395 'src="/sites/default/files/images/homepage/foo.jpg" '
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
396 '/*what am I doing here*/ />')
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
397 expected = [(
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
398 'startendtag', 'img',
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
399 [('width', '902'), ('height', '250px'),
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
400 ('src', '/sites/default/files/images/homepage/foo.jpg'),
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
401 ('*what', None), ('am', None), ('i', None),
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
402 ('doing', None), ('here*', None)]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
403 )]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
404 self._run_check(html, expected)
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
405 html = ('<a / /foo/ / /=/ / /bar/ / />'
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
406 '<a / /foo/ / /=/ / /bar/ / >')
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
407 expected = [
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
408 ('startendtag', 'a', [('foo', None), ('=', None), ('bar', None)]),
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
409 ('starttag', 'a', [('foo', None), ('=', None), ('bar', None)])
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
410 ]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
411 self._run_check(html, expected)
76414
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
412 #see issue #14538
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
413 html = ('<meta><meta / ><meta // ><meta / / >'
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
414 '<meta/><meta /><meta //><meta//>')
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
415 expected = [
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
416 ('starttag', 'meta', []), ('starttag', 'meta', []),
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
417 ('starttag', 'meta', []), ('starttag', 'meta', []),
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
418 ('startendtag', 'meta', []), ('startendtag', 'meta', []),
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
419 ('startendtag', 'meta', []), ('startendtag', 'meta', []),
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
420 ]
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
421 self._run_check(html, expected)
75117
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
422
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
423 def test_declaration_junk_chars(self):
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
424 self._run_check("<!DOCTYPE foo $ >", [('decl', 'DOCTYPE foo $ ')])
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
425
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
426 def test_illegal_declarations(self):
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
427 self._run_check('<!spacer type="block" height="25">',
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
428 [('comment', 'spacer type="block" height="25"')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
429
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
430 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
431 # see #12008
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
432 html = ("<html><body bgcolor=d0ca90 text='181008'>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
433 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
434 "<td align=left><font size=-1>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
435 "- <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
436 "- <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
437 expected = [
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
438 ('starttag', 'html', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
439 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
440 ('starttag', 'table',
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
441 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
442 ('starttag', 'tr', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
443 ('starttag', 'td', [('align', 'left')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
444 ('starttag', 'font', [('size', '-1')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
445 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
446 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
447 ('endtag', 'span'), ('endtag', 'a'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
448 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
449 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
450 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
451 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
452 self._run_check(html, expected)
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
453
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
454 def test_comma_between_attributes(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
455 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
456 'method="post">', [
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
457 ('starttag', 'form',
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
458 [('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
459 (',', None), ('method', 'post')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
460
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
461 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
462 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
463 ('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
464 [('action', 'bogus|&#()value')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
465
74901
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
466 def test_invalid_end_tags(self):
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
467 # A collection of broken end tags. <br> is used as separator.
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
468 # see http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
469 # and #13993
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
470 html = ('<br></label</p><br></div end tmAd-leaderBoard><br></<h4><br>'
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
471 '</li class="unit"><br></li\r\n\t\t\t\t\t\t</ul><br></><br>')
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
472 expected = [('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
473 # < is part of the name, / is discarded, p is an attribute
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
474 ('endtag', 'label<'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
475 ('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
476 # text and attributes are discarded
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
477 ('endtag', 'div'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
478 ('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
479 # comment because the first char after </ is not a-zA-Z
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
480 ('comment', '<h4'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
481 ('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
482 # attributes are discarded
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
483 ('endtag', 'li'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
484 ('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
485 # everything till ul (included) is discarded
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
486 ('endtag', 'li'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
487 ('starttag', 'br', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
488 # </> is ignored
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
489 ('starttag', 'br', [])]
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
490 self._run_check(html, expected)
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
491
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
492 def test_broken_invalid_end_tag(self):
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
493 # This is technically wrong (the "> shouldn't be included in the 'data')
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
494 # but is probably not worth fixing it (in addition to all the cases of
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
495 # the previous test, it would require a full attribute parsing).
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
496 # see #13993
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
497 html = '<b>This</b attr=">"> confuses the parser'
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
498 expected = [('starttag', 'b', []),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
499 ('data', 'This'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
500 ('endtag', 'b'),
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
501 ('data', '"> confuses the parser')]
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
502 self._run_check(html, expected)
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
503
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
504 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
505 # 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
506 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
507 '<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
508 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
509 ('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
510 ('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
511 ('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
512 ('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
513 ('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
514 ('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
515 ('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
516 ('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
517 ('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
518 ('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
519 ('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
520 ('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
521 ('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
522 ('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
523 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
524 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
525
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
526 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
527 expected = [
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
528 ('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
529 ('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
530 ('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
531 ('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
532 ('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
533 ('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
534 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
535 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
536
67189
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
537 def test_unescape_function(self):
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
538 p = self.get_collector()
67189
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
539 self.assertEqual(p.unescape('&#bad;'),'&#bad;')
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
540 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
541 # 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
542 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
543
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
544 def test_broken_comments(self):
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
545 html = ('<! not really a comment >'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
546 '<! not a comment either -->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
547 '<! -- close enough -->'
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
548 '<!><!<-- this was an empty comment>'
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
549 '<!!! another bogus comment !!!>')
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
550 expected = [
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
551 ('comment', ' not really a comment '),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
552 ('comment', ' not a comment either --'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
553 ('comment', ' -- close enough --'),
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
554 ('comment', ''),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
555 ('comment', '<-- this was an empty comment'),
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
556 ('comment', '!! another bogus comment !!!'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
557 ]
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
558 self._run_check(html, expected)
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
559
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
560 def test_broken_condcoms(self):
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
561 # these condcoms are missing the '--' after '<!' and before the '>'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
562 html = ('<![if !(IE)]>broken condcom<![endif]>'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
563 '<![if ! IE]><link href="favicon.tiff"/><![endif]>'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
564 '<![if !IE 6]><img src="firefox.png" /><![endif]>'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
565 '<![if !ie 6]><b>foo</b><![endif]>'
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
566 '<![if (!IE)|(lt IE 9)]><img src="mammoth.bmp" /><![endif]>')
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
567 # According to the HTML5 specs sections "8.2.4.44 Bogus comment state"
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
568 # and "8.2.4.45 Markup declaration open state", comment tokens should
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
569 # be emitted instead of 'unknown decl', but calling unknown_decl
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
570 # provides more flexibility.
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
571 # See also Lib/_markupbase.py:parse_declaration
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
572 expected = [
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
573 ('unknown decl', 'if !(IE)'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
574 ('data', 'broken condcom'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
575 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
576 ('unknown decl', 'if ! IE'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
577 ('startendtag', 'link', [('href', 'favicon.tiff')]),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
578 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
579 ('unknown decl', 'if !IE 6'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
580 ('startendtag', 'img', [('src', 'firefox.png')]),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
581 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
582 ('unknown decl', 'if !ie 6'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
583 ('starttag', 'b', []),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
584 ('data', 'foo'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
585 ('endtag', 'b'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
586 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
587 ('unknown decl', 'if (!IE)|(lt IE 9)'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
588 ('startendtag', 'img', [('src', 'mammoth.bmp')]),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
589 ('unknown decl', 'endif')
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
590 ]
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
591 self._run_check(html, expected)
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
592
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
593
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
594 class AttributesStrictTestCase(TestCaseBase):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
595
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
596 def get_collector(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
597 return EventCollector(strict=True)
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
598
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
599 def test_attr_syntax(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
600 output = [
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
601 ("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
602 ]
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
603 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
604 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
605 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
606 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
607
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
608 def test_attr_values(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
609 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
610 [("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
611 ("c", "yyy\t\nyyy"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
612 ("d", "\txyz\n")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
613 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
614 [("starttag", "a", [("b", ""), ("c", "")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
615 # 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
616 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
617 [("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
618 # 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
619 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
620 "<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
621 [("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
622
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
623 def test_attr_nonascii(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
624 # see issue 7311
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
625 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
626 "<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
627 [("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
628 ("alt", "\u4e2d\u6587")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
629 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
630 "<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
631 [("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
632 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
633 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
634 '<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
635 [("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
636 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
637
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
638 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
639 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
640 "<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
641 [("starttag", "a", [("b", "&><\"'")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
642
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
643 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
644 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
645 "<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
646 [("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
647
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
648 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
649 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
650 "<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
651 [("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
652
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
653
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
654
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
655 class AttributesTolerantTestCase(AttributesStrictTestCase):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
656
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
657 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
658 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
659
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
660 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
661 self._run_check(
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
662 "<a $><b $=%><c \=/>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
663 [("starttag", "a", [("$", None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
664 ("starttag", "b", [("$", "%")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
665 ("starttag", "c", [("\\", "/")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
666
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
667 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
668 # see #1200313
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
669 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
670 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
671 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
672 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
673 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
674 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
675 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
676
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
677 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
678 # see #13357
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
679 html = (
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
680 "<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
681 "<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
682 "<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
683 "<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
684 )
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
685 expected = [
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
686 ('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
687 ('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
688 ('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
689 ('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
690 ('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
691 ('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
692 ('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
693 ('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
694 ]
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
695 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
696
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
697 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
698 # see #12629
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
699 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
700 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
701 ('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
702 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
703 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
704 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
705 ('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
706 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
707
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
708 # 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
709 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
710 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
711 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
712 [("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
713
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
714 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
715 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
716 [("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
717
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
718 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
719 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
720 [("starttag", "a", [("v", "")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
721
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
722 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
723 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
724 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
725 [("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
726
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
727 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
728 # see #1745761
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
729 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
730 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
731 [("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
732 ("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
733
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
734
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
735
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
736 def test_main():
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
737 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
738 AttributesStrictTestCase, AttributesTolerantTestCase)
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
739
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
740
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
741 if __name__ == "__main__":
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
742 test_main()