From 264f55545a9edcc431b17a0b1573d97ef8d99239 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Wed, 12 Feb 2025 22:29:49 -0500 Subject: [PATCH] Fix faulty siblings-of-type check in setAutoAttachmentTitle() (#5045) --- chrome/content/zotero/xpcom/data/item.js | 16 +++++++----- chrome/content/zotero/zoteroPane.js | 2 +- test/tests/zoteroPaneTest.js | 32 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index f4faf27907..339ccd7aad 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2556,12 +2556,15 @@ Zotero.Item.prototype.numNonHTMLFileAttachments = function () { }; -Zotero.Item.prototype.numFileAttachmentsWithContentType = function (contentType) { +Zotero.Item.prototype.getFileAttachmentsWithContentType = function (contentType) { this._requireData('childItems'); - return this.getAttachments() - .map(itemID => Zotero.Items.get(itemID)) - .filter(item => item.isFileAttachment() && item.attachmentContentType == contentType) - .length; + return Zotero.Items.get(this.getAttachments()) + .filter(item => item.isFileAttachment() && item.attachmentContentType == contentType); +}; + + +Zotero.Item.prototype.numFileAttachmentsWithContentType = function (contentType) { + return this.getFileAttachmentsWithContentType(contentType).length; }; @@ -3974,7 +3977,8 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs // If this is the only attachment of its type on the parent item and the // file is being renamed, give it a default title ("PDF", "Webpage", etc.) let isFirstOfType = this.parentItemID - && this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1; + && this.parentItem.getFileAttachmentsWithContentType(this.attachmentContentType) + .every(item => item === this); let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this); if (isFirstOfType && isBeingRenamed) { let defaultTitle = this._getDefaultTitleForAttachmentContentType(); diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 537d3be088..93d8bcbf8e 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -4516,7 +4516,7 @@ var ZoteroPane = new function() /** * @param {Boolean} [link] * @param {Number} [parentItemID] - * @param {nsIFile[]} [files] Used instead of showing a file picker - for tests + * @param {String[]} [files] Used instead of showing a file picker - for tests * @returns {Promise} */ this.addAttachmentFromDialog = async function (link, parentItemID, files = null) { diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js index eb875e09ef..a5a167fba1 100644 --- a/test/tests/zoteroPaneTest.js +++ b/test/tests/zoteroPaneTest.js @@ -1692,6 +1692,38 @@ describe("ZoteroPane", function() { assert.equal(parentItem.getAttachments().length, 4); assert.equal(epubAttachment.getField('title'), Zotero.getString('file-type-ebook')); }); + + describe("Linked file renaming", function () { + before(() => { + Zotero.Prefs.set('autoRenameFiles.linked', true); + }); + + after(() => { + Zotero.Prefs.clear('autoRenameFiles.linked'); + }); + + it("should only rename and change the title of the first PDF attachment", async function () { + let testFile = getTestDataDirectory(); + testFile.append('test.pdf'); + + let tempDir = await getTempDirectory(); + let copy1 = PathUtils.join(tempDir, 'copy1.pdf'); + let copy2 = PathUtils.join(tempDir, 'copy2.pdf'); + + await IOUtils.copy(testFile.path, copy1); + await IOUtils.copy(testFile.path, copy2); + + let parentItem = await createDataObject('item', { title: 'Foo' }); + + let [attachment1] = await zp.addAttachmentFromDialog(false, parentItem.id, [copy1]); + assert.equal(attachment1.getField('title'), Zotero.getString('file-type-pdf')); + assert.equal(attachment1.attachmentFilename, 'Foo.pdf'); + + let [attachment2] = await zp.addAttachmentFromDialog(false, parentItem.id, [copy2]); + assert.equal(attachment2.getField('title'), 'copy2'); + assert.equal(attachment2.attachmentFilename, 'copy2.pdf'); + }); + }); }); describe("#createParentItemsFromSelected()", function () {