diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 749741c81..244aaaf6a 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1078,6 +1078,14 @@ Zotero.Item.prototype.setCreator = function (orderIndex, data) { * @param {Object[]} data - An array of creator data in internal or API JSON format */ Zotero.Item.prototype.setCreators = function (data) { + // If empty array, clear all existing creators + if (!data.length) { + while (this.hasCreatorAt(0)) { + this.removeCreator(0); + } + return; + } + for (let i = 0; i < data.length; i++) { this.setCreator(i, data[i]); } diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 591baa881..1b015f195 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -435,6 +435,21 @@ describe("Zotero.Item", function () { item = Zotero.Items.get(id); assert.sameDeepMembers(item.getCreators(), creators); }) + + it("should clear creators if empty array passed", function () { + var item = createUnsavedDataObject('item'); + item.setCreators([ + { + firstName: "First", + lastName: "Last", + fieldMode: 0, + creatorTypeID: 1 + } + ]); + assert.lengthOf(item.getCreators(), 1); + item.setCreators([]); + assert.lengthOf(item.getCreators(), 0); + }); }) describe("#getAttachments()", function () {