FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

#670664: Fix HTMLParser to correctly handle the content of ``<script>… · python-emscripten/cpython@7e82b27 · GitHub

Repository navigation

Commit 7e82b27

Browse files
committed
#670664: Fix HTMLParser to correctly handle the content of <script>...</script> and <style>...</style>.
1 parent 1f3b84f commit 7e82b27

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

‎Doc/library/htmlparser.rst‎

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

124124
.. method:: HTMLParser.handle_data(data)
125125

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

129130

‎Lib/HTMLParser.py‎

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

4850

@@ -96,6 +98,7 @@ def reset(self):
9698
self.rawdata = ''
9799
self.lasttag = '???'
98100
self.interesting = interesting_normal
101+
self.cdata_elem = None
99102
markupbase.ParserBase.reset(self)
100103

101104
def feed(self, data):
@@ -120,11 +123,13 @@ def get_starttag_text(self):
120123
"""Return full source of start tag: '<...>'."""
121124
return self.__starttag_text
122125

123-
def set_cdata_mode(self):
126+
def set_cdata_mode(self, elem):
124127
self.interesting = interesting_cdata
128+
self.cdata_elem = elem.lower()
125129

126130
def clear_cdata_mode(self):
127131
self.interesting = interesting_normal
132+
self.cdata_elem = None
128133

129134
# Internal -- handle data as far as reasonable. May leave state
130135
# and data to be processed by a subsequent call. If 'end' is
@@ -270,7 +275,7 @@ def parse_starttag(self, i):
270275
else:
271276
self.handle_starttag(tag, attrs)
272277
if tag in self.CDATA_CONTENT_ELEMENTS:
273-
self.set_cdata_mode()
278+
self.set_cdata_mode(tag)
274279
return endpos
275280

276281
# Internal -- check to see if we have a complete starttag; return end
@@ -314,9 +319,18 @@ def parse_endtag(self, i):
314319
j = match.end()
315320
match = endtagfind.match(rawdata, i) # </ + tag + >
316321
if not match:
322+
if self.cdata_elem is not None:
323+
self.handle_data(rawdata[i:j])
324+
return j
317325
self.error("bad end tag: %r" % (rawdata[i:j],))
318-
tag = match.group(1)
319-
self.handle_endtag(tag.lower())
326+
327+
elem = match.group(1).lower() # script or style
328+
if self.cdata_elem is not None:
329+
if elem != self.cdata_elem:
330+
self.handle_data(rawdata[i:j])
331+
return j
332+
333+
self.handle_endtag(elem)
320334
self.clear_cdata_mode()
321335
return j
322336

‎Lib/test/test_htmlparser.py‎

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

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

328346
def test_entityrefs_in_attributes(self):
329347
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
@@ -74,10 +74,13 @@ Core and Builtins
7474
Library
7575
-------
7676

77-
- Issue 10817: Fix urlretrieve function to raise ContentTooShortError even
77+
- Issue #670664: Fix HTMLParser to correctly handle the content of
78+
``<script>...</script>`` and ``<style>...</style>``.
79+
80+
- Issue #10817: Fix urlretrieve function to raise ContentTooShortError even
7881
when reporthook is None. Patch by Jyrki Pulliainen.
7982

80-
- Issue 13296: Fix IDLE to clear compile __future__ flags on shell restart.
83+
- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart.
8184
(Patch by Roger Serwy)
8285

8386
- Issue #7334: close source files on ElementTree.parse and iterparse.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL