diff --git a/HISTORY.md b/HISTORY.md index 9a3172a..8f464d6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -11,6 +11,8 @@ https://github.com/josdejong/jsoneditor - Implemented repairing JSON objects containing left and right single and double quotes (which you get when typing a JSON object in Word) in `text` and `code` mode. +- Implemented repairing JSON objects containing special white space + characters like non-breaking space. ## 2017-09-16, version 5.9.6 diff --git a/src/js/util.js b/src/js/util.js index 0694b09..90b4d27 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -163,6 +163,11 @@ exports.sanitize = function (jsString) { else if (c === '/' && next() === '/') { skipComment(); } + else if (c === '\u00A0' || (c >= '\u2000' && c <= '\u200A') || c === '\u202F' || c === '\u205F' || c === '\u3000') { + // special white spaces (like non breaking space) + chars.push(' ') + i++ + } else if (c === quote) { parseString(quote); } diff --git a/test/util.test.js b/test/util.test.js index 8abbf6b..92fa63a 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -32,6 +32,11 @@ describe('util', function () { assert.equal(util.sanitize('"foo\\\'bar"'), '"foo\'bar"'); }); + it('should replace special white characters', function () { + assert.equal(util.sanitize('{"a":\u00a0"foo\u00a0bar"}'), '{"a": "foo\u00a0bar"}'); + assert.equal(util.sanitize('{"a":\u2009"foo"}'), '{"a": "foo"}'); + }); + it('should escape unescaped control characters', function () { assert.equal(util.sanitize('"hello\bworld"'), '"hello\\bworld"') assert.equal(util.sanitize('"hello\fworld"'), '"hello\\fworld"') @@ -41,7 +46,7 @@ describe('util', function () { assert.equal(util.sanitize('{"value\n": "dc=hcm,dc=com"}'), '{"value\\n": "dc=hcm,dc=com"}') }) - it.only('should replace left/right quotes', function () { + it('should replace left/right quotes', function () { assert.equal(util.sanitize('\u2018foo\u2019'), '"foo"') assert.equal(util.sanitize('\u201Cfoo\u201D'), '"foo"') assert.equal(util.sanitize('\u0060foo\u00B4'), '"foo"')