diff --git a/chrome/content/zotero/xpcom/data/dataObjectUtilities.js b/chrome/content/zotero/xpcom/data/dataObjectUtilities.js index dfa737e58e..478f21cad3 100644 --- a/chrome/content/zotero/xpcom/data/dataObjectUtilities.js +++ b/chrome/content/zotero/xpcom/data/dataObjectUtilities.js @@ -321,6 +321,13 @@ Zotero.DataObjectUtilities = { } break; + case 'note': + let change = this._htmlDiff(field, val1, val2); + if (change) { + changeset.push(change); + } + break; + default: var changed = val1 !== val2; if (changed) { @@ -449,6 +456,45 @@ Zotero.DataObjectUtilities = { return changeset; }, + _htmlDiff: function (field, html1, html2 = "") { + if (html1 == "" && html2 != "") { + return { + field, + op: "add", + value: html2 + }; + } + if (html1 != "" && html2 == "") { + return { + field, + op: "delete" + }; + } + + // Until we have a consistent way of sanitizing HTML on client and server, account for differences + var mods = [ + ['
', '
\u00a0
'] + ]; + var a = html1; + var b = html2; + for (let mod of mods) { + a = a.replace(new RegExp(mod[0], 'g'), mod[1]); + b = b.replace(new RegExp(mod[0], 'g'), mod[1]); + } + if (a != b) { + Zotero.debug("HTML diff:"); + Zotero.debug(a); + Zotero.debug(b); + return { + field, + op: "modify", + value: html2 + }; + } + + return false; + }, + _tagsDiff: function (data1, data2 = []) { var changeset = []; outer: diff --git a/test/tests/dataObjectUtilitiesTest.js b/test/tests/dataObjectUtilitiesTest.js index bf425c1a2c..fcf97e8cc2 100644 --- a/test/tests/dataObjectUtilitiesTest.js +++ b/test/tests/dataObjectUtilitiesTest.js @@ -104,6 +104,19 @@ describe("Zotero.DataObjectUtilities", function() { }) }) + describe("notes", function () { + it("should ignore sanitization changes", function* () { + var json1 = { + note: "" + }; + var json2 = { + note: "
" + }; + var changes = Zotero.DataObjectUtilities.diff(json1, json2); + assert.lengthOf(changes, 0); + }); + }); + // // Relations //