Fix creator extraction from Extra

Creators shouldn't be extracted if creators of the same type already
exist on the item, to follow citeproc-js behavior, but the code to do
that was broken.
This commit is contained in:
Dan Stillman 2025-12-26 11:05:59 -05:00
parent d1fd7c998b
commit 7ac22dbef2
2 changed files with 23 additions and 14 deletions

View file

@ -1204,11 +1204,11 @@ Zotero.Utilities.Internal = {
c.name = value;
}
if (item) {
let creatorTypeID = Zotero.CreatorTypes.getID(possibleCreatorType);
if (Zotero.CreatorTypes.isValidForItemType(creatorTypeID, itemTypeID)
let possibleCreatorTypeID = Zotero.CreatorTypes.getID(possibleCreatorType);
if (Zotero.CreatorTypes.isValidForItemType(possibleCreatorTypeID, itemTypeID)
// Ignore if there are any creators of this type on the item already,
// to follow citeproc-js behavior
&& !item.getCreators().some(x => x.creatorType == possibleCreatorType)) {
&& !item.getCreators().some(x => x.creatorTypeID == possibleCreatorTypeID)) {
creators.push(c);
return false;
}

View file

@ -265,17 +265,6 @@ describe("Zotero.Utilities.Internal", function () {
assert.equal(fields.size, 0);
});
it("should extract an author and add it to existing creators", function () {
var item = createUnsavedDataObject('item', { itemType: 'book' });
item.setCreator(0, { creatorType: 'author', name: 'Foo' });
var str = 'author: Bar';
var { fields, creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item);
assert.equal(fields.size, 0);
assert.lengthOf(creators, 1);
assert.equal(creators[0].creatorType, 'author');
assert.equal(creators[0].name, 'Bar');
});
it("should extract a CSL date field", function () {
var str = 'issued: 2000';
var { fields, extra } = Zotero.Utilities.Internal.extractExtraFields(str);
@ -313,6 +302,26 @@ describe("Zotero.Utilities.Internal", function () {
assert.strictEqual(extra, str);
});
it("shouldn't extract a creator if creators of same type already exist", function () {
var item = createUnsavedDataObject('item', { itemType: 'book' });
item.setCreator(0, { creatorType: 'author', name: 'Foo' });
var str = 'author: Bar';
var { fields, creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item);
assert.equal(fields.size, 0);
assert.lengthOf(creators, 0);
});
it("should extract a creator if creators of the same type don't exist", function () {
var item = createUnsavedDataObject('item', { itemType: 'book' });
item.setCreator(0, { creatorType: 'author', name: 'Foo' });
var str = 'editor: Bar';
var { fields, creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item);
assert.equal(fields.size, 0);
assert.lengthOf(creators, 1);
assert.equal(creators[0].creatorType, 'editor');
assert.equal(creators[0].name, 'Bar');
});
it("should extract the citeproc-js cheater syntax", function () {
var issued = '{:number-of-pages:11}\n{:issued:2014}';
var { fields, extra } = Zotero.Utilities.Internal.extractExtraFields(issued);