Fix broken tag selector due to leading/trailing whitespace in colored tags

And sanitize colored tags in DB at startup
This commit is contained in:
Dan Stillman 2019-09-25 17:33:36 -04:00
parent ac255634c5
commit b310ccb04e
2 changed files with 39 additions and 18 deletions

View file

@ -580,19 +580,6 @@ Zotero.Tags = new function() {
} }
var tagColors = Zotero.SyncedSettings.get(libraryID, 'tagColors'); var tagColors = Zotero.SyncedSettings.get(libraryID, 'tagColors');
// Sanitize -- shouldn't be necessary, but just in case a bad value makes it into the setting
if (!Array.isArray(tagColors)) {
tagColors = [];
}
tagColors = tagColors.filter(color => {
if (typeof color != 'object' || typeof color.name != 'string' || typeof color.color != 'string') {
Zotero.logError("Skipping invalid colored tag: " + JSON.stringify(color));
return false;
}
return true;
});
_libraryColors[libraryID] = tagColors; _libraryColors[libraryID] = tagColors;
_libraryColorsByName[libraryID] = new Map; _libraryColorsByName[libraryID] = new Map;
@ -619,9 +606,10 @@ Zotero.Tags = new function() {
} }
this.getColors(libraryID); this.getColors(libraryID);
var tagColors = _libraryColors[libraryID]; var tagColors = _libraryColors[libraryID];
name = name.trim();
// Unset // Unset
if (!color) { if (!color) {
// Trying to clear color on tag that doesn't have one // Trying to clear color on tag that doesn't have one

View file

@ -70,7 +70,7 @@ Zotero.SyncedSettings = (function () {
}) })
}, },
loadAll: Zotero.Promise.coroutine(function* (libraryID) { loadAll: async function (libraryID) {
Zotero.debug("Loading synced settings for library " + libraryID); Zotero.debug("Loading synced settings for library " + libraryID);
if (!_cache[libraryID]) { if (!_cache[libraryID]) {
@ -81,7 +81,7 @@ Zotero.SyncedSettings = (function () {
var sql = "SELECT setting, value, synced, version FROM syncedSettings " var sql = "SELECT setting, value, synced, version FROM syncedSettings "
+ "WHERE libraryID=?"; + "WHERE libraryID=?";
yield Zotero.DB.queryAsync( await Zotero.DB.queryAsync(
sql, sql,
libraryID, libraryID,
{ {
@ -106,8 +106,41 @@ Zotero.SyncedSettings = (function () {
} }
); );
// TODO: Delete invalid settings //
}), // Delete invalid settings
//
if (_cache[libraryID].tagColors) {
// Sanitize colored tags -- shouldn't be necessary, but just in case a bad value makes it
// into the setting
let fixed = false;
let tagColors = _cache[libraryID].tagColors.value;
// Not an array
if (!Array.isArray(tagColors)) {
tagColors = [];
fixed = true;
}
// Invalid tag
tagColors = tagColors.filter((color) => {
if (typeof color != 'object' || typeof color.name != 'string' || typeof color.color != 'string') {
Zotero.logError("Removing invalid colored tag: " + JSON.stringify(color));
tagsFixed = true;
return false;
}
return true;
});
// Before whitespace was trimmed in Tags.setColor() in 5.0.75
tagColors.forEach((tag) => {
let trimmed = tag.name.trim();
if (trimmed != tag.name) {
tag.name = trimmed;
fixed = true;
}
});
if (fixed) {
await this.set(libraryID, 'tagColors', tagColors);
}
}
},
/** /**
* Return settings object * Return settings object