diff --git a/src/js/util.js b/src/js/util.js index e890117..760c9cd 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -298,14 +298,18 @@ const normalizedHostname = (hn) => { * @param {string} str */ const decode = (str) => { - try { - if (decodeURI(str) !== decodeURIComponent(str)) { - return decode(decodeURIComponent(str)); + let last = str; + while (true) { + try { + const decoded = decodeURIComponent(last); + if (decoded === last) { + return last; + } + last = decoded; + } catch (e) { + return last; } - } catch (ex) { - err(ex); } - return str; }; // #endregion diff --git a/tools/test_decode.js b/tools/test_decode.js new file mode 100644 index 0000000..0ca1fd0 --- /dev/null +++ b/tools/test_decode.js @@ -0,0 +1,13 @@ +import { decode } from '../src/js/util.js'; +import assert from 'assert'; + +const encodedURI = 'https://example.com/%25'; // %25 is '%' +const expectedDecodedURI = 'https://example.com/%'; + +try { + const result = decode(encodedURI); + assert.strictEqual(result, expectedDecodedURI, 'The URI should be decoded'); + console.log('Test passed!'); +} catch (e) { + console.error('Test failed:', e.message); +}