-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix string serialization - Fix dictionary key serialization
- Loading branch information
Showing
9 changed files
with
156 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,30 @@ | ||
export function serializeChar(str: string) { | ||
const SAFE_SERIALIZE_CHECK = /<|\u2028|\u2029/; | ||
|
||
export function serializeChar(str: string): string { | ||
switch (str) { | ||
case '"': return '\\"'; | ||
case '\\': return '\\\\'; | ||
case '<': return '\\x3C'; | ||
case '\n': return '\\n'; | ||
case '\r': return '\\r'; | ||
case '\u2028': return '\\u2028'; | ||
case '\u2029': return '\\u2029'; | ||
default: return undefined; | ||
case '<': | ||
return '\\x3C'; | ||
case '\u2028': | ||
return '\\u2028'; | ||
case '\u2029': | ||
return '\\u2029'; | ||
default: | ||
return str; | ||
} | ||
} | ||
|
||
// Written by https://github.com/DylanPiercey and is distributed under the MIT license. | ||
// Creates a JavaScript double quoted string and escapes all characters | ||
// not listed as DoubleStringCharacters on | ||
// Also includes "<" to escape "</script>" and "\" to avoid invalid escapes in the output. | ||
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 | ||
export function serializeString(str: string) { | ||
let result = ''; | ||
let lastPos = 0; | ||
let replacement: string | undefined; | ||
for (let i = 0, len = str.length; i < len; i++) { | ||
replacement = serializeChar(str[i]); | ||
if (replacement) { | ||
result += str.slice(lastPos, i) + replacement; | ||
lastPos = i + 1; | ||
} | ||
} | ||
if (lastPos === 0) { | ||
result = str; | ||
} else { | ||
result += str.slice(lastPos); | ||
const safe = JSON.stringify(str); | ||
const unquoted = safe.substring(1, safe.length - 1); | ||
if (SAFE_SERIALIZE_CHECK.test(unquoted)) { | ||
return unquoted.replace(/<|\u2028|\u2029/g, serializeChar); | ||
} | ||
return result; | ||
return unquoted; | ||
} | ||
|
||
export function deserializeString(str: string): string { | ||
return str | ||
.replace(/\\"/g, '"') | ||
.replace(/\\\\/g, '\\') | ||
.replace(/\\x3C/g, '<') | ||
.replace(/\\n/g, '\n') | ||
.replace(/\\r/g, '\r') | ||
const restored = str.replace(/\\x3C/g, '<') | ||
.replace(/\\u2028/g, '\u2028') | ||
.replace(/\\u2029/g, '\u2029'); | ||
return JSON.parse('"' + restored + '"') as string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.