changeset: 73274:a6f2244b251f branch: 3.2 parent: 73270:1d47d5fdb633 user: Ezio Melotti date: Tue Nov 01 14:12:22 2011 +0200 files: Doc/library/html.parser.rst Lib/html/parser.py Lib/test/test_htmlparser.py Misc/NEWS description: #670664: Fix HTMLParser to correctly handle the content of ```` and ````. diff -r 1d47d5fdb633 -r a6f2244b251f Doc/library/html.parser.rst --- a/Doc/library/html.parser.rst Tue Nov 01 10:32:05 2011 +0100 +++ b/Doc/library/html.parser.rst Tue Nov 01 14:12:22 2011 +0200 @@ -115,7 +115,8 @@ .. 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 -r 1d47d5fdb633 -r a6f2244b251f Lib/html/parser.py --- a/Lib/html/parser.py Tue Nov 01 10:32:05 2011 +0100 +++ b/Lib/html/parser.py Tue Nov 01 14:12:22 2011 +0200 @@ -62,6 +62,8 @@ \s* # trailing whitespace """, re.VERBOSE) endendtag = re.compile('>') +# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between +# ') @@ -121,6 +123,7 @@ self.rawdata = '' self.lasttag = '???' self.interesting = interesting_normal + self.cdata_elem = None _markupbase.ParserBase.reset(self) def feed(self, data): @@ -145,11 +148,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 @@ -314,7 +319,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 @@ -371,6 +376,9 @@ 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 if self.strict: self.error("bad end tag: %r" % (rawdata[i:j],)) k = rawdata.find('<', i + 1, j) @@ -380,8 +388,14 @@ j = i + 1 self.handle_data(rawdata[i:j]) return 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.lower()) self.clear_cdata_mode() return j diff -r 1d47d5fdb633 -r a6f2244b251f Lib/test/test_htmlparser.py --- a/Lib/test/test_htmlparser.py Tue Nov 01 10:32:05 2011 +0100 +++ b/Lib/test/test_htmlparser.py Tue Nov 01 14:12:22 2011 +0200 @@ -321,18 +321,36 @@ ("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 = "";', + '', + # 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 = '<{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 -r 1d47d5fdb633 -r a6f2244b251f Misc/NEWS --- a/Misc/NEWS Tue Nov 01 10:32:05 2011 +0100 +++ b/Misc/NEWS Tue Nov 01 14:12:22 2011 +0200 @@ -66,10 +66,13 @@ 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 #13293: Better error message when trying to marshal bytes using