Skip to content

Commit

Permalink
fix: #131 backslash character not being escaped when `escapeControlCh…
Browse files Browse the repository at this point in the history
…aracters: true` (#133)
  • Loading branch information
josdejong authored Aug 29, 2022
1 parent 224e5ca commit 1657d9a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/lib/utils/domUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
})
Expand All @@ -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')
Expand All @@ -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')
Expand Down
11 changes: 9 additions & 2 deletions src/lib/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand All @@ -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
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/routes/development.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit 1657d9a

Please sign in to comment.