Update utils.common.quote to escape unicode characters and inner escape sequences

This commit is contained in:
pyoor 2017-06-14 14:04:53 -04:00
parent 0bfe796364
commit fba49412c7

View file

@ -26,7 +26,24 @@ utils.common = {
return list return list
}, },
quote: function (obj) { quote: function (obj) {
return JSON.stringify(obj) function escapeString (s) {
let escaped = s.replace(/\\/g, '\\\\')
.replace(/\"/g, '\\\"') // eslint-disable-line no-useless-escape
.replace(/\0/g, '\\0')
.replace(/\n/g, '\\n')
.replace(/[^\0-~]/g, function (ch) {
return '\\u' + ('000' + ch.charCodeAt(0).toString(16)).slice(-4)
})
return '\"' + escaped + '\"' // eslint-disable-line no-useless-escape
}
if (typeof s === 'string') {
return escapeString(obj)
} else {
// For other things (such as numbers, |null|, and |undefined|), stringify
return JSON.stringify(obj)
}
}, },
b64encode: function (str) { b64encode: function (str) {
// Unicode safe b64 encoding // Unicode safe b64 encoding