From 0f5d475c7839e77fca13973caaaa62ff65a36102 Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Tue, 27 Aug 2013 08:56:07 +0000 Subject: [PATCH] fix a bug in Ox.decodeUTF8; move Ox.decodeURI/Ox.decodeURIComponent to encoding module --- source/Ox/js/Encoding.js | 43 +++++++++++++++++++++++++++++++++++++++- source/Ox/js/String.js | 41 -------------------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/source/Ox/js/Encoding.js b/source/Ox/js/Encoding.js index 94522bdc..00e3e896 100644 --- a/source/Ox/js/Encoding.js +++ b/source/Ox/js/Encoding.js @@ -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 Decodes URI + Unlike window.decodeURI, this doesn't throw on trailing '%'. + (string) -> Decoded string + @*/ + Ox.decodeURI = function(string) { + return decodeURI(replace(string)); + }; + + /*@ + Ox.decodeURIComponent Decodes URI component + Unlike window.decodeURIComponent, this doesn't throw on trailing '%'. + (string) -> Decoded string + @*/ + Ox.decodeURIComponent = function(string) { + return decodeURIComponent(replace(string)); + }; + +}()); + /*@ Ox.encodeUTF8 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 ( diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index cdb9c92e..8f28a701 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -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 Decodes URI - Unlike window.decodeURI, this doesn't throw on trailing '%'. - (string) -> Decoded string - @*/ - Ox.decodeURI = function(string) { - return decodeURI(replace(string)); - }; - - /*@ - Ox.decodeURIComponent Decodes URI component - Unlike window.decodeURIComponent, this doesn't throw on trailing '%'. - (string) -> Decoded string - @*/ - Ox.decodeURIComponent = function(string) { - return decodeURIComponent(replace(string)); - }; - -}()); - /*@ Ox.endsWith Tests if a string ends with a given substring Equivalent to `new RegExp(Ox.escapeRegExp(substring) + '$').test(string)`.