From 7386b376f361b4e7fe4283b40f12c3368b8b8123 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 18 Aug 2017 16:04:22 +0200 Subject: [PATCH] Fix linked attachment base directory handling at drive root The first letter of the relative path was being removed on save if the base directory was set to the drive root (e.g. D:\ instead of D:\foo). --- chrome/content/zotero/xpcom/attachments.js | 17 +++++++++-------- test/tests/attachmentsTest.js | 6 ++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index fb3de655e..20d07a4ad 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -882,14 +882,15 @@ Zotero.Attachments = new function(){ } if (Zotero.File.directoryContains(basePath, path)) { - basePath = OS.Path.normalize(basePath); - path = OS.Path.normalize(path); - path = this.BASE_PATH_PLACEHOLDER - + path.substr(basePath.length + 1) - // Since stored paths can be synced to other platforms, use - // forward slashes for consistency. resolveRelativePath() will - // convert to the appropriate platform-specific slash on use. - .replace(/\\/g, "/"); + // Since stored paths can be synced to other platforms, use forward slashes for consistency. + // resolveRelativePath() will convert to the appropriate platform-specific slash on use. + basePath = OS.Path.normalize(basePath).replace(/\\/g, "/"); + path = OS.Path.normalize(path).replace(/\\/g, "/"); + // Normalize D:\ vs. D:\foo + if (!basePath.endsWith('/')) { + basePath += '/'; + } + path = this.BASE_PATH_PLACEHOLDER + path.substr(basePath.length) } return path; diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index ce2a9fffb..c9f885606 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -210,6 +210,12 @@ describe("Zotero.Attachments", function() { }); describe("#getBaseDirectoryRelativePath()", function () { + it("should handle base directory at Windows drive root", function () { + Zotero.Prefs.set('baseAttachmentPath', "C:\\"); + var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\file.txt"); + assert.equal(path, Zotero.Attachments.BASE_PATH_PLACEHOLDER + "file.txt"); + }); + it("should convert backslashes to forward slashes", function () { Zotero.Prefs.set('baseAttachmentPath', "C:\\foo\\bar"); var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\foo\\bar\\test\\file.txt");