From 1b751d675b9b49aa11a5d2c375d304c1046d7968 Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Mon, 19 Feb 2024 10:43:19 +0100 Subject: [PATCH] Trim spaces from values in getFileBaseNameFromItem (#3711) --- chrome/content/zotero/xpcom/attachments.js | 3 +++ test/tests/attachmentsTest.js | 23 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 7e8e5f29ff..cf59eecc7c 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -2187,6 +2187,9 @@ Zotero.Attachments = new function () { if (truncate) { value = value.substr(0, truncate); } + + value = value.trim(); + if (prefix) { value = prefix + value; } diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index a5fecc7d2e..84b47c8d04 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -1290,7 +1290,7 @@ describe("Zotero.Attachments", function() { }); describe("#getFileBaseNameFromItem()", function () { - var item, itemManyAuthors, itemPatent, itemIncomplete, itemBookSection; + var item, itemManyAuthors, itemPatent, itemIncomplete, itemBookSection, itemSpaces; before(() => { item = createUnsavedDataObject('item', { title: 'Lorem Ipsum', itemType: 'journalArticle' }); @@ -1331,6 +1331,7 @@ describe("Zotero.Attachments", function() { itemIncomplete = createUnsavedDataObject('item', { title: 'Incomplete', itemType: 'preprint' }); itemBookSection = createUnsavedDataObject('item', { title: 'Book Section', itemType: 'bookSection' }); itemBookSection.setField('bookTitle', 'Book Title'); + itemSpaces = createUnsavedDataObject('item', { title: ' Spaces! ', itemType: 'book' }); }); @@ -1363,6 +1364,26 @@ describe("Zotero.Attachments", function() { ); }); + it('should trim whitespaces from a value', function () { + assert.equal( + Zotero.Attachments.getFileBaseNameFromItem(itemSpaces, '{{ title }}'), + 'Spaces!' + ); + assert.equal( + Zotero.Attachments.getFileBaseNameFromItem(item, '{{title truncate="6"}}'), + 'Lorem' + ); + assert.equal( + Zotero.Attachments.getFileBaseNameFromItem(item, '{{firstCreator truncate="7"}}'), + 'Barius' + ); + // but preserve if it's configured as a prefix or suffix + assert.equal( + Zotero.Attachments.getFileBaseNameFromItem(item, '{{title prefix=" " suffix=" "}}'), + ' Lorem Ipsum ' + ); + }); + it('should offer a range of options for composing creators', function () { assert.equal( Zotero.Attachments.getFileBaseNameFromItem(item, '{{ authors max="1" }}'),