diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-02-13 16:10:44 +0200 |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-02-13 16:10:44 +0200 |
commit | 4b92cc3f7924e455b7e41cf1a66034a44ede0cc0 (patch) | |
tree | 46a2c5992e840899d901932bb2559086d500fcc3 /Lib/HTMLParser.py | |
parent | 32b6371460e9b3b2708be2e732599f3c9a1fe336 (diff) | |
download | cpython-git-4b92cc3f7924e455b7e41cf1a66034a44ede0cc0.tar.gz |
#13960: HTMLParser is now able to handle broken comments.
Diffstat (limited to 'Lib/HTMLParser.py')
-rw-r--r-- | Lib/HTMLParser.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/Lib/HTMLParser.py b/Lib/HTMLParser.py index 1c6989e7d4..516bc70147 100644 --- a/Lib/HTMLParser.py +++ b/Lib/HTMLParser.py @@ -160,7 +160,7 @@ class HTMLParser(markupbase.ParserBase): elif startswith("<?", i): k = self.parse_pi(i) elif startswith("<!", i): - k = self.parse_declaration(i) + k = self.parse_html_declaration(i) elif (i + 1) < n: self.handle_data("<") k = i + 1 @@ -218,6 +218,40 @@ class HTMLParser(markupbase.ParserBase): i = self.updatepos(i, n) self.rawdata = rawdata[i:] + # Internal -- parse html declarations, return length or -1 if not terminated + # See w3.org/TR/html5/tokenization.html#markup-declaration-open-state + # See also parse_declaration in _markupbase + def parse_html_declaration(self, i): + rawdata = self.rawdata + if rawdata[i:i+2] != '<!': + self.error('unexpected call to parse_html_declaration()') + if rawdata[i:i+4] == '<!--': + return self.parse_comment(i) + elif rawdata[i:i+3] == '<![': + return self.parse_marked_section(i) + elif rawdata[i:i+9].lower() == '<!doctype': + # find the closing > + gtpos = rawdata.find('>', 9) + if gtpos == -1: + return -1 + self.handle_decl(rawdata[i+2:gtpos]) + return gtpos+1 + else: + return self.parse_bogus_comment(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 |