2012-07-20 22:16:57 +08:00
|
|
|
from django.utils.six.moves import html_parser as _html_parser
|
2012-02-05 18:29:08 +08:00
|
|
|
import re
|
2012-08-17 03:03:11 +08:00
|
|
|
import sys
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
current_version = sys.version_info
|
|
|
|
|
|
|
|
use_workaround = (
|
2012-09-10 02:13:42 +08:00
|
|
|
(current_version < (2, 7, 3)) or
|
2012-08-17 03:03:11 +08:00
|
|
|
(current_version >= (3, 0) and current_version < (3, 2, 3))
|
|
|
|
)
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-07-20 22:16:57 +08:00
|
|
|
HTMLParseError = _html_parser.HTMLParseError
|
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
if not use_workaround:
|
|
|
|
HTMLParser = _html_parser.HTMLParser
|
|
|
|
else:
|
|
|
|
tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*')
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
class HTMLParser(_html_parser.HTMLParser):
|
|
|
|
"""
|
|
|
|
Patched version of stdlib's HTMLParser with patch from:
|
|
|
|
http://bugs.python.org/issue670664
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
_html_parser.HTMLParser.__init__(self)
|
|
|
|
self.cdata_tag = None
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
def set_cdata_mode(self, tag):
|
|
|
|
try:
|
|
|
|
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()
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
def clear_cdata_mode(self):
|
|
|
|
self.interesting = _html_parser.interesting_normal
|
|
|
|
self.cdata_tag = None
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
# Internal -- handle starttag, return end or -1 if not terminated
|
|
|
|
def parse_starttag(self, i):
|
|
|
|
self.__starttag_text = None
|
|
|
|
endpos = self.check_for_whole_start_tag(i)
|
|
|
|
if endpos < 0:
|
|
|
|
return endpos
|
|
|
|
rawdata = self.rawdata
|
|
|
|
self.__starttag_text = rawdata[i:endpos]
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
# Now parse the data between i+1 and j into a tag and attrs
|
|
|
|
attrs = []
|
|
|
|
match = tagfind.match(rawdata, i + 1)
|
|
|
|
assert match, 'unexpected call to parse_starttag()'
|
|
|
|
k = match.end()
|
|
|
|
self.lasttag = tag = match.group(1).lower()
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
while k < endpos:
|
|
|
|
m = _html_parser.attrfind.match(rawdata, k)
|
|
|
|
if not m:
|
|
|
|
break
|
|
|
|
attrname, rest, attrvalue = m.group(1, 2, 3)
|
|
|
|
if not rest:
|
|
|
|
attrvalue = None
|
2013-09-22 20:01:57 +08:00
|
|
|
elif (attrvalue[:1] == '\'' == attrvalue[-1:] or
|
|
|
|
attrvalue[:1] == '"' == attrvalue[-1:]):
|
2012-08-17 03:03:11 +08:00
|
|
|
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")
|
2013-09-22 20:01:57 +08:00
|
|
|
offset = (len(self.__starttag_text)
|
|
|
|
- self.__starttag_text.rfind("\n"))
|
2012-08-17 03:03:11 +08:00
|
|
|
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)
|
2012-02-01 04:36:11 +08:00
|
|
|
else:
|
2012-08-17 03:03:11 +08:00
|
|
|
self.handle_starttag(tag, attrs)
|
|
|
|
if tag in self.CDATA_CONTENT_ELEMENTS:
|
2013-11-03 03:27:47 +08:00
|
|
|
self.set_cdata_mode(tag) # <--------------------------- Changed
|
2012-08-17 03:03:11 +08:00
|
|
|
return endpos
|
2012-02-01 04:36:11 +08:00
|
|
|
|
2012-08-17 03:03:11 +08:00
|
|
|
# Internal -- parse endtag, return end or -1 if incomplete
|
|
|
|
def parse_endtag(self, i):
|
|
|
|
rawdata = self.rawdata
|
|
|
|
assert rawdata[i:i + 2] == "</", "unexpected call to parse_endtag"
|
2013-11-03 03:27:47 +08:00
|
|
|
match = _html_parser.endendtag.search(rawdata, i + 1) # >
|
2012-08-17 03:03:11 +08:00
|
|
|
if not match:
|
|
|
|
return -1
|
|
|
|
j = match.end()
|
2013-11-03 03:27:47 +08:00
|
|
|
match = _html_parser.endtagfind.match(rawdata, i) # </ + tag + >
|
2012-08-17 03:03:11 +08:00
|
|
|
if not match:
|
2013-11-03 03:27:47 +08:00
|
|
|
if self.cdata_tag is not None: # *** add ***
|
|
|
|
self.handle_data(rawdata[i:j]) # *** add ***
|
|
|
|
return j # *** add ***
|
2012-08-17 03:03:11 +08:00
|
|
|
self.error("bad end tag: %r" % (rawdata[i:j],))
|
|
|
|
# --- changed start ---------------------------------------------------
|
|
|
|
tag = match.group(1).strip()
|
|
|
|
if self.cdata_tag is not None:
|
|
|
|
if tag.lower() != self.cdata_tag:
|
|
|
|
self.handle_data(rawdata[i:j])
|
|
|
|
return j
|
|
|
|
# --- changed end -----------------------------------------------------
|
|
|
|
self.handle_endtag(tag.lower())
|
|
|
|
self.clear_cdata_mode()
|
|
|
|
return j
|