annotate Lib/test/test_htmlparser.py @ 84007:dbfbdf2b5c19 3.3

Fix #17967: For ftp urls CWD to target instead of hopping to each directory towards target. This fixes a bug where target is accessible, but parent directories are restricted.
author Senthil Kumaran <senthil@uthcode.com>
date Sat, 01 Jun 2013 08:27:06 -0700
parents 9cb90c1a1a46
children b77c1a19028e 0a56709eb798
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):
77630
8dd2f5754b2f #15114: the strict mode of HTMLParser and the HTMLParseError exception are deprecated now that the parser is able to parse invalid markup.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76414
diff changeset
105 with support.check_warnings(("", DeprecationWarning), quite=False):
8dd2f5754b2f #15114: the strict mode of HTMLParser and the HTMLParseError exception are deprecated now that the parser is able to parse invalid markup.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76414
diff changeset
106 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
107
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
108 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
109 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
110 ("pi", "processing instruction"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 ])
28660
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
112 self._run_check("<?processing instruction ?>", [
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
113 ("pi", "processing instruction ?"),
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
114 ])
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
116 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
117 self._run_check("""
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 <!DOCTYPE html PUBLIC 'foo'>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 <HTML>&entity;&#32;
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 <!--comment1a
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 -></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
122 comment1b-->
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 <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
124 text
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
125 &#x201C;
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
126 <!--comment2a-- --comment2b-->
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 </Html>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 """, [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 ("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
131 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132 ("starttag", "html", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 ("entityref", "entity"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 ("charref", "32"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 ("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
137 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 ("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
139 ("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
140 ("charref", "x201C"),
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
141 ("data", "\n"),
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 ("comment", "comment2a-- --comment2b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 ("endtag", "html"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147
61889
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
148 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
149 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
150 ("starttag", "p", []),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
151 ("data", "&#bad;"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
152 ("endtag", "p"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
153 ])
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
154
21157
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
155 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
156 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
157 ("entityref", "entityref"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
158 ("data", " foo"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
159 ])
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
160
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
161 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
162 # 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
163 # 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
164 # 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
165 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
166 ("starttag", "a", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 ("starttag", "b", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 ("endtag", "a"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 ("endtag", "b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
172 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
173 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
174 ("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
175 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
176
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
177 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
178 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
179 ("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
180 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
181
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
182 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
183 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
184
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
185 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
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 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
188
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
189 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
190 output = [("starttag", "a", [("b", "<")])]
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 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
197
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 output = [("starttag", "a", [("b", ">")])]
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 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
205
33432
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
206 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
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(["<!--", "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
212 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
213 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
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 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
218
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
219 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
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("</")
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")
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("</a<a>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226 self._parse_error("<!")
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")
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='bar")
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 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
232
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
233 def test_valid_doctypes(self):
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
234 # 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
235 dtds = ['HTML', # HTML5 doctype
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
236 ('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
237 '"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
238 ('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
239 '"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
240 ('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
241 '"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
242 ('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
243 '"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
244 ('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
245 '"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
246 ('html PUBLIC "-//W3C//DTD '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
247 '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
248 '"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
249 ('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
250 '"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
251 '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
252 '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
253 for dtd in dtds:
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
254 self._run_check("<!DOCTYPE %s>" % dtd,
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
255 [('decl', 'DOCTYPE ' + dtd)])
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
256
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
257 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
258 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
259
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
260 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
261 self._run_check("<p/>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262 ("startendtag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 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
265 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 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
269 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270 ("startendtag", "img", [("src", "foo")]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
274 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
275 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
276 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
277 ("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
278 ("starttag_text", s)])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
279
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
280 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
281 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
282 '<!-- 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
283 "<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
284 '<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
285 '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
286 '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
287 '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
288 '<!-- 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
289 ('\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
290 '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
291 '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
292 '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
293 '\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
294 '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
295 '<!-- \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
296 # 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
297 # 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
298 #'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
299 #'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
300 ]
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 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
302 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
303 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
304 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
305 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
306 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
307 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
308 ("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
309 ("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
310
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
311 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
312 # 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
313 # 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
314 # 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
315 # 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
316 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
317 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
318 return self.events
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
320 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
321 <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
322 '</script' + '>'"""
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
323 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
324 '\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
325 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
326 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
327 content=content)
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
328 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
329 ("data", content),
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
330 ("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
331 collector=Collector())
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
333 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
334 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
335 '<!--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
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 '<!---->'
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 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
339 '<!-- 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
340 '<!-- 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
341 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
342 ('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
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', ''),
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 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
346 ('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
347 ('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
348 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
349
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
350 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
351 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
352 '<!--[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
353 '<!--[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
354 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
355 ('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
356 ('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
357 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
358
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
359
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
360 class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
361
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
362 def get_collector(self):
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
363 return EventCollector(strict=False)
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
364
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
365 def test_tolerant_parsing(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
366 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
367 '<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
368 ('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
369 ('data', 'te>>xt'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
370 ('entityref', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
371 ('data', '<<bc'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
372 ('endtag', 'a'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
373 ('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
374 ('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
375 ('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
376 ('endtag', 'html<')])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
377
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
378 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
379 self._run_check("</>", [])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
380 self._run_check("</$>", [('comment', '$')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
381 self._run_check("</", [('data', '</')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
382 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
383 # 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
384 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
385 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
386 self._run_check("<!", [('data', '<!')])
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", [('data', '<a')])
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='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
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 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
392
75117
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
393 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
394 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
395 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
396 '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
397 '/*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
398 expected = [(
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
399 'startendtag', 'img',
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
400 [('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
401 ('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
402 ('*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
403 ('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
404 )]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
405 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
406 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
407 '<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
408 expected = [
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
409 ('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
410 ('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
411 ]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
412 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
413 #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
414 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
415 '<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
416 expected = [
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 ('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
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 ('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
421 ]
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
422 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
423
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
424 def test_declaration_junk_chars(self):
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
425 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
426
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
427 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
428 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
429 [('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
430
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
431 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
432 # see #12008
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
433 html = ("<html><body bgcolor=d0ca90 text='181008'>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
434 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
435 "<td align=left><font size=-1>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
436 "- <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
437 "- <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
438 expected = [
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
439 ('starttag', 'html', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
440 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
441 ('starttag', 'table',
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
442 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
443 ('starttag', 'tr', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
444 ('starttag', 'td', [('align', 'left')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
445 ('starttag', 'font', [('size', '-1')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
446 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
447 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
448 ('endtag', 'span'), ('endtag', 'a'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
449 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
450 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
451 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
452 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
453 self._run_check(html, expected)
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
454
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
455 def test_comma_between_attributes(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
456 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
457 'method="post">', [
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
458 ('starttag', 'form',
77742
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
459 [('action', '/xxx.php?a=1&b=2&'),
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
460 (',', None), ('method', 'post')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
461
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
462 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
463 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
464 ('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
465 [('action', 'bogus|&#()value')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
466
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
467 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
468 # 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
469 # 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
470 # 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
471 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
472 '</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
473 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
474 # < 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
475 ('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
476 ('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
477 # 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
478 ('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
479 ('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
480 # 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
481 ('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
482 ('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
483 # 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
484 ('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
485 ('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
486 # 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
487 ('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
488 ('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
489 # </> 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
490 ('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
491 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
492
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 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
494 # 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
495 # 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
496 # 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
497 # 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
498 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
499 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
500 ('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
501 ('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
502 ('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
503 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
504
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
505 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
506 # 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
507 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
508 '<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
509 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
510 ('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
511 ('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
512 ('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
513 ('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
514 ('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
515 ('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
516 ('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
517 ('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
518 ('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
519 ('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
520 ('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
521 ('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
522 ('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
523 ('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
524 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
525 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
526
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 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
528 expected = [
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
529 ('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
530 ('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
531 ('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
532 ('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
533 ('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
534 ('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
535 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
536 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
537
83586
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
538 def test_EOF_in_charref(self):
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
539 # see #17802
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
540 # This test checks that the UnboundLocalError reported in the issue
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
541 # is not raised, however I'm not sure the returned values are correct.
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
542 # Maybe HTMLParser should use self.unescape for these
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
543 data = [
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
544 ('a&', [('data', 'a&')]),
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
545 ('a&b', [('data', 'ab')]),
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
546 ('a&b ', [('data', 'a'), ('entityref', 'b'), ('data', ' ')]),
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
547 ('a&b;', [('data', 'a'), ('entityref', 'b')]),
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
548 ]
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
549 for html, expected in data:
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
550 self._run_check(html, expected)
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
551
67189
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
552 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
553 p = self.get_collector()
67189
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
554 self.assertEqual(p.unescape('&#bad;'),'&#bad;')
3546ce1aa355 Merged revisions 87542 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 61889
diff changeset
555 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
556 # 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
557 self.assertEqual(p.unescape('&#123; ' * 1050), '{ ' * 1050)
77742
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
558 # see #15156
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
559 self.assertEqual(p.unescape('&Eacuteric&Eacute;ric'
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
560 '&alphacentauri&alpha;centauri'),
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
561 'ÉricÉric&alphacentauriαcentauri')
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
562 self.assertEqual(p.unescape('&co;'), '&co;')
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
563
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
564 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
565 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
566 '<! 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
567 '<! -- close enough -->'
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
568 '<!><!<-- 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
569 '<!!! 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
570 expected = [
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
571 ('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
572 ('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
573 ('comment', ' -- close enough --'),
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
574 ('comment', ''),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
575 ('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
576 ('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
577 ]
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
578 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
579
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
580 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
581 # 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
582 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
583 '<![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
584 '<![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
585 '<![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
586 '<![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
587 # 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
588 # 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
589 # 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
590 # provides more flexibility.
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
591 # 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
592 expected = [
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
593 ('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
594 ('data', 'broken condcom'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
595 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
596 ('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
597 ('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
598 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
599 ('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
600 ('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
601 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
602 ('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
603 ('starttag', 'b', []),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
604 ('data', 'foo'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
605 ('endtag', 'b'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
606 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
607 ('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
608 ('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
609 ('unknown decl', 'endif')
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
610 ]
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
611 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
612
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
613
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
614 class AttributesStrictTestCase(TestCaseBase):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
615
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
616 def get_collector(self):
77630
8dd2f5754b2f #15114: the strict mode of HTMLParser and the HTMLParseError exception are deprecated now that the parser is able to parse invalid markup.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76414
diff changeset
617 with support.check_warnings(("", DeprecationWarning), quite=False):
8dd2f5754b2f #15114: the strict mode of HTMLParser and the HTMLParseError exception are deprecated now that the parser is able to parse invalid markup.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76414
diff changeset
618 return EventCollector(strict=True)
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
619
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
620 def test_attr_syntax(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
621 output = [
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
622 ("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
623 ]
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
624 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
625 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
626 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
627 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
628
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
629 def test_attr_values(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
630 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
631 [("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
632 ("c", "yyy\t\nyyy"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
633 ("d", "\txyz\n")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
634 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
635 [("starttag", "a", [("b", ""), ("c", "")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
636 # 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
637 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
638 [("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
639 # 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
640 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
641 "<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
642 [("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
643
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
644 def test_attr_nonascii(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
645 # see issue 7311
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
646 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
647 "<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
648 [("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
649 ("alt", "\u4e2d\u6587")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
650 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
651 "<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
652 [("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
653 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
654 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
655 '<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
656 [("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
657 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
658
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
659 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
660 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
661 "<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
662 [("starttag", "a", [("b", "&><\"'")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
663
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
664 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
665 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
666 "<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
667 [("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
668
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
669 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
670 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
671 "<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
672 [("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
673
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
674
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
675
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
676 class AttributesTolerantTestCase(AttributesStrictTestCase):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
677
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
678 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
679 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
680
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
681 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
682 self._run_check(
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
683 "<a $><b $=%><c \=/>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
684 [("starttag", "a", [("$", None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
685 ("starttag", "b", [("$", "%")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
686 ("starttag", "c", [("\\", "/")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
687
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
688 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
689 # see #1200313
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
690 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
691 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
692 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
693 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
694 [("starttag", "a", [("href", "&")])])
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("<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
696 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
697
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
698 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
699 # see #13357
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
700 html = (
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
701 "<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
702 "<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
703 "<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
704 "<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
705 )
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
706 expected = [
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
707 ('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
708 ('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
709 ('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
710 ('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
711 ('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
712 ('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
713 ('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
714 ('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
715 ]
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
716 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
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_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
719 # see #12629
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
720 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
721 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
722 ('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
723 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
724 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
725 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
726 ('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
727 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
728
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
729 # 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
730 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
731 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
732 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
733 [("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
734
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
735 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
736 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
737 [("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
738
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
739 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
740 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
741 [("starttag", "a", [("v", "")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
742
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
743 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
744 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
745 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
746 [("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
747
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
748 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
749 # see #1745761
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
750 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
751 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
752 [("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
753 ("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
754
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
755
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
756
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
757 def test_main():
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
758 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
759 AttributesStrictTestCase, AttributesTolerantTestCase)
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
760
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
761
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
762 if __name__ == "__main__":
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
763 test_main()