From 3be8abeae284265774ae77a7a9c7ffe7ca2cf5f4 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 9 Jan 2023 05:28:58 -0500 Subject: [PATCH] Fix broken creator autocomplete if `place` becomes a base field Well this was a wild one to debug. Creator fields were only initialized for autocomplete due to a series of >10-year-old bugs: 1) In `showEditor()`, `Zotero.ItemFields.isAutocompleteField(fieldName)` was called for creator fields, which would pass, e.g., `creator-0-lastName`. 2) In `isAutocompleteField()`, `ItemFields.getName()` would normalize `creator-0-lastName` to `false`, since it's not a valid field. 3) `isAutocompleteField()` listed `place` as a base field despite its not having any mapped fields, so when `getTypeFieldsFromBase()` was called on it, the return value would be `false`, which would be added to the list of autocomplete fields, which would mean that the normalized field of `false` from `creator-0-lastName` would match, which would mean that `isAutocompleteField('creator-0-lastName')` would always return true...as long as `place` never gained a mapped field. Except `isAutocompleteField()` wasn't supposed to be the test for initializing autocomplete for creator fields anyway -- `fieldName == 'creator'` was. But `fieldName` is something like `creator-0-lastName`, not `creator`, which meant that that test always failed, which meant that if `place` did gain a mapped field, both tests would fail, which would cause the creator field not to be initialized for autocomplete, which would cause it to break as soon as you started to type into it. This fixes that. --- chrome/content/zotero/elements/itemBox.js | 3 +-- chrome/content/zotero/xpcom/data/itemFields.js | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/elements/itemBox.js b/chrome/content/zotero/elements/itemBox.js index 8142a92800..83971bd2fc 100644 --- a/chrome/content/zotero/elements/itemBox.js +++ b/chrome/content/zotero/elements/itemBox.js @@ -1573,8 +1573,7 @@ t.setAttribute('rows', 8); } // Add auto-complete for certain fields - else if (Zotero.ItemFields.isAutocompleteField(fieldName) - || fieldName == 'creator') { + else if (field == 'creator' || Zotero.ItemFields.isAutocompleteField(fieldName)) { t = document.createElement("input", { is: 'shadow-autocomplete-input' }); t.setAttribute('autocompletesearch', 'zotero'); diff --git a/chrome/content/zotero/xpcom/data/itemFields.js b/chrome/content/zotero/xpcom/data/itemFields.js index 176fd81ab0..b0ed5788f4 100644 --- a/chrome/content/zotero/xpcom/data/itemFields.js +++ b/chrome/content/zotero/xpcom/data/itemFields.js @@ -349,7 +349,11 @@ Zotero.ItemFields = new function() { this.isAutocompleteField = function (field) { - field = this.getName(field); + var fieldName = this.getName(field); + if (!fieldName) { + Zotero.logError(`Can't check autocomplete for invalid field '${field}'`); + return false; + } var autoCompleteFields = [ 'journalAbbreviation', @@ -379,7 +383,7 @@ Zotero.ItemFields = new function() { autoCompleteFields = autoCompleteFields.concat(add); } - return autoCompleteFields.indexOf(field) != -1; + return autoCompleteFields.includes(fieldName); }