From 7e82b276dd5c1f786e7bd3c1554ac2017a909ab9 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Tue, 1 Nov 2011 14:09:56 +0200 Subject: [PATCH] #670664: Fix HTMLParser to correctly handle the content of ```` and ````. --- Doc/library/htmlparser.rst | 3 ++- Lib/HTMLParser.py | 22 +++++++++++++++---- Lib/test/test_htmlparser.py | 42 ++++++++++++++++++++++++++----------- Misc/NEWS | 7 +++++-- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/Doc/library/htmlparser.rst b/Doc/library/htmlparser.rst index b75b61ec7d..aa10498042 100644 --- a/Doc/library/htmlparser.rst +++ b/Doc/library/htmlparser.rst @@ -123,7 +123,8 @@ An exception is defined as well: .. method:: HTMLParser.handle_data(data) - This method is called to process arbitrary data. It is intended to be + This method is called to process arbitrary data (e.g. the content of + ```` and ````). It is intended to be overridden by a derived class; the base class implementation does nothing. diff --git a/Lib/HTMLParser.py b/Lib/HTMLParser.py index 884d2a53c5..94ebc7f8dc 100644 --- a/Lib/HTMLParser.py +++ b/Lib/HTMLParser.py @@ -43,6 +43,8 @@ locatestarttagend = re.compile(r""" \s* # trailing whitespace """, re.VERBOSE) endendtag = re.compile('>') +# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between +# ') @@ -96,6 +98,7 @@ class HTMLParser(markupbase.ParserBase): self.rawdata = '' self.lasttag = '???' self.interesting = interesting_normal + self.cdata_elem = None markupbase.ParserBase.reset(self) def feed(self, data): @@ -120,11 +123,13 @@ class HTMLParser(markupbase.ParserBase): """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 @@ class HTMLParser(markupbase.ParserBase): 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 @@ class HTMLParser(markupbase.ParserBase): j = 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.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 diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 0620d0bdc2..f557da1021 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -312,18 +312,36 @@ DOCTYPE html [ ("starttag_text", s)]) def test_cdata_content(self): - s = """""" - self._run_check(s, [ - ("starttag", "script", []), - ("data", " ¬-an-entity-ref; "), - ("endtag", "script"), - ]) - s = """""" - self._run_check(s, [ - ("starttag", "script", []), - ("data", " "), - ("endtag", "script"), - ]) + contents = [ + ' ¬-an-entity-ref;', + "", + '

', + 'foo = "";', + 'foo = "";', + 'foo = <\n/script> ', + '', + ('\n//<\\/s\'+\'cript>\');\n//]]>'), + '\n\n', + 'foo = "";', + u'', + # these two should be invalid according to the HTML 5 spec, + # section 8.1.2.2 + #'foo = ', + #'foo = ', + ] + elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style'] + for content in contents: + for element in elements: + element_lower = element.lower() + s = u'<{element}>{content}'.format(element=element, + content=content) + self._run_check(s, [("starttag", element_lower, []), + ("data", content), + ("endtag", element_lower)]) + def test_entityrefs_in_attributes(self): self._run_check("", [ diff --git a/Misc/NEWS b/Misc/NEWS index be64128776..3f1f520fe0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -74,10 +74,13 @@ Core and Builtins Library ------- -- Issue 10817: Fix urlretrieve function to raise ContentTooShortError even +- Issue #670664: Fix HTMLParser to correctly handle the content of + ```` and ````. + +- Issue #10817: Fix urlretrieve function to raise ContentTooShortError even when reporthook is None. Patch by Jyrki Pulliainen. -- Issue 13296: Fix IDLE to clear compile __future__ flags on shell restart. +- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart. (Patch by Roger Serwy) - Issue #7334: close source files on ElementTree.parse and iterparse. -- 2.50.1