diff --git a/index.js b/index.js index 065b4d6..86a34e6 100644 --- a/index.js +++ b/index.js @@ -161,7 +161,7 @@ function base64Decode(x) { // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-quotejsonstring // DO: 2.a -> 2.b -> 2.c -> 2.d var escapeJavaScriptTable = { - '"': '\"', // 2.a + '"': '\\"', // 2.a '\\': '\\\\', '\b': '\\b', // 2.b (skip abbrev) '\f': '\\f', diff --git a/test/util.js b/test/util.js index b0b6dac..2c03143 100644 --- a/test/util.js +++ b/test/util.js @@ -3,11 +3,25 @@ var mappingTemplate = require('../'); describe('$util', function() { describe('.escapeJavaScript()', function() { - it ('escapes as javascript string', function() { - var template = '$util.escapeJavaScript($input.path(\'$\'))'; - var result = mappingTemplate({template: template, payload: 'bo"dy'}); - assert.equal(result, 'bo\"dy'); + var template = '$util.escapeJavaScript($input.path(\'$\'))'; + var result = mappingTemplate({template: template, payload: 'bo"dy'}); + it (`escapes as javascript string - simple - ${result}`, function() { + assert.equal(result, 'bo\\"dy'); }); + var doc = `{"foo":"${result}"}`; + it (`escapes as javascript string - parse stringify doc - ${doc}`, function() { + // this truly tests whether it is embeddable + var p1 = JSON.parse(doc); // this will fail if not escaped properly + var s1 = JSON.stringify(p1); + assert.equal(s1, doc); + }); + it (`escapes as javascript string - stringify parse doc - ${doc}`, function() { + // this is tautological - it should work always + var s2 = JSON.stringify(doc); + var p2 = JSON.parse(s2); + assert.equal(p2, doc); + }); + }); describe('.urlEncode()', function() { it ('encodes to url', function() {