From b2e902746a14d1fb3be91b806d78ede95264457e Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 2 Jun 2020 15:31:12 -0400 Subject: [PATCH] Strip HTML tags from titles when generating filenames --- chrome/content/zotero/xpcom/attachments.js | 10 ++++++++-- test/tests/attachmentsTest.js | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index f3de0cefba..3677e71952 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -1887,9 +1887,14 @@ Zotero.Attachments = new function(){ break; } + var value; switch (field) { + case 'title': + value = item.getField('title', false, true); + break; + case 'year': - var value = item.getField('date', true, true); + value = item.getField('date', true, true); if (value) { value = Zotero.Date.multipartToSQL(value).substr(0, 4); if (value == '0000') { @@ -1899,7 +1904,7 @@ Zotero.Attachments = new function(){ break; default: - var value = '' + item.getField(field, false, true); + value = '' + item.getField(field, false, true); } var re = new RegExp("\{?([^%\{\}]*)" + rpl + "(\{[0-9]+\})?" + "([^%\{\}]*)\}?"); @@ -1924,6 +1929,7 @@ Zotero.Attachments = new function(){ formatString = rpl('year'); formatString = rpl('title'); + formatString = Zotero.Utilities.cleanTags(formatString); formatString = Zotero.File.getValidFileName(formatString); return formatString; } diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index c223c8d535..47f0c56ca5 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -971,6 +971,14 @@ describe("Zotero.Attachments", function() { }); }); + describe("#getFileBaseNameFromItem()", function () { + it("should strip HTML tags from title", async function () { + var item = createUnsavedDataObject('item', { title: 'Foo Bar Foo


Bar' }); + var str = Zotero.Attachments.getFileBaseNameFromItem(item); + assert.equal(str, 'Foo Bar Foo Bar'); + }); + }); + describe("#getBaseDirectoryRelativePath()", function () { it("should handle base directory at Windows drive root", function () { Zotero.Prefs.set('baseAttachmentPath', "C:\\");