diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-02-10 08:50:49 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-02-10 08:50:49 (GMT) |
commit | 176630ec1968e4467f8bd45745c6e402760be072 (patch) | |
tree | a0feefcce4ac357f17cd0fd5db54ca1881567e37 | |
parent | 9f90a731eb9a09e9e2fc022800475ff22206ac01 (diff) | |
parent | fa3702dc28fa8aef291785c560832c9af60305a8 (diff) | |
download | cpython-176630ec1968e4467f8bd45745c6e402760be072.zip cpython-176630ec1968e4467f8bd45745c6e402760be072.tar.gz cpython-176630ec1968e4467f8bd45745c6e402760be072.tar.bz2 |
#13960: merge with 3.2.
-rw-r--r-- | Lib/html/parser.py | 25 | ||||
-rw-r--r-- | Lib/test/test_htmlparser.py | 30 | ||||
-rw-r--r-- | Misc/NEWS | 5 |
3 files changed, 58 insertions, 2 deletions
diff --git a/Lib/html/parser.py b/Lib/html/parser.py index dd9c2e1..5c4a7ef 100644 --- a/Lib/html/parser.py +++ b/Lib/html/parser.py @@ -184,7 +184,17 @@ class HTMLParser(_markupbase.ParserBase): elif startswith("<?", i): k = self.parse_pi(i) elif startswith("<!", i): - k = self.parse_declaration(i) + # this might fail with things like <! not a comment > or + # <! -- space before '--' -->. When strict is True an + # error is raised, when it's False they will be considered + # as bogus comments and parsed (see parse_bogus_comment). + if self.strict: + k = self.parse_declaration(i) + else: + try: + k = self.parse_declaration(i) + except HTMLParseError: + k = self.parse_bogus_comment(i) elif (i + 1) < n: self.handle_data("<") k = i + 1 @@ -256,6 +266,19 @@ class HTMLParser(_markupbase.ParserBase): i = self.updatepos(i, n) self.rawdata = rawdata[i:] + # Internal -- parse bogus comment, return length or -1 if not terminated + # see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state + def parse_bogus_comment(self, i, report=1): + rawdata = self.rawdata + if rawdata[i:i+2] != '<!': + self.error('unexpected call to parse_comment()') + pos = rawdata.find('>', i+2) + if pos == -1: + return -1 + if report: + self.handle_comment(rawdata[i+2:pos]) + return pos + 1 + # Internal -- parse processing instr, return end or -1 if not terminated def parse_pi(self, i): rawdata = self.rawdata diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 8c2e25e..7af9131 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -323,6 +323,23 @@ DOCTYPE html [ ("endtag", element_lower)], collector=Collector()) + def test_comments(self): + html = ("<!-- I'm a valid comment -->" + '<!--me too!-->' + '<!------>' + '<!---->' + '<!----I have many hyphens---->' + '<!-- I have a > in the middle -->' + '<!-- and I have -- in the middle! -->') + expected = [('comment', " I'm a valid comment "), + ('comment', 'me too!'), + ('comment', '--'), + ('comment', ''), + ('comment', '--I have many hyphens--'), + ('comment', ' I have a > in the middle '), + ('comment', ' and I have -- in the middle! ')] + self._run_check(html, expected) + def test_condcoms(self): html = ('<!--[if IE & !(lte IE 8)]>aren\'t<![endif]-->' '<!--[if IE 8]>condcoms<![endif]-->' @@ -426,6 +443,19 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase): # see #12888 self.assertEqual(p.unescape('{ ' * 1050), '{ ' * 1050) + def test_broken_comments(self): + html = ('<! not really a comment >' + '<! not a comment either -->' + '<! -- close enough -->' + '<!!! another bogus comment !!!>') + expected = [ + ('comment', ' not really a comment '), + ('comment', ' not a comment either --'), + ('comment', ' -- close enough --'), + ('comment', '!! another bogus comment !!!'), + ] + self._run_check(html, expected) + def test_broken_condcoms(self): # these condcoms are missing the '--' after '<!' and before the '>' html = ('<![if !(IE)]>broken condcom<![endif]>' @@ -466,6 +466,9 @@ Core and Builtins Library ------- +- Issue #13960: HTMLParser is now able to handle broken comments when + strict=False. + - Issue #13921: Undocument and clean up sqlite3.OptimizedUnicode, which is obsolete in Python 3.x. It's now aliased to str for backwards compatibility. @@ -498,7 +501,7 @@ Library - Issue #10881: Fix test_site failure with OS X framework builds. -- Issue #964437 Make IDLE help window non-modal. +- Issue #964437: Make IDLE help window non-modal. Patch by Guilherme Polo and Roger Serwy. - Issue #13734: Add os.fwalk(), a directory walking function yielding file |