From e00d98d3a11f6233651a052c108117cf44873edc Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Wed, 14 Aug 2024 15:58:04 -0600 Subject: [PATCH] cleanDOI: Handle malformed URI escape sequence (#36) --- test/tests/utilitiesTest.js | 4 ++++ utilities.js | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/tests/utilitiesTest.js b/test/tests/utilitiesTest.js index 60b1bc4..8569974 100644 --- a/test/tests/utilitiesTest.js +++ b/test/tests/utilitiesTest.js @@ -112,6 +112,10 @@ describe("Zotero.Utilities", function() { it("should parse a DOI in brackets", function () { assert.equal(cleanDOI(`Foo bar [${doi}] foo bar`), doi); }); + + it("should parse a DOI with an invalid URI-encoded character", function () { + assert.equal(cleanDOI('https://doi.org/10.101/%E0%A4%A')) + }); }); diff --git a/utilities.js b/utilities.js index 7c3581f..761e0d3 100644 --- a/utilities.js +++ b/utilities.js @@ -482,9 +482,15 @@ var Utilities = { if(typeof(x) != "string") { throw new Error("cleanDOI: argument must be a string"); } - // If it's a URL, decode it - if (x.match(/^https?:/)) { - x = decodeURIComponent(x); + // If it's a URL, try to decode it + if (/^https?:/.test(x)) { + try { + x = decodeURIComponent(x); + } + catch (e) { + // URI contains an invalid escape sequence + Zotero.debug("Not decoding URL-like DOI because of invalid escape sequence: " + x); + } } // Even if it's not a URL, decode %3C followed by %3E as < > var openingPos = x.indexOf("%3C");