summaryrefslogtreecommitdiff
path: root/Lib/html/parser.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-11-01 14:12:22 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2011-11-01 14:12:22 +0200
commite5ae086ff175bb6a12406ac7e39d127194e811fa (patch)
treecc0a84b076526852a5ce9b18704bde90bfe60103 /Lib/html/parser.py
parentb2a6ae254f04348426682239ff5a4bf5a699d5f6 (diff)
downloadcpython-e5ae086ff175bb6a12406ac7e39d127194e811fa.tar.gz
#670664: Fix HTMLParser to correctly handle the content of ``<script>...</script>`` and ``<style>...</style>``.
Diffstat (limited to 'Lib/html/parser.py')
-rw-r--r--Lib/html/parser.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index affaf7344f..afdb305d08 100644
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -62,6 +62,8 @@ locatestarttagend_tolerant = 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
+# </ and the tag name, so maybe this should be fixed
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
@@ -121,6 +123,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):
@@ -145,11 +148,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
@@ -314,7 +319,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
@@ -371,6 +376,9 @@ class HTMLParser(_markupbase.ParserBase):
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
if self.strict:
self.error("bad end tag: %r" % (rawdata[i:j],))
k = rawdata.find('<', i + 1, j)
@@ -380,8 +388,14 @@ class HTMLParser(_markupbase.ParserBase):
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