Mercurial > cpython-lru_cache-weakref
diff Lib/HTMLParser.py @ 74909:a349448474ea 2.7
#13993: HTMLParser is now able to handle broken end tags.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Mon, 13 Feb 2012 16:28:54 +0200 |
| parents | 333e3acf2008 |
| children | 4743a3a1e669 |
line wrap: on
line diff
--- a/Lib/HTMLParser.py Mon Feb 13 16:10:44 2012 +0200 +++ b/Lib/HTMLParser.py Mon Feb 13 16:28:54 2012 +0200 @@ -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\r\f />\x00]*') attrfind = re.compile( r'\s*((?<=[\'"\s])[^\s/>][^\s/=>]*)(\s*=+\s*' @@ -243,7 +246,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] != '<!': + if rawdata[i:i+2] not in ('<!', '</'): self.error('unexpected call to parse_comment()') pos = rawdata.find('>', i+2) if pos == -1: @@ -353,23 +356,38 @@ match = endendtag.search(rawdata, i+1) # > if not match: return -1 - j = match.end() + gtpos = match.end() match = endtagfind.match(rawdata, i) # </ + tag + > if not match: if self.cdata_elem is not None: - self.handle_data(rawdata[i:j]) - return j - self.error("bad end tag: %r" % (rawdata[i:j],)) + self.handle_data(rawdata[i:gtpos]) + return 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 + # </tag attr=">">, 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) self.clear_cdata_mode() - return j + return gtpos # Overridable -- finish processing of start+end tag: <tag.../> def handle_startendtag(self, tag, attrs):
