www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit e272465f6c8e97f7b8637532922fc6b9a365f293
parent d4727521011d2299dc4bccbb669abf0961f1ae23
Author: Adomas VenĨkauskas <adomas.ven@gmail.com>
Date:   Thu, 20 Apr 2017 13:39:19 +0300

Fix DocumentData serialization hairiness due to #htmlSpecialChars

Add tests

Diffstat:
Mchrome/content/zotero/xpcom/integration.js | 4++--
Mtest/tests/integrationTests.js | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js @@ -3085,8 +3085,8 @@ Zotero.Integration.DocumentData = function(string) { Zotero.Integration.DocumentData.prototype.serializeXML = function() { var prefs = ""; for(var pref in this.prefs) { - prefs += '<pref name="'+Zotero.Utilities.htmlSpecialChars(pref)+'" '+ - 'value="'+Zotero.Utilities.htmlSpecialChars(this.prefs[pref])+'"/>'; + prefs += `<pref name="${Zotero.Utilities.htmlSpecialChars(pref)}" `+ + `value="${Zotero.Utilities.htmlSpecialChars(this.prefs[pref].toString())}"/>`; } return '<data data-version="'+Zotero.Utilities.htmlSpecialChars(DATA_VERSION)+'" '+ diff --git a/test/tests/integrationTests.js b/test/tests/integrationTests.js @@ -470,4 +470,65 @@ describe("Zotero.Integration", function () { }); }); }); + + describe("DocumentData", function() { + it('should properly unserialize document data', function() { + var serializedXMLData = "<data data-version=\"3\" zotero-version=\"5.0.SOURCE\"><session id=\"F0NFmZ32\"/><style id=\"http://www.zotero.org/styles/cell\" hasBibliography=\"1\" bibliographyStyleHasBeenSet=\"1\"/><prefs><pref name=\"fieldType\" value=\"ReferenceMark\"/><pref name=\"storeReferences\" value=\"true\"/><pref name=\"automaticJournalAbbreviations\" value=\"true\"/><pref name=\"noteType\" value=\"0\"/></prefs></data>"; + var data = new Zotero.Integration.DocumentData(serializedXMLData); + var expectedData = { + style: { + styleID: 'http://www.zotero.org/styles/cell', + locale: null, + hasBibliography: true, + bibliographyStyleHasBeenSet: true + }, + prefs: { + fieldType: 'ReferenceMark', + storeReferences: true, + automaticJournalAbbreviations: true, + noteType: '0' + }, + sessionID: 'F0NFmZ32', + zoteroVersion: '5.0.SOURCE', + dataVersion: '3' + }; + // Convert to JSON to remove functions from DocumentData object + assert.equal(JSON.stringify(data), JSON.stringify(expectedData)); + }); + + it('should properly serialize document data', function() { + sinon.spy(Zotero, 'debug'); + var data = new Zotero.Integration.DocumentData(); + data.sessionID = "owl-sesh"; + data.zoteroVersion = Zotero.version; + data.dataVersion = 3; + data.style = { + styleID: 'http://www.zotero.org/styles/cell', + locale: 'en-US', + hasBibliography: false, + bibliographyStyleHasBeenSet: true + }; + data.prefs = { + noteType: 1, + fieldType: "Field", + storeReferences: true, + automaticJournalAbbreviations: true + }; + + // Serialize and unserialize (above test makes sure unserialize works properly). + var processedData = new Zotero.Integration.DocumentData(data.serializeXML()); + + // This isn't ideal, but currently how it works. Better serialization which properly retains types + // coming with official 5.0 release. + data.prefs.noteType = "1"; + data.dataVersion = "3"; + + // Convert to JSON to remove functions from DocumentData objects + assert.equal(JSON.stringify(processedData), JSON.stringify(data)); + + // Make sure we are not triggering debug traces in Utilities.htmlSpecialChars() + assert.isFalse(Zotero.debug.calledWith(sinon.match.string, 1)); + Zotero.debug.restore(); + }); + }) });