diff --git a/Lib/html/parser.py b/Lib/html/parser.py --- a/Lib/html/parser.py +++ b/Lib/html/parser.py @@ -23,6 +23,9 @@ piclose = re.compile('>') commentclose = re.compile(r'--\s*>') tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*') +# 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\f />\x00]*') # Note, the strict one of this pair isn't really strict, but we can't # make it correctly strict without breaking backward compatibility. attrfind = re.compile( @@ -270,7 +273,7 @@ # 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] != '', i+2) if pos == -1: @@ -398,31 +401,40 @@ match = endendtag.search(rawdata, i+1) # > if not match: return -1 - j = match.end() + gtpos = match.end() match = endtagfind.match(rawdata, i) # if not match: if self.cdata_elem is not None: - self.handle_data(rawdata[i:j]) - return j + self.handle_data(rawdata[i:gtpos]) + return gtpos if self.strict: - self.error("bad end tag: %r" % (rawdata[i:j],)) - k = rawdata.find('<', i + 1, j) - if k > i: - j = k - if j <= i: - j = i + 1 - self.handle_data(rawdata[i:j]) - return j + self.error("bad end tag: %r" % (rawdata[i:gtpos],)) + # find the name: w3.org/TR/html5/tokenization.html#tag-name-state + namematch = tagfind_tolerant.match(rawdata, i+2) + if not namematch: + # w3.org/TR/html5/tokenization.html#end-tag-open-state + if rawdata[i:i+3] == '': + return i+3 + else: + return self.parse_bogus_comment(i) + tagname = namematch.group().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 + # most of the cases and is much simpler + gtpos = rawdata.find('>', namematch.end()) + self.handle_endtag(tagname) + return gtpos+1 elem = match.group(1).lower() # script or style if self.cdata_elem is not None: if elem != self.cdata_elem: - self.handle_data(rawdata[i:j]) - return j + self.handle_data(rawdata[i:gtpos]) + return gtpos self.handle_endtag(elem.lower()) self.clear_cdata_mode() - return j + return gtpos # Overridable -- finish processing of start+end tag: def handle_startendtag(self, tag, attrs): 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 @@ -364,8 +364,9 @@ ('data', '<'), + ('comment', '/img'), + ('endtag', 'html<')]) def test_with_unquoted_attributes(self): # see #12008 @@ -403,6 +404,43 @@ ('starttag', 'form', [('action', 'bogus|&#()value')])]) + def test_invalid_end_tags(self): + # A collection of broken end tags.
is used as separator + # These results might not make much sense, but they follow the standard: + # http://www.w3.org/TR/html5/tokenization.html#end-tag-open-state + html = ('



' + '


') + expected = [('starttag', 'br', []), + # < is part of the name, / is discarded, p is an attribute + ('endtag', 'label<'), + ('starttag', 'br', []), + # text and attributes are discarded + ('endtag', 'div'), + ('starttag', 'br', []), + # comment because the first char after is ignored + ('starttag', 'br', [])] + self._run_check(html, expected) + + def test_broken_invalid_end_tag(self): + # This is technically wrong (the "> shouldn't be included in the 'data') + # but is probably not worth fixing it (in addition to all the cases of + # the previous test, it would require a full attribute parsing). + html = 'This breaks the parser' + expected = [('starttag', 'b', []), + ('data', 'This'), + ('endtag', 'b'), + ('data', '"> breaks the parser')] + self._run_check(html, expected) + def test_correct_detection_of_start_tags(self): # see #13273 html = ('
The rain '