-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.py
More file actions
50 lines (43 loc) · 2.09 KB
/
Copy pathhtml.py
File metadata and controls
50 lines (43 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@namespace("html")
# A small, opt-in subset of Python's html module: `escape`/`unescape` for
# the five entities CPython's own `html.escape` produces (`&`, `<`,
# `>`, `"`, `'`) plus `unescape`'s common aliases for the
# apostrophe (`'`, `'`) — not the full HTML5 named-character-
# reference table (2000+ entries) or numeric character references
# (`&#NNN;`/`&#xHHH;` for arbitrary code points), which would need an
# int-codepoint-to-`str` conversion this codebase has never established
# (a different question from the confirmed `int`→`str` *decimal* text
# conversion `"" + value` gives — see `string.py`'s notes).
#
# Built on the already-confirmed per-target `.Replace`/`.replace`/
# `.stringByReplacingOccurrencesOfString` (see `string.py`'s notes) — all
# three replace *every* occurrence by default, so no manual loop is
# needed. Order matters: `escape` replaces `&` first (so the `&` it
# introduces via `<`/`>`/etc. isn't re-escaped); `unescape` replaces
# `&` last (so `&lt;` — an already-escaped literal `<` — comes
# back as `<` text, not `<`).
def _replace(value: str, oldValue: str, newValue: str) -> str:
if defined("ECHOES") or defined("ISLAND"):
return value.Replace(oldValue, newValue)
elif defined("COOPER"):
return value.replace(oldValue, newValue)
else:
return value.stringByReplacingOccurrencesOfString(oldValue, withString=newValue)
def escape(value: str) -> str:
result: str = value
result = _replace(result, "&", "&")
result = _replace(result, "<", "<")
result = _replace(result, ">", ">")
result = _replace(result, "\"", """)
result = _replace(result, "'", "'")
return result
def unescape(value: str) -> str:
result: str = value
result = _replace(result, """, "\"")
result = _replace(result, "'", "'")
result = _replace(result, "'", "'")
result = _replace(result, "'", "'")
result = _replace(result, "<", "<")
result = _replace(result, ">", ">")
result = _replace(result, "&", "&")
return result