From 15cb48923449bdd1325a7736a5f9bb73c8529cec Mon Sep 17 00:00:00 2001
From: Ezio Melotti
Date: Fri, 18 Nov 2011 18:01:49 +0200
Subject: [PATCH] #13358: HTMLParser now calls handle_data only once for each
CDATA.
---
Lib/html/parser.py | 7 ++++---
Lib/test/test_htmlparser.py | 20 ++++++++++++++++++++
Misc/NEWS | 2 ++
3 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index 662e85575a4..dd9c2e14862 100644
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -14,7 +14,6 @@ import re
# Regular expressions used for parsing
interesting_normal = re.compile('[&<]')
-interesting_cdata = re.compile(r'<(/|\Z)')
incomplete = re.compile('&[a-zA-Z#]')
entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
@@ -149,8 +148,8 @@ class HTMLParser(_markupbase.ParserBase):
return self.__starttag_text
def set_cdata_mode(self, elem):
- self.interesting = interesting_cdata
self.cdata_elem = elem.lower()
+ self.interesting = re.compile(r'\s*%s\s*>' % self.cdata_elem, re.I)
def clear_cdata_mode(self):
self.interesting = interesting_normal
@@ -168,6 +167,8 @@ class HTMLParser(_markupbase.ParserBase):
if match:
j = match.start()
else:
+ if self.cdata_elem:
+ break
j = n
if i < j: self.handle_data(rawdata[i:j])
i = self.updatepos(i, j)
@@ -250,7 +251,7 @@ class HTMLParser(_markupbase.ParserBase):
else:
assert 0, "interesting.search() lied"
# end while
- if end and i < n:
+ if end and i < n and not self.cdata_elem:
self.handle_data(rawdata[i:n])
i = self.updatepos(i, n)
self.rawdata = rawdata[i:]
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
index 1ce4594a44a..87b5060611d 100644
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -301,7 +301,27 @@ DOCTYPE html [
("data", content),
("endtag", element_lower)])
+ def test_cdata_with_closing_tags(self):
+ # see issue #13358
+ # make sure that HTMLParser calls handle_data only once for each CDATA.
+ # The normal event collector normalizes the events in get_events,
+ # so we override it to return the original list of events.
+ class Collector(EventCollector):
+ def get_events(self):
+ return self.events
+ content = """ ¬-an-entity-ref;
+
+ ''"""
+ for element in [' script', 'script ', ' script ',
+ '\nscript', 'script\n', '\nscript\n']:
+ element_lower = element.lower().strip()
+ s = '