diff --git a/Lib/html/parser.py b/Lib/html/parser.py
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -23,16 +23,16 @@
starttagopen = re.compile('<[a-zA-Z]')
piclose = re.compile('>')
commentclose = re.compile(r'--\s*>')
+# Note:
+# 1) the strict attrfind isn't really strict, but we can't make it
+# correctly strict without breaking backward compatibility;
+# 2) if you change tagfind/attrfind remember to update locatestarttagend too;
+# 3) if you change tagfind/attrfind and/or locatestarttagend the parser will
+# explode, so don't do it.
tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*')
# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
-tagfind_tolerant = re.compile('[a-zA-Z][^\t\n\r\f />\x00]*')
-# Note:
-# 1) the strict attrfind isn't really strict, but we can't make it
-# correctly strict without breaking backward compatibility;
-# 2) if you change attrfind remember to update locatestarttagend too;
-# 3) if you change attrfind and/or locatestarttagend the parser will
-# explode, so don't do it.
+tagfind_tolerant = re.compile('([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
attrfind = re.compile(
r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?')
@@ -54,7 +54,7 @@
\s* # trailing whitespace
""", re.VERBOSE)
locatestarttagend_tolerant = re.compile(r"""
- <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
+ <[a-zA-Z][^\t\n\r\f />\x00]* # tag name
(?:[\s/]* # optional whitespace before attribute name
(?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
(?:\s*=+\s* # value indicator
@@ -328,7 +328,10 @@
# Now parse the data between i+1 and j into a tag and attrs
attrs = []
- match = tagfind.match(rawdata, i+1)
+ if self.strict:
+ match = tagfind.match(rawdata, i+1)
+ else:
+ match = tagfind_tolerant.match(rawdata, i+1)
assert match, 'unexpected call to parse_starttag()'
k = match.end()
self.lasttag = tag = match.group(1).lower()
@@ -440,7 +443,7 @@
return i+3
else:
return self.parse_bogus_comment(i)
- tagname = namematch.group().lower()
+ tagname = namematch.group(1).lower()
# consume and ignore other stuff between the name and the >
# Note: this is not 100% correct, since we might have things like
# ">, but looking for > after tha name should cover
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -229,6 +229,11 @@
self._parse_error("'")
self._parse_error(">xt'),
('entityref', 'a'),
- ('data', '<", [('comment', '$')])
self._run_check("", [('data', '')])
self._run_check("", [('data', '", [('starttag', 'a", [('endtag', 'a'", [('data', "", [('starttag', 'a$b', [])])
+ self._run_check("", [('startendtag', 'a$b', [])])
+ self._run_check("", [('starttag', 'a$b', [])])
+ self._run_check("", [('startendtag', 'a$b', [])])
def test_slashes_in_starttag(self):
self._run_check('', [('startendtag', 'a', [('foo', 'var')])])