|
1 | | -from __future__ import absolute_import, unicode_literals |
| 1 | +from __future__ import absolute_import |
2 | 2 | from future.utils import PY3 |
3 | 3 | __future_module__ = True |
4 | 4 |
|
5 | 5 | if PY3: |
6 | 6 | from html import * |
7 | 7 | else: |
8 | | - |
9 | 8 | # cgi.escape isn't good enough for the single Py3.3 html test to pass. |
10 | | - # Define it inline here instead. From the Py3.3 stdlib |
| 9 | + # Define it inline here instead. From the Py3.4 stdlib. Note that the |
| 10 | + # html.escape() function from the Py3.3 stdlib is not suitable for use on |
| 11 | + # Py2.x. |
11 | 12 | """ |
12 | 13 | General functions for HTML manipulation. |
13 | 14 | """ |
14 | 15 |
|
15 | | - |
16 | | - _escape_map = {ord('&'): '&', ord('<'): '<', ord('>'): '>'} |
17 | | - _escape_map_full = {ord('&'): '&', ord('<'): '<', ord('>'): '>', |
18 | | - ord('"'): '"', ord('\''): '''} |
19 | | - |
20 | | - # NB: this is a candidate for a bytes/string polymorphic interface |
21 | | - |
22 | 16 | def escape(s, quote=True): |
23 | 17 | """ |
24 | 18 | Replace special characters "&", "<" and ">" to HTML-safe sequences. |
25 | 19 | If the optional flag quote is true (the default), the quotation mark |
26 | 20 | characters, both double quote (") and single quote (') characters are also |
27 | 21 | translated. |
28 | 22 | """ |
| 23 | + s = s.replace("&", "&") # Must be done first! |
| 24 | + s = s.replace("<", "<") |
| 25 | + s = s.replace(">", ">") |
29 | 26 | if quote: |
30 | | - return s.translate(_escape_map_full) |
31 | | - return s.translate(_escape_map) |
| 27 | + s = s.replace('"', """) |
| 28 | + s = s.replace('\'', "'") |
| 29 | + return s |
| 30 | + |
| 31 | + __all__ = ['escape'] |
0 commit comments