diff --git a/src/lib/utils/domUtils.test.ts b/src/lib/utils/domUtils.test.ts index bcb84443..0c45004d 100644 --- a/src/lib/utils/domUtils.test.ts +++ b/src/lib/utils/domUtils.test.ts @@ -45,12 +45,15 @@ describe('domUtils', () => { strictEqual(escapeValue('greeting'), 'greeting') strictEqual(escapeValue('hello\nworld'), 'hello\nworld') + strictEqual(escapeValue('hello\\nworld'), 'hello\\nworld') + strictEqual(escapeValue('back\\slash'), 'back\\slash') strictEqual(escapeValue('😀'), '😀') strictEqual(escapeValue('\ud83d\ude00'), '\ud83d\ude00') strictEqual(unescapeValue('greeting'), 'greeting') - strictEqual(escapeValue('hello\nworld'), 'hello\nworld') - strictEqual(escapeValue('hello\\nworld'), 'hello\\nworld') + strictEqual(unescapeValue('hello\nworld'), 'hello\nworld') + strictEqual(unescapeValue('hello\\nworld'), 'hello\\nworld') + strictEqual(unescapeValue('back\\slash'), 'back\\slash') strictEqual(unescapeValue('😀'), '😀') strictEqual(unescapeValue('\ud83d\ude00'), '\ud83d\ude00') strictEqual(unescapeValue('\\ud83d\\ude00'), '\\ud83d\\ude00') @@ -64,11 +67,13 @@ describe('domUtils', () => { strictEqual(escapeValue('greeting'), 'greeting') strictEqual(escapeValue('hello\nworld'), 'hello\\nworld') + strictEqual(escapeValue('back\\slash'), 'back\\\\slash') strictEqual(escapeValue('😀'), '😀') strictEqual(escapeValue('\ud83d\ude00'), '\ud83d\ude00') strictEqual(unescapeValue('greeting'), 'greeting') strictEqual(unescapeValue('hello\\nworld'), 'hello\nworld') + strictEqual(unescapeValue('back\\\\slash'), 'back\\slash') strictEqual(unescapeValue('\\ud83d\\ude00'), '\\ud83d\\ude00') strictEqual(unescapeValue('\ud83d\ude00'), '\ud83d\ude00') }) @@ -81,11 +86,13 @@ describe('domUtils', () => { strictEqual(escapeValue('greeting'), 'greeting') strictEqual(escapeValue('hello\nworld'), 'hello\nworld') + strictEqual(escapeValue('back\\slash'), 'back\\slash') strictEqual(escapeValue('😀'), '\\ud83d\\ude00') strictEqual(escapeValue('\ud83d\ude00'), '\\ud83d\\ude00') strictEqual(unescapeValue('greeting'), 'greeting') strictEqual(unescapeValue('hello\\nworld'), 'hello\\nworld') + strictEqual(unescapeValue('back\\\\slash'), 'back\\\\slash') strictEqual(unescapeValue('\\ud83d\\ude00'), '😀') strictEqual(unescapeValue('\ud83d\ude00'), '\ud83d\ude00') strictEqual(unescapeValue('\\ud83d\\ude00'), '\ud83d\ude00') @@ -99,11 +106,13 @@ describe('domUtils', () => { strictEqual(escapeValue('greeting'), 'greeting') strictEqual(escapeValue('hello\nworld'), 'hello\\nworld') + strictEqual(escapeValue('back\\slash'), 'back\\\\slash') strictEqual(escapeValue('😀'), '\\ud83d\\ude00') strictEqual(escapeValue('\ud83d\ude00'), '\\ud83d\\ude00') strictEqual(unescapeValue('greeting'), 'greeting') strictEqual(unescapeValue('hello\\nworld'), 'hello\nworld') + strictEqual(unescapeValue('back\\\\slash'), 'back\\slash') strictEqual(unescapeValue('\\ud83d\\ude00'), '😀') strictEqual(unescapeValue('\ud83d\ude00'), '\ud83d\ude00') strictEqual(unescapeValue('\\ud83d\\ude00'), '\ud83d\ude00') diff --git a/src/lib/utils/domUtils.ts b/src/lib/utils/domUtils.ts index e71608da..8b69ef51 100644 --- a/src/lib/utils/domUtils.ts +++ b/src/lib/utils/domUtils.ts @@ -77,26 +77,33 @@ export function jsonUnescapeUnicode(value) { } const controlCharacters = { + '\\': '\\\\', + // escaped forward slash '\/' is the same as '/', we can't escape/unescape it '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t' + // unicode is handled separately } const escapedControlCharacters = { + '\\\\': '\\', + // escaped forward slash '\/' is the same as '/', we can't escape/unescape it + '\\/': '/', '\\b': '\b', '\\f': '\f', '\\n': '\n', '\\r': '\r', '\\t': '\t' + // unicode is handled separately } /** * @param {string} value */ export function jsonEscapeControl(value) { - return value.replace(/[\b\f\n\r\t]/g, (x) => { + return value.replace(/[\b\f\n\r\t\\]/g, (x) => { return controlCharacters[x] || x }) } @@ -105,7 +112,7 @@ export function jsonEscapeControl(value) { * @param {string} value */ export function jsonUnescapeControl(value) { - return value.replace(/\\[bfnrt]/g, (x) => { + return value.replace(/\\[bfnrt\\]/g, (x) => { return escapedControlCharacters[x] || x }) } diff --git a/src/routes/development.svelte b/src/routes/development.svelte index 124c9da8..0c157645 100644 --- a/src/routes/development.svelte +++ b/src/routes/development.svelte @@ -36,6 +36,9 @@ "stringContainingNumber": "1234", "multi\\nline text": "Hello\\nWorld text", "tab": "Hello\\tTab", + "backslash": "back\\\\slash", + "forwardslash": "forward\\/slash", + "quote": "quote\\"", "timestamp": 1534952749890, "url": "https://jsoneditoronline.org", "array": [