From f3313158dbc2364b89ee9cecae8142b44619e16c Mon Sep 17 00:00:00 2001 From: jos Date: Mon, 18 Sep 2017 21:05:26 +0200 Subject: [PATCH] Fixed issue with escaping spaces --- src/utils/stringUtils.js | 20 +++++++++++--------- test/eson.test.js | 4 ++-- test/stringUtils.test.js | 22 +++++++++++++++++++--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index 93dfb1c..e473b5c 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -11,17 +11,19 @@ export function escapeHTML (text, escapeUnicode = false) { return String(text) } else { - var htmlEscaped = String(text) - .replace(/ /g, ' \u00a0') // replace double space with an nbsp and space - .replace(/^ /, '\u00a0') // space at start - .replace(/ $/, '\u00a0') // space at end - - var json = JSON.stringify(htmlEscaped) - var html = json.substring(1, json.length - 1) + let htmlEscaped = String(text) if (escapeUnicode === true) { - html = escapeUnicodeChars(html) + // FIXME: should not unescape the just created non-breaking spaces \u00A0 ? + htmlEscaped = escapeUnicodeChars(htmlEscaped) } - return html + + htmlEscaped = htmlEscaped + .replace(/ /g, ' \u00A0') // replace double space with an nbsp and space + .replace(/^ /, '\u00A0') // space at start + .replace(/ $/, '\u00A0') // space at end + + const json = JSON.stringify(htmlEscaped) + return json.substring(1, json.length - 1) // remove enclosing double quotes } } diff --git a/test/eson.test.js b/test/eson.test.js index 5691501..7380f37 100644 --- a/test/eson.test.js +++ b/test/eson.test.js @@ -451,8 +451,8 @@ const JSON_DATA_SMALL = { const JSON_SCHEMA_ERRORS = [ - {esonPath: '/obj/arr/2/last', message: 'String expected'}, - {esonPath: '/nill', message: 'Null expected'} + {dataPath: '/obj/arr/2/last', message: 'String expected'}, + {dataPath: '/nill', message: 'Null expected'} ] const JSON_DATA_EXAMPLE_ERRORS = { diff --git a/test/stringUtils.test.js b/test/stringUtils.test.js index 859bef6..f14af22 100644 --- a/test/stringUtils.test.js +++ b/test/stringUtils.test.js @@ -1,10 +1,26 @@ import test from 'ava'; -import { findUniqueName } from '../src/utils/stringUtils' +import { escapeHTML, unescapeHTML, findUniqueName } from '../src/utils/stringUtils' + +test('escapeHTML', t => { + t.is(escapeHTML(' hello '), '\u00A0\u00A0 hello \u00A0') + t.is(escapeHTML('\u00A0 hello'), '\u00A0 hello') + t.is(escapeHTML('hello\nworld'), 'hello\\nworld') + + // TODO: test escapeHTML more thorougly +}) + +test('unescapeHTML', t => { + t.is(unescapeHTML(' \u00A0 hello \u00A0'), ' hello ') + t.is(unescapeHTML('\u00A0 hello'), ' hello') + + t.is(unescapeHTML('hello\\nworld'), 'hello\nworld') + + // TODO: test unescapeHTML more thorougly +}) test('findUniqueName', t => { - t.is(findUniqueName('other', ['a', 'b', 'c']), 'other') t.is(findUniqueName('b', ['a', 'b', 'c']), 'b (copy)') t.is(findUniqueName('b', ['a', 'b', 'c', 'b (copy)']), 'b (copy 2)') t.is(findUniqueName('b', ['a', 'b', 'c', 'b (copy)', 'b (copy 2)']), 'b (copy 3)') -}) \ No newline at end of file +})