annotate Lib/test/test_htmlparser.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 1575f2dd08c4
children
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
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
73 class EventCollectorCharrefs(EventCollector):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
74
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
75 def get_events(self):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
76 return self.events
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
77
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
78 def handle_charref(self, data):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
79 self.fail('This should never be called with convert_charrefs=True')
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
80
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
81 def handle_entityref(self, data):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
82 self.fail('This should never be called with convert_charrefs=True')
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
83
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
84
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 class TestCaseBase(unittest.TestCase):
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
87 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
88 raise NotImplementedError
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
89
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
90 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
91 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
92 collector = self.get_collector()
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
93 parser = collector
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 for s in source:
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 parser.feed(s)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 parser.close()
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
97 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
98 if events != expected_events:
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
99 self.fail("received events did not match expected events" +
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
100 "\nSource:\n" + repr(source) +
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
101 "\nExpected:\n" + pprint.pformat(expected_events) +
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
102 "\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
103
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 def _run_check_extra(self, source, events):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
105 self._run_check(source, events,
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
106 EventCollectorExtra(convert_charrefs=False))
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 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
109 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
110 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
111 parser.feed(source)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 parser.close()
86842
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
113 with self.assertRaises(html.parser.HTMLParseError):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
114 with self.assertWarns(DeprecationWarning):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
115 parse()
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
118 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
119
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
120 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
121 with support.check_warnings(("", DeprecationWarning), quite=False):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
122 return EventCollector(strict=True, convert_charrefs=False)
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
124 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
125 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
126 ("pi", "processing instruction"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 ])
28660
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
128 self._run_check("<?processing instruction ?>", [
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
129 ("pi", "processing instruction ?"),
5f1ef1882b25 Add test that demonstrates SGML-style handling of processing
Fred Drake <fdrake@acm.org>
parents: 28297
diff changeset
130 ])
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
132 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
133 self._run_check("""
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 <!DOCTYPE html PUBLIC 'foo'>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 <HTML>&entity;&#32;
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 <!--comment1a
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 -></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
138 comment1b-->
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 <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
140 text
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
141 &#x201C;
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
142 <!--comment2a-- --comment2b-->
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 </Html>
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 """, [
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 ("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
147 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148 ("starttag", "html", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 ("entityref", "entity"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 ("charref", "32"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 ("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
153 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 ("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
155 ("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
156 ("charref", "x201C"),
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
157 ("data", "\n"),
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 ("comment", "comment2a-- --comment2b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 ("endtag", "html"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
161 ("data", "\n"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163
61889
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
164 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
165 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
166 ("starttag", "p", []),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
167 ("data", "&#bad;"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
168 ("endtag", "p"),
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
169 ])
bf04eaf1af74 Recorded merge of revisions 81500-81501 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 47205
diff changeset
170
21157
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
171 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
172 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
173 ("entityref", "entityref"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
174 ("data", " foo"),
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
175 ])
e592472d55a3 Add a test that makes sure unclosed entity references are handled consitently.
Fred Drake <fdrake@acm.org>
parents: 20069
diff changeset
176
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
177 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
178 # 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
179 # 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
180 # 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
181 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
182 ("starttag", "a", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 ("starttag", "b", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 ("endtag", "a"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 ("endtag", "b"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187
19410
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
188 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
189 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
190 ("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
191 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
192
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
193 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
194 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
195 ("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
196 ])
c403af003884 Deal more appropriately with bare ampersands and pointy brackets; this
Fred Drake <fdrake@acm.org>
parents: 18985
diff changeset
197
19683
8cbc74f5586b Enhanced the test for DOCTYPE declarations, added a test for dealing with
Fred Drake <fdrake@acm.org>
parents: 19410
diff changeset
198 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
199 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
200
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
201 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
202 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
203 self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
205 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
206 output = [("starttag", "a", [("b", "<")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 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
208 self._run_check(["<a ", "b='<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209 self._run_check(["<a b", "='<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 self._run_check(["<a b=", "'<'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 self._run_check(["<a b='<", "'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212 self._run_check(["<a b='<'", ">"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214 output = [("starttag", "a", [("b", ">")])]
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 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
216 self._run_check(["<a ", "b='>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 self._run_check(["<a b", "='>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218 self._run_check(["<a b=", "'>'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 self._run_check(["<a b='>", "'>"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 self._run_check(["<a b='>'", ">"], output)
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221
33432
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
222 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
223 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
224 self._run_check(["<", "!--abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
225 self._run_check(["<!", "--abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
226 self._run_check(["<!-", "-abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
227 self._run_check(["<!--", "abc-->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
228 self._run_check(["<!--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
229 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
230 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
231 self._run_check(["<!--abc-", "->"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
232 self._run_check(["<!--abc--", ">"], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
233 self._run_check(["<!--abc-->", ""], output)
4b708fa0a60a add tests that make sure buffer boundaries are handled properly for SGML comments
Fred Drake <fdrake@acm.org>
parents: 32538
diff changeset
234
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
235 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
236 self._parse_error("</>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 self._parse_error("</$>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 self._parse_error("</")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239 self._parse_error("</a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
240 self._parse_error("<a<a>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 self._parse_error("</a<a>")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 self._parse_error("<!")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243 self._parse_error("<a")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
244 self._parse_error("<a foo='bar'")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245 self._parse_error("<a foo='bar")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
246 self._parse_error("<a foo='>'")
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
247 self._parse_error("<a foo='>")
86989
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
248 self._parse_error("<a$>")
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
249 self._parse_error("<a$b>")
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
250 self._parse_error("<a$b/>")
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
251 self._parse_error("<a$b >")
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
252 self._parse_error("<a$b />")
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
253
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
254 def test_valid_doctypes(self):
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
255 # 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
256 dtds = ['HTML', # HTML5 doctype
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
257 ('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
258 '"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
259 ('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
260 '"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
261 ('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
262 '"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
263 ('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
264 '"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
265 ('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
266 '"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
267 ('html PUBLIC "-//W3C//DTD '
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
268 '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
269 '"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
270 ('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
271 '"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
272 '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
273 '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
274 for dtd in dtds:
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
275 self._run_check("<!DOCTYPE %s>" % dtd,
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
276 [('decl', 'DOCTYPE ' + dtd)])
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
277
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
278 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
279 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
280
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
281 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
282 self._run_check("<p/>", [
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 ("startendtag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285 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
286 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
287 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
288 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
289 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
290 ("starttag", "p", []),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
291 ("startendtag", "img", [("src", "foo")]),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292 ("endtag", "p"),
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293 ])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
295 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
296 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
297 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
298 ("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
299 ("starttag_text", s)])
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
300
18985
ba9e4f55133f Fix stupid bug: when migrating these tests from the Zope repository, the
Fred Drake <fdrake@acm.org>
parents: 18634
diff changeset
301 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
302 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 '<!-- 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
304 "<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
305 '<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
306 '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
307 '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
308 '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
309 '<!-- 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
310 ('\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
311 '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
312 '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
313 '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
314 '\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
315 '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
316 '<!-- \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
317 # 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
318 # 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
319 #'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
320 #'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
321 ]
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
322 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
323 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
324 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
325 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
326 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
327 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
328 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
329 ("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
330 ("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
331
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
332 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
333 # 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
334 # 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
335 # 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
336 # 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
337 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
338 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
339 return self.events
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340
73609
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
341 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
342 <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
343 '</script' + '>'"""
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
344 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
345 '\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
346 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
347 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
348 content=content)
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
349 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
350 ("data", content),
0a32e7e3aa1f #13358: HTMLParser now calls handle_data only once for each CDATA.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73547
diff changeset
351 ("endtag", element_lower)],
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
352 collector=Collector(convert_charrefs=False))
17854
028a1d39efb6 Added test suite for the new HTMLParser module, originally from the
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
354 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
355 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
356 '<!--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
357 '<!------>'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
358 '<!---->'
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
359 '<!----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
360 '<!-- 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
361 '<!-- 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
362 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
363 ('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
364 ('comment', '--'),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
365 ('comment', ''),
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
366 ('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
367 ('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
368 ('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
369 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
370
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
371 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
372 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
373 '<!--[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
374 '<!--[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
375 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
376 ('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
377 ('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
378 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
379
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
380 def test_convert_charrefs(self):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
381 collector = lambda: EventCollectorCharrefs(convert_charrefs=True)
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
382 self.assertTrue(collector().convert_charrefs)
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
383 charrefs = ['&quot;', '&#34;', '&#x22;', '&quot', '&#34', '&#x22']
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
384 # check charrefs in the middle of the text/attributes
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
385 expected = [('starttag', 'a', [('href', 'foo"zar')]),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
386 ('data', 'a"z'), ('endtag', 'a')]
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
387 for charref in charrefs:
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
388 self._run_check('<a href="foo{0}zar">a{0}z</a>'.format(charref),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
389 expected, collector=collector())
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
390 # check charrefs at the beginning/end of the text/attributes
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
391 expected = [('data', '"'),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
392 ('starttag', 'a', [('x', '"'), ('y', '"X'), ('z', 'X"')]),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
393 ('data', '"'), ('endtag', 'a'), ('data', '"')]
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
394 for charref in charrefs:
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
395 self._run_check('{0}<a x="{0}" y="{0}X" z="X{0}">'
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
396 '{0}</a>{0}'.format(charref),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
397 expected, collector=collector())
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
398 # check charrefs in <script>/<style> elements
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
399 for charref in charrefs:
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
400 text = 'X'.join([charref]*3)
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
401 expected = [('data', '"'),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
402 ('starttag', 'script', []), ('data', text),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
403 ('endtag', 'script'), ('data', '"'),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
404 ('starttag', 'style', []), ('data', text),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
405 ('endtag', 'style'), ('data', '"')]
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
406 self._run_check('{1}<script>{0}</script>{1}'
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
407 '<style>{0}</style>{1}'.format(text, charref),
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
408 expected, collector=collector())
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
409 # check truncated charrefs at the end of the file
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
410 html = '&quo &# &#x'
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
411 for x in range(1, len(html)):
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
412 self._run_check(html[:x], [('data', html[:x])],
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
413 collector=collector())
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
414 # check a string with no charrefs
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
415 self._run_check('no charrefs here', [('data', 'no charrefs here')],
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
416 collector=collector())
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
417
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
418
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
419 class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
420
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
421 def get_collector(self):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
422 return EventCollector(convert_charrefs=False)
86842
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
423
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
424 def test_deprecation_warnings(self):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
425 with self.assertWarns(DeprecationWarning):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
426 EventCollector() # convert_charrefs not passed explicitly
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
427 with self.assertWarns(DeprecationWarning):
86842
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
428 EventCollector(strict=True)
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
429 with self.assertWarns(DeprecationWarning):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
430 EventCollector(strict=False)
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
431 with self.assertRaises(html.parser.HTMLParseError):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
432 with self.assertWarns(DeprecationWarning):
0a56709eb798 #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
433 EventCollector().error('test')
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
434
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
435 def test_tolerant_parsing(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
436 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
437 '<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
438 ('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
439 ('data', 'te>>xt'),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
440 ('entityref', 'a'),
86989
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
441 ('data', '<'),
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
442 ('starttag', 'bc<', [('a', None)]),
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
443 ('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
444 ('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
445 ('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
446 ('endtag', 'html<')])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
447
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
448 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
449 self._run_check("</>", [])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
450 self._run_check("</$>", [('comment', '$')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
451 self._run_check("</", [('data', '</')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
452 self._run_check("</a", [('data', '</a')])
86989
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
453 self._run_check("<a<a>", [('starttag', 'a<a', [])])
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
454 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
455 self._run_check("<!", [('data', '<!')])
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
456 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
457 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
458 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
459 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
460 self._run_check("<a foo='>", [('data', "<a foo='>")])
86989
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
461 self._run_check("<a$>", [('starttag', 'a$', [])])
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
462 self._run_check("<a$b>", [('starttag', 'a$b', [])])
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
463 self._run_check("<a$b/>", [('startendtag', 'a$b', [])])
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
464 self._run_check("<a$b >", [('starttag', 'a$b', [])])
9b9d188ed549 #19480: HTMLParser now accepts all valid start-tag names as defined by the HTML5 standard.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 86844
diff changeset
465 self._run_check("<a$b />", [('startendtag', 'a$b', [])])
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
466
75117
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
467 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
468 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
469 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
470 '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
471 '/*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
472 expected = [(
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
473 'startendtag', 'img',
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
474 [('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
475 ('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
476 ('*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
477 ('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
478 )]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
479 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
480 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
481 '<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
482 expected = [
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
483 ('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
484 ('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
485 ]
2d16048b10cd HTMLParser is now able to handle slashes in the start tag.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74913
diff changeset
486 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
487 #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
488 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
489 '<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
490 expected = [
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
491 ('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
492 ('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
493 ('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
494 ('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
495 ]
ba4baaddac8d #14538: HTMLParser can now parse correctly start tags that contain a bare /.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75117
diff changeset
496 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
497
74905
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
498 def test_declaration_junk_chars(self):
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
499 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
500
3f9531a4ef09 Fix htmlparser tests to always use the right collector.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74901
diff changeset
501 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
502 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
503 [('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
504
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
505 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
506 # see #12008
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
507 html = ("<html><body bgcolor=d0ca90 text='181008'>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
508 "<table cellspacing=0 cellpadding=1 width=100% ><tr>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
509 "<td align=left><font size=-1>"
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
510 "- <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
511 "- <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
512 expected = [
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
513 ('starttag', 'html', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
514 ('starttag', 'body', [('bgcolor', 'd0ca90'), ('text', '181008')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
515 ('starttag', 'table',
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
516 [('cellspacing', '0'), ('cellpadding', '1'), ('width', '100%')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
517 ('starttag', 'tr', []),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
518 ('starttag', 'td', [('align', 'left')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
519 ('starttag', 'font', [('size', '-1')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
520 ('data', '- '), ('starttag', 'a', [('href', '/rabota/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
521 ('starttag', 'span', [('class', 'en')]), ('data', ' software-and-i'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
522 ('endtag', 'span'), ('endtag', 'a'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
523 ('data', '- '), ('starttag', 'a', [('href', '/1/')]),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
524 ('starttag', 'span', [('class', 'en')]), ('data', ' library'),
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
525 ('endtag', 'span'), ('endtag', 'a'), ('endtag', 'table')
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
526 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
527 self._run_check(html, expected)
73277
6107a84e3c44 #12008: add a test.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73274
diff changeset
528
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
529 def test_comma_between_attributes(self):
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
530 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
531 'method="post">', [
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
532 ('starttag', 'form',
77742
0d53703b1a99 #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77630
diff changeset
533 [('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
534 (',', None), ('method', 'post')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
535
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
536 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
537 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
538 ('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
539 [('action', 'bogus|&#()value')])])
66623
1cbfeffea19f #1486713: Add a tolerant mode to HTMLParser.
R. David Murray <rdmurray@bitdance.com>
parents: 61889
diff changeset
540
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
541 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
542 # 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
543 # 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
544 # 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
545 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
546 '</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
547 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
548 # < 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
549 ('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
550 ('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
551 # 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
552 ('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
553 ('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
554 # 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
555 ('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
556 ('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
557 # 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
558 ('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
559 ('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
560 # 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
561 ('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
562 ('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
563 # </> 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
564 ('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
565 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
566
df5e5eea7833 #13993: HTMLParser is now able to handle broken end tags when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74859
diff changeset
567 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
568 # 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
569 # 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
570 # 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
571 # 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
572 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
573 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
574 ('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
575 ('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
576 ('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
577 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
578
73279
4404e784918c Avoid reusing the same collector in the tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73277
diff changeset
579 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
580 # 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
581 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
582 '<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
583 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
584 ('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
585 ('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
586 ('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
587 ('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
588 ('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
589 ('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
590 ('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
591 ('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
592 ('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
593 ('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
594 ('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
595 ('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
596 ('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
597 ('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
598 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
599 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
600
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
601 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
602 expected = [
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
603 ('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
604 ('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
605 ('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
606 ('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
607 ('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
608 ('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
609 ]
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
610 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
611
83586
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
612 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
613 # 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
614 # 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
615 # 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
616 # 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
617 data = [
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
618 ('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
619 ('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
620 ('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
621 ('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
622 ]
9cb90c1a1a46 #17802: Fix an UnboundLocalError in html.parser. Initial tests by Thomas Barlow.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 77742
diff changeset
623 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
624 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
625
87334
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
626 def test_unescape_method(self):
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
627 from html import unescape
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
628 p = self.get_collector()
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
629 with self.assertWarns(DeprecationWarning):
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
630 s = '&quot;&#34;&#x22;&quot&#34&#x22&#bad;'
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
631 self.assertEqual(p.unescape(s), unescape(s))
0534b882f9ce #19688: add back and deprecate the internal HTMLParser.unescape() method.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87275
diff changeset
632
74859
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
633 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
634 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
635 '<! 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
636 '<! -- close enough -->'
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
637 '<!><!<-- 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
638 '<!!! 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
639 expected = [
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
640 ('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
641 ('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
642 ('comment', ' -- close enough --'),
74906
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
643 ('comment', ''),
4c4ff9fd19b6 Improve handling of declarations in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74905
diff changeset
644 ('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
645 ('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
646 ]
242b697449d8 #13960: HTMLParser is now able to handle broken comments when strict=False.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 74070
diff changeset
647 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
648
74070
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
649 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
650 # 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
651 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
652 '<![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
653 '<![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
654 '<![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
655 '<![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
656 # 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
657 # 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
658 # 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
659 # provides more flexibility.
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
660 # 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
661 expected = [
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
662 ('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
663 ('data', 'broken condcom'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
664 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
665 ('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
666 ('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
667 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
668 ('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
669 ('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
670 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
671 ('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
672 ('starttag', 'b', []),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
673 ('data', 'foo'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
674 ('endtag', 'b'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
675 ('unknown decl', 'endif'),
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
676 ('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
677 ('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
678 ('unknown decl', 'endif')
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
679 ]
4ddbb756b602 #13576: add tests about the handling of (possibly broken) condcoms.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73609
diff changeset
680 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
681
73289
8f9f490b4777 Make sure that the tolerant parser still parses valid HTML correctly.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73279
diff changeset
682
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
683 class AttributesStrictTestCase(TestCaseBase):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
684
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
685 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
686 with support.check_warnings(("", DeprecationWarning), quite=False):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
687 return EventCollector(strict=True, convert_charrefs=False)
73546
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
688
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
689 def test_attr_syntax(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
690 output = [
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
691 ("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
692 ]
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
693 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
694 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
695 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
696 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
697
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
698 def test_attr_values(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
699 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
700 [("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
701 ("c", "yyy\t\nyyy"),
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
702 ("d", "\txyz\n")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
703 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
704 [("starttag", "a", [("b", ""), ("c", "")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
705 # 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
706 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
707 [("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
708 # 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
709 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
710 "<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
711 [("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
712
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
713 def test_attr_nonascii(self):
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
714 # see issue 7311
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
715 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
716 "<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
717 [("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
718 ("alt", "\u4e2d\u6587")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
719 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
720 "<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
721 [("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
722 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
723 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
724 '<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
725 [("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
726 ("href", "\u30c6\u30b9\u30c8.html")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
727
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
728 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
729 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
730 "<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
731 [("starttag", "a", [("b", "&><\"'")])])
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
732
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
733 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
734 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
735 "<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
736 [("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
737
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
738 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
739 self._run_check(
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
740 "<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
741 [("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
742
62d4dc5bb3ec Group tests about attributes in a separate class.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73289
diff changeset
743
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
744
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
745 class AttributesTolerantTestCase(AttributesStrictTestCase):
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
746
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
747 def get_collector(self):
87437
1575f2dd08c4 #13633: Added a new convert_charrefs keyword arg to HTMLParser that, when True, automatically converts all character references.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 87334
diff changeset
748 return EventCollector(convert_charrefs=False)
73547
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
749
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
750 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
751 self._run_check(
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
752 "<a $><b $=%><c \=/>",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
753 [("starttag", "a", [("$", None)]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
754 ("starttag", "b", [("$", "%")]),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
755 ("starttag", "c", [("\\", "/")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
756
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
757 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
758 # see #1200313
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
759 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
760 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
761 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
762 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
763 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
764 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
765 [("starttag", "a", [("href", "&")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
766
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
767 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
768 # see #13357
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
769 html = (
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
770 "<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
771 "<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
772 "<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
773 "<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
774 )
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
775 expected = [
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
776 ('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
777 ('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
778 ('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
779 ('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
780 ('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
781 ('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
782 ('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
783 ('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
784 ]
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
785 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
786
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
787 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
788 # see #12629
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
789 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
790 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
791 ('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
792 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
793 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
794 [('starttag', 'x', []),
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
795 ('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
796 ('endtag', 'x')])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
797
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
798 # 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
799 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
800 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
801 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
802 [("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
803
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
804 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
805 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
806 [("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
807
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
808 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
809 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
810 [("starttag", "a", [("v", "")])])
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
811
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
812 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
813 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
814 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
815 [("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
816
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
817 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
818 # see #1745761
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
819 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
820 [("starttag", "a",
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
821 [("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
822 ("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
823
16ed15ff0d7c #1745761, #755670, #13357, #12629, #1200313: improve attribute handling in HTMLParser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73546
diff changeset
824
20069
90a4f23abda7 Adapt to use the test_main() approach.
Fred Drake <fdrake@acm.org>
parents: 19687
diff changeset
825 if __name__ == "__main__":
86844
b77c1a19028e Use unittest.main() in test_htmlparser.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83586
diff changeset
826 unittest.main()