From fa655c86e2d7de22faf0b04244da5d163a206e6b Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Tue, 24 Jan 2017 20:25:57 -0800 Subject: [PATCH] Fix, hopefully for the last time, quote escaping --- CHANGELOG.md | 4 ++++ lib/jsonview.js | 11 ++++++++--- package.json | 2 +- tests/issue141c.json | 3 +++ tests/issue141d.json | 3 +++ tests/issue141e.json | 6 ++++++ 6 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tests/issue141c.json create mode 100644 tests/issue141d.json create mode 100644 tests/issue141e.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d3d5ac..eb41bc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +JSONView 1.2.4 +--- +* Fixed a case where JSON would fail to parse if a string containing a number was preceded by a quote that was preceded by a lot of escaped slashes, or occurred near another number. + JSONView 1.2.3 --- * Fixed a case where JSON would fail to parse if a string containing a number was preceded by a quote that was preceded by an escaped slash. diff --git a/lib/jsonview.js b/lib/jsonview.js index e95800a..14ea219 100644 --- a/lib/jsonview.js +++ b/lib/jsonview.js @@ -105,9 +105,14 @@ var JSONView = Class({ var inQuotes = false; for (var i = 0; i < str.length; i++) { if (str[i] === '"') { - var escaped = - (i > 0 && str[i - 1] === '\\') && - (i > 1 && str[i - 2] !== '\\'); + var escaped = false; + for (var lookback = i - 1; lookback >= 0; lookback--) { + if (str[lookback] === '\\') { + escaped = !escaped; + } else { + break; + } + } if (!escaped) { inQuotes = !inQuotes; } diff --git a/package.json b/package.json index 5c89bb4..708073f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "id": "jsonview@brh.numbera.com", - "version": "1.2.3", + "version": "1.2.4", "name": "jsonview", "title": "JSONView", "description": "View JSON documents in the browser.", diff --git a/tests/issue141c.json b/tests/issue141c.json new file mode 100644 index 0000000..deebaad --- /dev/null +++ b/tests/issue141c.json @@ -0,0 +1,3 @@ +{ +"dots":"\"v4.0\" \"v4.0\"" +} diff --git a/tests/issue141d.json b/tests/issue141d.json new file mode 100644 index 0000000..152a642 --- /dev/null +++ b/tests/issue141d.json @@ -0,0 +1,3 @@ +{ +"dots":"\\\"v4.0\" \\\\\\\\\\\"v4.0\"" +} diff --git a/tests/issue141e.json b/tests/issue141e.json new file mode 100644 index 0000000..a61210e --- /dev/null +++ b/tests/issue141e.json @@ -0,0 +1,6 @@ +{ + "abc": { + "value": "{\"user\":\"NN12345\"}" + }, + "MM98765": {} +}