Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7903913

Browse files
committed
#670664: merge with 3.2.
2 parents 49ce068 + 7de56f6 commit 7903913

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

Doc/library/html.parser.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ An exception is defined as well:
115115

116116
.. method:: HTMLParser.handle_data(data)
117117

118-
This method is called to process arbitrary data. It is intended to be
118+
This method is called to process arbitrary data (e.g. the content of
119+
``<script>...</script>`` and ``<style>...</style>``). It is intended to be
119120
overridden by a derived class; the base class implementation does nothing.
120121

121122

Lib/html/parser.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
\s* # trailing whitespace
6363
""", re.VERBOSE)
6464
endendtag = re.compile('>')
65+
# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
66+
# </ and the tag name, so maybe this should be fixed
6567
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
6668

6769

@@ -121,6 +123,7 @@ def reset(self):
121123
self.rawdata = ''
122124
self.lasttag = '???'
123125
self.interesting = interesting_normal
126+
self.cdata_elem = None
124127
_markupbase.ParserBase.reset(self)
125128

126129
def feed(self, data):
@@ -145,11 +148,13 @@ def get_starttag_text(self):
145148
"""Return full source of start tag: '<...>'."""
146149
return self.__starttag_text
147150

148-
def set_cdata_mode(self):
151+
def set_cdata_mode(self, elem):
149152
self.interesting = interesting_cdata
153+
self.cdata_elem = elem.lower()
150154

151155
def clear_cdata_mode(self):
152156
self.interesting = interesting_normal
157+
self.cdata_elem = None
153158

154159
# Internal -- handle data as far as reasonable. May leave state
155160
# and data to be processed by a subsequent call. If 'end' is
@@ -314,7 +319,7 @@ def parse_starttag(self, i):
314319
else:
315320
self.handle_starttag(tag, attrs)
316321
if tag in self.CDATA_CONTENT_ELEMENTS:
317-
self.set_cdata_mode()
322+
self.set_cdata_mode(tag)
318323
return endpos
319324

320325
# Internal -- check to see if we have a complete starttag; return end
@@ -371,6 +376,9 @@ def parse_endtag(self, i):
371376
j = match.end()
372377
match = endtagfind.match(rawdata, i) # </ + tag + >
373378
if not match:
379+
if self.cdata_elem is not None:
380+
self.handle_data(rawdata[i:j])
381+
return j
374382
if self.strict:
375383
self.error("bad end tag: %r" % (rawdata[i:j],))
376384
k = rawdata.find('<', i + 1, j)
@@ -380,8 +388,14 @@ def parse_endtag(self, i):
380388
j = i + 1
381389
self.handle_data(rawdata[i:j])
382390
return j
383-
tag = match.group(1)
384-
self.handle_endtag(tag.lower())
391+
392+
elem = match.group(1).lower() # script or style
393+
if self.cdata_elem is not None:
394+
if elem != self.cdata_elem:
395+
self.handle_data(rawdata[i:j])
396+
return j
397+
398+
self.handle_endtag(elem.lower())
385399
self.clear_cdata_mode()
386400
return j
387401

Lib/test/test_htmlparser.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,36 @@ def test_get_starttag_text(self):
321321
("starttag_text", s)])
322322

323323
def test_cdata_content(self):
324-
s = """<script> <!-- not a comment --> &not-an-entity-ref; </script>"""
325-
self._run_check(s, [
326-
("starttag", "script", []),
327-
("data", " <!-- not a comment --> &not-an-entity-ref; "),
328-
("endtag", "script"),
329-
])
330-
s = """<script> <not a='start tag'> </script>"""
331-
self._run_check(s, [
332-
("starttag", "script", []),
333-
("data", " <not a='start tag'> "),
334-
("endtag", "script"),
335-
])
324+
contents = [
325+
'<!-- not a comment --> &not-an-entity-ref;',
326+
"<not a='start tag'>",
327+
'<a href="" /> <p> <span></span>',
328+
'foo = "</scr" + "ipt>";',
329+
'foo = "</SCRIPT" + ">";',
330+
'foo = <\n/script> ',
331+
'<!-- document.write("</scr" + "ipt>"); -->',
332+
('\n//<![CDATA[\n'
333+
'document.write(\'<s\'+\'cript type="text/javascript" '
334+
'src="http://www.example.org/r=\'+new '
335+
'Date().getTime()+\'"><\\/s\'+\'cript>\');\n//]]>'),
336+
'\n<!-- //\nvar foo = 3.14;\n// -->\n',
337+
'foo = "</sty" + "le>";',
338+
'<!-- \u2603 -->',
339+
# these two should be invalid according to the HTML 5 spec,
340+
# section 8.1.2.2
341+
#'foo = </\nscript>',
342+
#'foo = </ script>',
343+
]
344+
elements = ['script', 'style', 'SCRIPT', 'STYLE', 'Script', 'Style']
345+
for content in contents:
346+
for element in elements:
347+
element_lower = element.lower()
348+
s = '<{element}>{content}</{element}>'.format(element=element,
349+
content=content)
350+
self._run_check(s, [("starttag", element_lower, []),
351+
("data", content),
352+
("endtag", element_lower)])
353+
336354

337355
def test_entityrefs_in_attributes(self):
338356
self._run_check("<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>", [

Misc/NEWS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,13 @@ Core and Builtins
350350
Library
351351
-------
352352

353-
- Issue 10817: Fix urlretrieve function to raise ContentTooShortError even
353+
- Issue #670664: Fix HTMLParser to correctly handle the content of
354+
``<script>...</script>`` and ``<style>...</style>``.
355+
356+
- Issue #10817: Fix urlretrieve function to raise ContentTooShortError even
354357
when reporthook is None. Patch by Jyrki Pulliainen.
355358

356-
- Issue 13296: Fix IDLE to clear compile __future__ flags on shell restart.
359+
- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart.
357360
(Patch by Roger Serwy)
358361

359362
- Fix the xmlrpc.client user agent to return something similar to

0 commit comments

Comments
 (0)