diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js index 87eff6333..99ae0692b 100644 --- a/chrome/content/zotero/xpcom/data/creators.js +++ b/chrome/content/zotero/xpcom/data/creators.js @@ -175,6 +175,7 @@ Zotero.Creators = new function() { switch (field) { case 'firstName': case 'lastName': + if (val === undefined) continue; cleanedData[field] = val.trim().normalize(); break; @@ -184,9 +185,9 @@ Zotero.Creators = new function() { } } - // Handle API JSON format + // Handle API JSON .name if (data.name !== undefined) { - cleanedData.lastName = data.name.trim(); + cleanedData.lastName = data.name.trim().normalize(); cleanedData.fieldMode = 1; } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index b6599c70c..c18257b10 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -974,6 +974,11 @@ Zotero.Item.prototype.getCreatorsJSON = function () { * */ Zotero.Item.prototype.setCreator = function (orderIndex, data) { + var itemTypeID = this._itemTypeID; + if (!itemTypeID) { + throw new Error('Item type must be set before setting creators'); + } + this._requireData('creators'); data = Zotero.Creators.cleanData(data); @@ -983,7 +988,6 @@ Zotero.Item.prototype.setCreator = function (orderIndex, data) { } // If creatorTypeID isn't valid for this type, use the primary type - var itemTypeID = this._itemTypeID; if (!data.creatorTypeID || !Zotero.CreatorTypes.isValidForItemType(data.creatorTypeID, itemTypeID)) { var msg = "Creator type '" + Zotero.CreatorTypes.getName(data.creatorTypeID) + "' " + "isn't valid for " + Zotero.ItemTypes.getName(itemTypeID) @@ -4022,7 +4026,6 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { case 'key': case 'version': case 'itemType': - case 'creators': case 'note': // Use? case 'md5': diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index cc726ac49..1ed70cc1d 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -242,6 +242,53 @@ describe("Zotero.Item", function () { }) }); + describe("#setCreators", function () { + it("should accept an array of creators in API JSON format", function* () { + var creators = [ + { + firstName: "First", + lastName: "Last", + creatorType: "author" + }, + { + name: "Test Name", + creatorType: "editor" + } + ]; + + var item = new Zotero.Item("journalArticle"); + item.setCreators(creators); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + yield item.loadCreators(); + assert.sameDeepMembers(item.getCreatorsJSON(), creators); + }) + + it("should accept an array of creators in internal format", function* () { + var creators = [ + { + firstName: "First", + lastName: "Last", + fieldMode: 0, + creatorTypeID: 1 + }, + { + firstName: "", + lastName: "Test Name", + fieldMode: 1, + creatorTypeID: 2 + } + ]; + + var item = new Zotero.Item("journalArticle"); + item.setCreators(creators); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + yield item.loadCreators(); + assert.sameDeepMembers(item.getCreators(), creators); + }) + }) + describe("#attachmentCharset", function () { it("should get and set a value", function* () { var charset = 'utf-8';