Mercurial > cpython-lru_cache-weakref
diff Lib/HTMLParser.py @ 73273:0a5eb57d5876 2.7
#670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 01 Nov 2011 14:09:56 +0200 |
| parents | 02a4535c0a6c |
| children | 3c3009f63700 |
line wrap: on
line diff
--- a/Lib/HTMLParser.py Tue Nov 01 10:31:26 2011 +0100 +++ b/Lib/HTMLParser.py Tue Nov 01 14:09:56 2011 +0200 @@ -43,6 +43,8 @@ \s* # trailing whitespace """, re.VERBOSE) endendtag = re.compile('>') +# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between +# </ and the tag name, so maybe this should be fixed endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>') @@ -96,6 +98,7 @@ self.rawdata = '' self.lasttag = '???' self.interesting = interesting_normal + self.cdata_elem = None markupbase.ParserBase.reset(self) def feed(self, data): @@ -120,11 +123,13 @@ """Return full source of start tag: '<...>'.""" return self.__starttag_text - def set_cdata_mode(self): + def set_cdata_mode(self, elem): self.interesting = interesting_cdata + self.cdata_elem = elem.lower() def clear_cdata_mode(self): self.interesting = interesting_normal + self.cdata_elem = None # Internal -- handle data as far as reasonable. May leave state # and data to be processed by a subsequent call. If 'end' is @@ -270,7 +275,7 @@ else: self.handle_starttag(tag, attrs) if tag in self.CDATA_CONTENT_ELEMENTS: - self.set_cdata_mode() + self.set_cdata_mode(tag) return endpos # Internal -- check to see if we have a complete starttag; return end @@ -314,9 +319,18 @@ j = 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],)) - tag = match.group(1) - self.handle_endtag(tag.lower()) + + 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_endtag(elem) self.clear_cdata_mode() return j
