| 1 |
# mako/filters.py |
| 2 |
# Copyright 2006-2022 the Mako authors and contributors <see AUTHORS file> |
| 3 |
# |
| 4 |
# This module is part of Mako and is released under |
| 5 |
# the MIT License: http://www.opensource.org/licenses/mit-license.php |
| 6 |
|
| 7 |
|
| 8 |
import codecs |
| 9 |
from html.entities import codepoint2name |
| 10 |
from html.entities import name2codepoint |
| 11 |
import re |
| 12 |
from urllib.parse import quote_plus |
| 13 |
|
| 14 |
import markupsafe |
| 15 |
|
| 16 |
html_escape = markupsafe.escape |
| 17 |
|
| 18 |
xml_escapes = { |
| 19 |
"&": "&", |
| 20 |
">": ">", |
| 21 |
"<": "<", |
| 22 |
'"': """, # also " in html-only |
| 23 |
"'": "'", # also ' in html-only |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
def xml_escape(string): |
| 28 |
return re.sub(r'([&<"\'>])', lambda m: xml_escapes[m.group()], string) |
| 29 |
|
| 30 |
|
| 31 |
def url_escape(string): |
| 32 |
# convert into a list of octets |
| 33 |
string = string.encode("utf8") |
| 34 |
return quote_plus(string) |
| 35 |
|
| 36 |
|
| 37 |
def trim(string): |
| 38 |
return string.strip() |
| 39 |
|
| 40 |
|
| 41 |
class Decode: |
| 42 |
def __getattr__(self, key): |
| 43 |
def decode(x): |
| 44 |
if isinstance(x, str): |
| 45 |
return x |
| 46 |
elif not isinstance(x, bytes): |
| 47 |
return decode(str(x)) |
| 48 |
else: |
| 49 |
return str(x, encoding=key) |
| 50 |
|
| 51 |
return decode |
| 52 |
|
| 53 |
|
| 54 |
decode = Decode() |
| 55 |
|
| 56 |
|
| 57 |
class XMLEntityEscaper: |
| 58 |
def __init__(self, codepoint2name, name2codepoint): |
| 59 |
self.codepoint2entity = { |
| 60 |
c: str("&%s;" % n) for c, n in codepoint2name.items() |
| 61 |
} |
| 62 |
self.name2codepoint = name2codepoint |
| 63 |
|
| 64 |
def escape_entities(self, text): |
| 65 |
"""Replace characters with their character entity references. |
| 66 |
|
| 67 |
Only characters corresponding to a named entity are replaced. |
| 68 |
""" |
| 69 |
return str(text).translate(self.codepoint2entity) |
| 70 |
|
| 71 |
def __escape(self, m): |
| 72 |
codepoint = ord(m.group()) |
| 73 |
try: |
| 74 |
return self.codepoint2entity[codepoint] |
| 75 |
except (KeyError, IndexError): |
| 76 |
return "&#x%X;" % codepoint |
| 77 |
|
| 78 |
__escapable = re.compile(r'["&<>]|[^\x00-\x7f]') |
| 79 |
|
| 80 |
def escape(self, text): |
| 81 |
"""Replace characters with their character references. |
| 82 |
|
| 83 |
Replace characters by their named entity references. |
| 84 |
Non-ASCII characters, if they do not have a named entity reference, |
| 85 |
are replaced by numerical character references. |
| 86 |
|
| 87 |
The return value is guaranteed to be ASCII. |
| 88 |
""" |
| 89 |
return self.__escapable.sub(self.__escape, str(text)).encode("ascii") |
| 90 |
|
| 91 |
# XXX: This regexp will not match all valid XML entity names__. |
| 92 |
# (It punts on details involving involving CombiningChars and Extenders.) |
| 93 |
# |
| 94 |
# .. __: http://www.w3.org/TR/2000/REC-xml-20001006#NT-EntityRef |
| 95 |
__characterrefs = re.compile( |
| 96 |
r"""& (?: |
| 97 |
\#(\d+) |
| 98 |
| \#x([\da-f]+) |
| 99 |
| ( (?!\d) [:\w] [-.:\w]+ ) |
| 100 |
) ;""", |
| 101 |
re.X | re.UNICODE, |
| 102 |
) |
| 103 |
|
| 104 |
def __unescape(self, m): |
| 105 |
dval, hval, name = m.groups() |
| 106 |
if dval: |
| 107 |
codepoint = int(dval) |
| 108 |
elif hval: |
| 109 |
codepoint = int(hval, 16) |
| 110 |
else: |
| 111 |
codepoint = self.name2codepoint.get(name, 0xFFFD) |
| 112 |
# U+FFFD = "REPLACEMENT CHARACTER" |
| 113 |
if codepoint < 128: |
| 114 |
return chr(codepoint) |
| 115 |
return chr(codepoint) |
| 116 |
|
| 117 |
def unescape(self, text): |
| 118 |
"""Unescape character references. |
| 119 |
|
| 120 |
All character references (both entity references and numerical |
| 121 |
character references) are unescaped. |
| 122 |
""" |
| 123 |
return self.__characterrefs.sub(self.__unescape, text) |
| 124 |
|
| 125 |
|
| 126 |
_html_entities_escaper = XMLEntityEscaper(codepoint2name, name2codepoint) |
| 127 |
|
| 128 |
html_entities_escape = _html_entities_escaper.escape_entities |
| 129 |
html_entities_unescape = _html_entities_escaper.unescape |
| 130 |
|
| 131 |
|
| 132 |
def htmlentityreplace_errors(ex): |
| 133 |
"""An encoding error handler. |
| 134 |
|
| 135 |
This python codecs error handler replaces unencodable |
| 136 |
characters with HTML entities, or, if no HTML entity exists for |
| 137 |
the character, XML character references:: |
| 138 |
|
| 139 |
>>> 'The cost was \u20ac12.'.encode('latin1', 'htmlentityreplace') |
| 140 |
'The cost was €12.' |
| 141 |
""" |
| 142 |
if isinstance(ex, UnicodeEncodeError): |
| 143 |
# Handle encoding errors |
| 144 |
bad_text = ex.object[ex.start : ex.end] |
| 145 |
text = _html_entities_escaper.escape(bad_text) |
| 146 |
return (str(text), ex.end) |
| 147 |
raise ex |
| 148 |
|
| 149 |
|
| 150 |
codecs.register_error("htmlentityreplace", htmlentityreplace_errors) |
| 151 |
|
| 152 |
|
| 153 |
DEFAULT_ESCAPES = { |
| 154 |
"x": "filters.xml_escape", |
| 155 |
"h": "filters.html_escape", |
| 156 |
"u": "filters.url_escape", |
| 157 |
"trim": "filters.trim", |
| 158 |
"entity": "filters.html_entities_escape", |
| 159 |
"unicode": "str", |
| 160 |
"decode": "decode", |
| 161 |
"str": "str", |
| 162 |
"n": "n", |
| 163 |
} |