Ox.decodeURI/Ox.decodeURIComponent: replace all '%' with '%25' that are not part of valid UTF8 sequence (fixes #1841)

This commit is contained in:
rlx 2013-08-26 23:18:05 +00:00
parent 4c390cc236
commit 7693079b8c

View file

@ -24,13 +24,34 @@ 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.decodeURIComponent <f> Decodes URI
Ox.decodeURI <f> Decodes URI
Unlike window.decodeURI, this doesn't throw on trailing '%'.
(string) -> <s> Decoded string
@*/
Ox.decodeURI = function(string) {
return decodeURIComponent(string.replace(/%(?![0-9A-F]{2})/g, '%25'));
return decodeURI(replace(string));
};
/*@
@ -39,9 +60,11 @@ Ox.decodeURIComponent <f> Decodes URI component
(string) -> <s> Decoded string
@*/
Ox.decodeURIComponent = function(string) {
return decodeURIComponent(string.replace(/%(?![0-9A-F]{2})/g, '%25'));
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)`.