From 182bfb9a38bf4046fa1eb33f8a46dddcf2bade16 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 20 Aug 2024 02:50:51 -0400 Subject: [PATCH] OS.Path shim: Add Windows version of fromFileURI() from Fx102 Should fix #4588 for real --- chrome/content/zotero/osfile.mjs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/chrome/content/zotero/osfile.mjs b/chrome/content/zotero/osfile.mjs index 1f7286ab6d..279594ac67 100644 --- a/chrome/content/zotero/osfile.mjs +++ b/chrome/content/zotero/osfile.mjs @@ -286,6 +286,28 @@ export let OS = { }, fromFileURI: function (uri) { + if (Services.appinfo.OS == "WINNT") { + let url = new URL(uri); + if (url.protocol != "file:") { + throw new Error("fromFileURI expects a file URI"); + } + + // strip leading slash, since Windows paths don't start with one + uri = url.pathname.substr(1); + + let path = decodeURI(uri); + // decode a few characters where URL's parsing is overzealous + path = path.replace(/%(3b|3f|23)/gi, match => decodeURIComponent(match)); + path = this.normalize(path); + + // this.normalize() does not remove the trailing slash if the path + // component is a drive letter. eg. 'C:\'' will not get normalized. + if (path.endsWith(":\\")) { + path = path.substr(0, path.length - 1); + } + return this.normalize(path); + } + let url = new URL(uri); if (url.protocol != "file:") { throw new Error("fromFileURI expects a file URI");