Fixed issue with escaping spaces

This commit is contained in:
jos 2017-09-18 21:05:26 +02:00
parent 6f9776a07a
commit f3313158db
3 changed files with 32 additions and 14 deletions

View File

@ -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
}
}

View File

@ -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 = {

View File

@ -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)')
})
})