Fixed #18239 -- Subclassed HTMLParser only for selected Python versions

Only Python versions affected by http://bugs.python.org/issue670664
should patch HTMLParser.
Thanks Raphaël Hertzog for the initial patch (for 1.4).
This commit is contained in:
Claude Paroz 2012-08-16 21:03:11 +02:00
parent 37a894b48c
commit 5c79dd5865
1 changed files with 100 additions and 88 deletions

View File

@ -1,102 +1,114 @@
from django.utils.six.moves import html_parser as _html_parser from django.utils.six.moves import html_parser as _html_parser
import re import re
import sys
tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*') current_version = sys.version_info
use_workaround = (
(current_version < (2, 6, 8)) or
(current_version >= (2, 7) and current_version < (2, 7, 3)) or
(current_version >= (3, 0) and current_version < (3, 2, 3))
)
HTMLParseError = _html_parser.HTMLParseError HTMLParseError = _html_parser.HTMLParseError
class HTMLParser(_html_parser.HTMLParser): if not use_workaround:
""" HTMLParser = _html_parser.HTMLParser
Patched version of stdlib's HTMLParser with patch from: else:
http://bugs.python.org/issue670664 tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*')
"""
def __init__(self):
_html_parser.HTMLParser.__init__(self)
self.cdata_tag = None
def set_cdata_mode(self, tag): class HTMLParser(_html_parser.HTMLParser):
try: """
self.interesting = _html_parser.interesting_cdata Patched version of stdlib's HTMLParser with patch from:
except AttributeError: http://bugs.python.org/issue670664
self.interesting = re.compile(r'</\s*%s\s*>' % tag.lower(), re.I) """
self.cdata_tag = tag.lower() def __init__(self):
_html_parser.HTMLParser.__init__(self)
self.cdata_tag = None
def clear_cdata_mode(self): def set_cdata_mode(self, tag):
self.interesting = _html_parser.interesting_normal try:
self.cdata_tag = None self.interesting = _html_parser.interesting_cdata
except AttributeError:
self.interesting = re.compile(r'</\s*%s\s*>' % tag.lower(), re.I)
self.cdata_tag = tag.lower()
# Internal -- handle starttag, return end or -1 if not terminated def clear_cdata_mode(self):
def parse_starttag(self, i): self.interesting = _html_parser.interesting_normal
self.__starttag_text = None self.cdata_tag = None
endpos = self.check_for_whole_start_tag(i)
if endpos < 0:
return endpos
rawdata = self.rawdata
self.__starttag_text = rawdata[i:endpos]
# Now parse the data between i+1 and j into a tag and attrs # Internal -- handle starttag, return end or -1 if not terminated
attrs = [] def parse_starttag(self, i):
match = tagfind.match(rawdata, i + 1) self.__starttag_text = None
assert match, 'unexpected call to parse_starttag()' endpos = self.check_for_whole_start_tag(i)
k = match.end() if endpos < 0:
self.lasttag = tag = match.group(1).lower() return endpos
rawdata = self.rawdata
self.__starttag_text = rawdata[i:endpos]
while k < endpos: # Now parse the data between i+1 and j into a tag and attrs
m = _html_parser.attrfind.match(rawdata, k) attrs = []
if not m: match = tagfind.match(rawdata, i + 1)
break assert match, 'unexpected call to parse_starttag()'
attrname, rest, attrvalue = m.group(1, 2, 3) k = match.end()
if not rest: self.lasttag = tag = match.group(1).lower()
attrvalue = None
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
attrvalue[:1] == '"' == attrvalue[-1:]:
attrvalue = attrvalue[1:-1]
if attrvalue:
attrvalue = self.unescape(attrvalue)
attrs.append((attrname.lower(), attrvalue))
k = m.end()
end = rawdata[k:endpos].strip() while k < endpos:
if end not in (">", "/>"): m = _html_parser.attrfind.match(rawdata, k)
lineno, offset = self.getpos() if not m:
if "\n" in self.__starttag_text: break
lineno = lineno + self.__starttag_text.count("\n") attrname, rest, attrvalue = m.group(1, 2, 3)
offset = len(self.__starttag_text) \ if not rest:
- self.__starttag_text.rfind("\n") attrvalue = None
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
attrvalue[:1] == '"' == attrvalue[-1:]:
attrvalue = attrvalue[1:-1]
if attrvalue:
attrvalue = self.unescape(attrvalue)
attrs.append((attrname.lower(), attrvalue))
k = m.end()
end = rawdata[k:endpos].strip()
if end not in (">", "/>"):
lineno, offset = self.getpos()
if "\n" in self.__starttag_text:
lineno = lineno + self.__starttag_text.count("\n")
offset = len(self.__starttag_text) \
- self.__starttag_text.rfind("\n")
else:
offset = offset + len(self.__starttag_text)
self.error("junk characters in start tag: %r"
% (rawdata[k:endpos][:20],))
if end.endswith('/>'):
# XHTML-style empty tag: <span attr="value" />
self.handle_startendtag(tag, attrs)
else: else:
offset = offset + len(self.__starttag_text) self.handle_starttag(tag, attrs)
self.error("junk characters in start tag: %r" if tag in self.CDATA_CONTENT_ELEMENTS:
% (rawdata[k:endpos][:20],)) self.set_cdata_mode(tag) # <--------------------------- Changed
if end.endswith('/>'): return endpos
# XHTML-style empty tag: <span attr="value" />
self.handle_startendtag(tag, attrs)
else:
self.handle_starttag(tag, attrs)
if tag in self.CDATA_CONTENT_ELEMENTS:
self.set_cdata_mode(tag) # <--------------------------- Changed
return endpos
# Internal -- parse endtag, return end or -1 if incomplete # Internal -- parse endtag, return end or -1 if incomplete
def parse_endtag(self, i): def parse_endtag(self, i):
rawdata = self.rawdata rawdata = self.rawdata
assert rawdata[i:i + 2] == "</", "unexpected call to parse_endtag" assert rawdata[i:i + 2] == "</", "unexpected call to parse_endtag"
match = _html_parser.endendtag.search(rawdata, i + 1) # > match = _html_parser.endendtag.search(rawdata, i + 1) # >
if not match: if not match:
return -1 return -1
j = match.end() j = match.end()
match = _html_parser.endtagfind.match(rawdata, i) # </ + tag + > match = _html_parser.endtagfind.match(rawdata, i) # </ + tag + >
if not match: if not match:
if self.cdata_tag is not None: # *** add *** if self.cdata_tag is not None: # *** add ***
self.handle_data(rawdata[i:j]) # *** add *** self.handle_data(rawdata[i:j]) # *** add ***
return j # *** add *** return j # *** add ***
self.error("bad end tag: %r" % (rawdata[i:j],)) self.error("bad end tag: %r" % (rawdata[i:j],))
# --- changed start --------------------------------------------------- # --- changed start ---------------------------------------------------
tag = match.group(1).strip() tag = match.group(1).strip()
if self.cdata_tag is not None: if self.cdata_tag is not None:
if tag.lower() != self.cdata_tag: if tag.lower() != self.cdata_tag:
self.handle_data(rawdata[i:j]) self.handle_data(rawdata[i:j])
return j return j
# --- changed end ----------------------------------------------------- # --- changed end -----------------------------------------------------
self.handle_endtag(tag.lower()) self.handle_endtag(tag.lower())
self.clear_cdata_mode() self.clear_cdata_mode()
return j return j