#19480: merge with 3.3.

This commit is contained in:
Ezio Melotti 2013-11-07 18:35:27 +02:00
commit b7038817fe
3 changed files with 28 additions and 13 deletions

View File

@ -25,16 +25,16 @@ charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
starttagopen = re.compile('<[a-zA-Z]') starttagopen = re.compile('<[a-zA-Z]')
piclose = re.compile('>') piclose = re.compile('>')
commentclose = re.compile(r'--\s*>') commentclose = re.compile(r'--\s*>')
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: # Note:
# 1) the strict attrfind isn't really strict, but we can't make it # 1) the strict attrfind isn't really strict, but we can't make it
# correctly strict without breaking backward compatibility; # correctly strict without breaking backward compatibility;
# 2) if you change attrfind remember to update locatestarttagend too; # 2) if you change tagfind/attrfind remember to update locatestarttagend too;
# 3) if you change attrfind and/or locatestarttagend the parser will # 3) if you change tagfind/attrfind and/or locatestarttagend the parser will
# explode, so don't do it. # 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]*)(?:\s|/(?!>))*')
attrfind = re.compile( attrfind = re.compile(
r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*' r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?') r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?')
@ -56,7 +56,7 @@ locatestarttagend = re.compile(r"""
\s* # trailing whitespace \s* # trailing whitespace
""", re.VERBOSE) """, re.VERBOSE)
locatestarttagend_tolerant = re.compile(r""" 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/]* # optional whitespace before attribute name
(?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name (?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
(?:\s*=+\s* # value indicator (?:\s*=+\s* # value indicator
@ -336,7 +336,10 @@ class HTMLParser(_markupbase.ParserBase):
# Now parse the data between i+1 and j into a tag and attrs # Now parse the data between i+1 and j into a tag and attrs
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()' assert match, 'unexpected call to parse_starttag()'
k = match.end() k = match.end()
self.lasttag = tag = match.group(1).lower() self.lasttag = tag = match.group(1).lower()
@ -448,7 +451,7 @@ class HTMLParser(_markupbase.ParserBase):
return i+3 return i+3
else: else:
return self.parse_bogus_comment(i) 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 > # consume and ignore other stuff between the name and the >
# Note: this is not 100% correct, since we might have things like # Note: this is not 100% correct, since we might have things like
# </tag attr=">">, but looking for > after tha name should cover # </tag attr=">">, but looking for > after tha name should cover

View File

@ -231,6 +231,11 @@ text
self._parse_error("<a foo='bar") self._parse_error("<a foo='bar")
self._parse_error("<a foo='>'") self._parse_error("<a foo='>'")
self._parse_error("<a foo='>") self._parse_error("<a foo='>")
self._parse_error("<a$>")
self._parse_error("<a$b>")
self._parse_error("<a$b/>")
self._parse_error("<a$b >")
self._parse_error("<a$b />")
def test_valid_doctypes(self): def test_valid_doctypes(self):
# from http://www.w3.org/QA/2002/04/valid-dtd-list.html # from http://www.w3.org/QA/2002/04/valid-dtd-list.html
@ -379,8 +384,8 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
('starttag', 'html', [('<html', None)]), ('starttag', 'html', [('<html', None)]),
('data', 'te>>xt'), ('data', 'te>>xt'),
('entityref', 'a'), ('entityref', 'a'),
('data', '<<bc'), ('data', '<'),
('endtag', 'a'), ('starttag', 'bc<', [('a', None)]),
('endtag', 'html'), ('endtag', 'html'),
('data', '\n<img src="URL>'), ('data', '\n<img src="URL>'),
('comment', '/img'), ('comment', '/img'),
@ -391,8 +396,7 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
self._run_check("</$>", [('comment', '$')]) self._run_check("</$>", [('comment', '$')])
self._run_check("</", [('data', '</')]) self._run_check("</", [('data', '</')])
self._run_check("</a", [('data', '</a')]) self._run_check("</a", [('data', '</a')])
# XXX this might be wrong self._run_check("<a<a>", [('starttag', 'a<a', [])])
self._run_check("<a<a>", [('data', '<a'), ('starttag', 'a', [])])
self._run_check("</a<a>", [('endtag', 'a<a')]) self._run_check("</a<a>", [('endtag', 'a<a')])
self._run_check("<!", [('data', '<!')]) self._run_check("<!", [('data', '<!')])
self._run_check("<a", [('data', '<a')]) self._run_check("<a", [('data', '<a')])
@ -400,6 +404,11 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
self._run_check("<a foo='bar", [('data', "<a foo='bar")]) self._run_check("<a foo='bar", [('data', "<a foo='bar")])
self._run_check("<a foo='>'", [('data', "<a foo='>'")]) self._run_check("<a foo='>'", [('data', "<a foo='>'")])
self._run_check("<a foo='>", [('data', "<a foo='>")]) self._run_check("<a foo='>", [('data', "<a foo='>")])
self._run_check("<a$>", [('starttag', 'a$', [])])
self._run_check("<a$b>", [('starttag', 'a$b', [])])
self._run_check("<a$b/>", [('startendtag', 'a$b', [])])
self._run_check("<a$b >", [('starttag', 'a$b', [])])
self._run_check("<a$b />", [('startendtag', 'a$b', [])])
def test_slashes_in_starttag(self): def test_slashes_in_starttag(self):
self._run_check('<a foo="var"/>', [('startendtag', 'a', [('foo', 'var')])]) self._run_check('<a foo="var"/>', [('startendtag', 'a', [('foo', 'var')])])

View File

@ -64,6 +64,9 @@ Library
- Issue #19286: Directories in ``package_data`` are no longer added to - Issue #19286: Directories in ``package_data`` are no longer added to
the filelist, preventing failure outlined in the ticket. the filelist, preventing failure outlined in the ticket.
- Issue #19480: HTMLParser now accepts all valid start-tag names as defined
by the HTML5 standard.
- Issue #15114: The html.parser module now raises a DeprecationWarning when the - Issue #15114: The html.parser module now raises a DeprecationWarning when the
strict argument of HTMLParser or the HTMLParser.error method are used. strict argument of HTMLParser or the HTMLParser.error method are used.