From ff38ff5b9d5fc976ebbd9f3ca330c8dd0cfcedad 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/bindings/itembox.xml | 3 +-- chrome/content/zotero/xpcom/data/itemFields.js | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/bindings/itembox.xml b/chrome/content/zotero/bindings/itembox.xml index 2b6904a52b..be9af33a05 100644 --- a/chrome/content/zotero/bindings/itembox.xml +++ b/chrome/content/zotero/bindings/itembox.xml @@ -1584,8 +1584,7 @@ } else { // Add auto-complete for certain fields - if (Zotero.ItemFields.isAutocompleteField(fieldName) - || fieldName == 'creator') { + if (field == 'creator' || Zotero.ItemFields.isAutocompleteField(fieldName)) { t.setAttribute('type', 'autocomplete'); t.setAttribute('autocompletesearch', 'zotero'); diff --git a/chrome/content/zotero/xpcom/data/itemFields.js b/chrome/content/zotero/xpcom/data/itemFields.js index 0013f336cb..e5cfb1d955 100644 --- a/chrome/content/zotero/xpcom/data/itemFields.js +++ b/chrome/content/zotero/xpcom/data/itemFields.js @@ -347,7 +347,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', @@ -377,7 +381,7 @@ Zotero.ItemFields = new function() { autoCompleteFields = autoCompleteFields.concat(add); } - return autoCompleteFields.indexOf(field) != -1; + return autoCompleteFields.includes(fieldName); }