我已经实现了Jint - C#中的javascript引擎。问题是它不支持已删除的unescape
/ escape
函数,并且替代方法不会返回完全相同的结果。与此实现相同:http://cwestblog.com/2011/05/23/escape-unescape-deprecated/
答案 0 :(得分:2)
由于没有人回复我必须自己从Ecma规范中编写两个已弃用的encode
和decode
函数。 Standard ECMA-262
5.1 Edition / June 2011。很抱歉,如果这些内容写得不够强大,我不是javascript开发人员。
var escape = function (s) {
s = s.toString(s);
var n = s.length;
var result = "";
for(var k = 0; k < n; k++) {
var c = s[k];
var S = "";
var code = c.charCodeAt(0);
if(("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./").indexOf(c) > -1) {
S = c;
}
else if(code < 256) {
S = '%' + code.toString(16);
}
else {
S = code.toString(16);
S = "%u" + ("0000").substring(0, 4 - S.length) + S;
}
result = result + S;
}
return result;};
var unescape = function (s) {
s = s.toString(s);
var n = s.length;
var result = "";
for(var k = 0; k < n; k++) {
var c = s[k];
if(c == '%') {
if(k <= (n - 6)) {
if(s[k + 1] == 'u') {
if(("0123456789abcdef").indexOf(s[k + 2]) > -1 &&
("0123456789abcdef").indexOf(s[k + 3]) > -1 &&
("0123456789abcdef").indexOf(s[k + 4]) > -1 &&
("0123456789abcdef").indexOf(s[k + 5]) > -1) {
c = String.fromCharCode(parseInt(s.substring(k + 2, k + 7), 16));
k = k + 5;
}
else {
if(k <= (n - 3) &&
("0123456789abcdef").indexOf(s[k + 1]) > -1 &&
("0123456789abcdef").indexOf(s[k + 2]) > -1)
{
c = String.fromCharCode(parseInt(("00" + s.substring(k + 1, k + 3)), 16));
k = k + 2;
}
}
}
else {
if(k <= (n - 3) &&
("0123456789abcdef").indexOf(s[k + 1]) > -1 &&
("0123456789abcdef").indexOf(s[k + 2]) > -1) {
c = String.fromCharCode(parseInt(("00" + s.substring(k + 1, k + 3)), 16));
k = k + 2;
}
}
}
else if(("0123456789abcdef").indexOf(s[k + 1]) > -1 &&
("0123456789abcdef").indexOf(s[k + 2]) > -1) {
c = String.fromCharCode(parseInt(("00" + s.substring(k + 1, k + 3)), 16));
k = k + 2;
}
}
result = result + c;
}
return result;};