From 6c58389563607fea62b9b8fdc6d207c47f60e628 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 27 Feb 2017 23:34:16 -0500 Subject: [PATCH] Fix translation error when firstName is null for fieldMode 1 Some translators (e.g., CrossRef) return firstName: null with fieldMode: 1, which was causing an error --- chrome/content/zotero/xpcom/data/creators.js | 7 ++++--- test/tests/creatorsTest.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js index 94fbeaae86..ae21064df6 100644 --- a/chrome/content/zotero/xpcom/data/creators.js +++ b/chrome/content/zotero/xpcom/data/creators.js @@ -165,13 +165,14 @@ Zotero.Creators = new function() { if (data.name !== undefined && data.fieldMode === 0) { throw new Error("'fieldMode' cannot be 0 with 'name' property"); } - if (data.fieldMode === 1 && !(data.firstName === undefined || data.firstName === "")) { + if (data.fieldMode === 1 + && !(data.firstName === undefined || data.firstName === "" || data.firstName === null)) { throw new Error("'fieldMode' cannot be 1 with 'firstName' property"); } if (data.name !== undefined && typeof data.name != 'string') { throw new Error("'name' must be a string"); } - if (data.firstName !== undefined && typeof data.firstName != 'string') { + if (data.firstName !== undefined && data.firstName !== null && typeof data.firstName != 'string') { throw new Error("'firstName' must be a string"); } if (data.lastName !== undefined && typeof data.lastName != 'string') { @@ -189,7 +190,7 @@ Zotero.Creators = new function() { switch (field) { case 'firstName': case 'lastName': - if (val === undefined) continue; + if (val === undefined || val === null) continue; cleanedData[field] = val.trim().normalize(); break; diff --git a/test/tests/creatorsTest.js b/test/tests/creatorsTest.js index 90ccf202a1..4fc3d5f6fb 100644 --- a/test/tests/creatorsTest.js +++ b/test/tests/creatorsTest.js @@ -18,4 +18,17 @@ describe("Zotero.Creators", function() { assert.propertyVal(data2, "lastName", data1.lastName); }); }); + + describe("#cleanData()", function () { + it("should allow firstName to be null for fieldMode 1", function* () { + var data = Zotero.Creators.cleanData({ + firstName: null, + lastName: "Test", + fieldMode: 1 + }); + assert.propertyVal(data, 'fieldMode', 1); + assert.propertyVal(data, 'firstName', ''); + assert.propertyVal(data, 'lastName', 'Test'); + }); + }); });