summaryrefslogtreecommitdiff
path: root/Lib/html/parser.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2012-02-13 15:50:37 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2012-02-13 15:50:37 +0200
commitef3ebd82bf4ecc9fd222c13ac240e260b20eb5fe (patch)
tree2d9fe774fd2ed99b226cab8bbbedfce8b311fea1 /Lib/html/parser.py
parent2133fef94f436600760941f96b756f34a27fea4c (diff)
downloadcpython-ef3ebd82bf4ecc9fd222c13ac240e260b20eb5fe.tar.gz
Improve handling of declarations in HTMLParser.
Diffstat (limited to 'Lib/html/parser.py')
-rw-r--r--Lib/html/parser.py30
1 files changed, 22 insertions, 8 deletions
diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index a65478058f..9db8ab582b 100644
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -187,17 +187,10 @@ class HTMLParser(_markupbase.ParserBase):
elif startswith("<?", i):
k = self.parse_pi(i)
elif startswith("<!", 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)
+ k = self.parse_html_declaration(i)
elif (i + 1) < n:
self.handle_data("<")
k = i + 1
@@ -269,6 +262,27 @@ 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):