fix a bug in Ox.decodeUTF8; move Ox.decodeURI/Ox.decodeURIComponent to encoding module

This commit is contained in:
rlx 2013-08-27 08:56:07 +00:00
parent 0c76680864
commit 0f5d475c78
2 changed files with 42 additions and 42 deletions

View file

@ -241,6 +241,47 @@ Ox.decodeDeflate = function(string, callback) {
image.src = 'data:image/png;base64,' + btoa(data);
};
(function() {
function replace(string) {
return string.replace(/%(?![0-9A-Fa-f]{2})/g, '%25')
.replace(/(%[0-9A-Fa-f]{2})+/g, function(match) {
var hex = match.split('%').slice(1), ret;
Ox.forEach(Ox.range(1, hex.length + 1), function(length) {
var string = Ox.range(length).map(function(i) {
return Ox.char(parseInt(hex[i], 16));
}).join('');
try {
Ox.decodeUTF8(string);
ret = match.slice(0, length * 3)
+ match.slice(length * 3).replace(/%/g, '%25');
return false;
} catch(e) {}
});
return ret || '%25' + hex[0] + replace(match.slice(3));
});
}
/*@
Ox.decodeURI <f> Decodes URI
Unlike window.decodeURI, this doesn't throw on trailing '%'.
(string) -> <s> Decoded string
@*/
Ox.decodeURI = function(string) {
return decodeURI(replace(string));
};
/*@
Ox.decodeURIComponent <f> Decodes URI component
Unlike window.decodeURIComponent, this doesn't throw on trailing '%'.
(string) -> <s> Decoded string
@*/
Ox.decodeURIComponent = function(string) {
return decodeURIComponent(replace(string));
};
}());
/*@
Ox.encodeUTF8 <f> Encodes a string as UTF-8
see http://en.wikipedia.org/wiki/UTF-8
@ -293,7 +334,7 @@ Ox.decodeUTF8 = function(string) {
string.charCodeAt(i + 1),
string.charCodeAt(i + 2)
];
if (code[0] <= 128) {
if (code[0] < 128) {
ret += string[i];
i++;
} else if (

View file

@ -24,47 +24,6 @@ Ox.clean = function(string) {
})).join('\n');
};
(function() {
function replace(string) {
return string.replace(/%(?![0-9A-Fa-f]{2})/g, '%25')
.replace(/(%[0-9A-Fa-f]{2})+/g, function(match) {
var hex = match.split('%').slice(1), ret;
Ox.forEach(Ox.range(1, hex.length + 1), function(length) {
var string = Ox.range(length).map(function(i) {
return Ox.char(parseInt(hex[i], 16));
}).join('');
try {
Ox.decodeUTF8(string);
ret = match.slice(0, length * 3)
+ match.slice(length * 3).replace(/%/g, '%25');
return false;
} catch(e) {}
});
return ret || '%25' + hex[0] + replace(match.slice(3));
});
}
/*@
Ox.decodeURI <f> Decodes URI
Unlike window.decodeURI, this doesn't throw on trailing '%'.
(string) -> <s> Decoded string
@*/
Ox.decodeURI = function(string) {
return decodeURI(replace(string));
};
/*@
Ox.decodeURIComponent <f> Decodes URI component
Unlike window.decodeURIComponent, this doesn't throw on trailing '%'.
(string) -> <s> Decoded string
@*/
Ox.decodeURIComponent = function(string) {
return decodeURIComponent(replace(string));
};
}());
/*@
Ox.endsWith <f> Tests if a string ends with a given substring
Equivalent to `new RegExp(Ox.escapeRegExp(substring) + '$').test(string)`.