Strip HTML tags from titles when generating filenames

This commit is contained in:
Dan Stillman 2020-06-02 15:31:12 -04:00
parent d7ee3fdee2
commit b2e902746a
2 changed files with 16 additions and 2 deletions

View file

@ -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;
}

View file

@ -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 <i>Bar</i> Foo<br><br/><br />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:\\");