From 141258d5642bf14b7bf2b9360edf6a4171ed6a04 Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Thu, 7 Jul 2022 12:19:37 +0200 Subject: [PATCH] Fix a bug in regex extracting fields to "extra" Because regex is built using a template string, \s* is actually escaped into s*, i.e. literal "s" appearing 0 or more times. In most cases this would mean that output can have spacing slightly off. In extreme case, when identifier starts with letter "s", this could this could lead to identifier being stored incorrectly. Also adjusted tests to be more strict and mock data to cover this case. --- chrome/content/zotero/import/mendeley/mendeleyImport.js | 2 +- test/tests/data/mendeleyMock/items-simple.json | 2 +- test/tests/mendeleyImportTest.js | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/import/mendeley/mendeleyImport.js b/chrome/content/zotero/import/mendeley/mendeleyImport.js index 1e54e233c7..7c37816f5b 100644 --- a/chrome/content/zotero/import/mendeley/mendeleyImport.js +++ b/chrome/content/zotero/import/mendeley/mendeleyImport.js @@ -1241,7 +1241,7 @@ Zotero_Import_Mendeley.prototype._makeCreator = function (creatorType, firstName Zotero_Import_Mendeley.prototype._addExtraField = function (extra, field, val) { // Strip the field if it appears at the beginning of the value (to avoid "DOI: DOI: 10...") if (typeof val == 'string') { - val = val.replace(new RegExp(`^${field}:\s*`, 'i'), ""); + val = val.replace(new RegExp(`^${field}:\\s*`, 'i'), ""); } extra = extra ? extra + '\n' : ''; if (field != 'arXiv') { diff --git a/test/tests/data/mendeleyMock/items-simple.json b/test/tests/data/mendeleyMock/items-simple.json index 997edbe792..ff3aa22494 100644 --- a/test/tests/data/mendeleyMock/items-simple.json +++ b/test/tests/data/mendeleyMock/items-simple.json @@ -14,7 +14,7 @@ "identifiers": { "doi": "10.1111", - "pmid": "11111111", + "pmid": "PMID: 11111111", "arxiv": "1111.2222" }, "starred": false, diff --git a/test/tests/mendeleyImportTest.js b/test/tests/mendeleyImportTest.js index 796a34cd2e..a4b65ff887 100644 --- a/test/tests/mendeleyImportTest.js +++ b/test/tests/mendeleyImportTest.js @@ -174,8 +174,7 @@ describe('Zotero_Import_Mendeley', function () { // identifiers assert.equal(journal.getField('DOI'), '10.1111'); - assert.include(journal.getField('extra'), 'PMID: 11111111'); - assert.include(journal.getField('extra'), 'arXiv: 1111.2222'); + assert.sameMembers(journal.getField('extra').split('\n'), ['PMID: 11111111', 'arXiv: 1111.2222']); // attachment & annotations assert.lengthOf(withpdf.getAttachments(), 1);