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
|
From 924a19487854ba724de465afb3b03e0997f1cdb7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=88=91=E5=8F=AB=E4=BB=A5=E8=B5=8F?= <422880152@qq.com>
Date: Mon, 3 Feb 2025 11:38:41 +0800
Subject: [PATCH] fix: escape and unescape function
---
js2py/host/jsfunctions.py | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/js2py/host/jsfunctions.py b/js2py/host/jsfunctions.py
index 2360fee8..ae39999b 100644
--- a/js2py/host/jsfunctions.py
+++ b/js2py/host/jsfunctions.py
@@ -1,3 +1,4 @@
+import re
from ..base import *
from six.moves.urllib.parse import quote, unquote
@@ -148,12 +149,27 @@ def isFinite(number):
@Js
def escape(text):
- return quote(text.to_string().value)
+ def replacer(match):
+ char = match.group()
+ code = ord(char)
+ if code <= 0xff:
+ return f'%{code:02X}'
+ else:
+ return f'%u{code:04X}'
+ return re.sub(r'[^A-Za-z0-9@*_+\-./]', replacer, text.to_python())
@Js
def unescape(text):
- return unquote(text.to_string().value)
+ def replacer(match):
+ u_group = match.group(1)
+ hex_group = match.group(2)
+ if u_group is not None:
+ return chr(int(u_group, 16))
+ elif hex_group is not None:
+ return chr(int(hex_group, 16))
+ return match.group()
+ return re.sub(r'%u([0-9A-Fa-f]{4})|%([0-9A-Fa-f]{2})', replacer, text.to_python(), flags=re.IGNORECASE)
@Js
|