Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix escapeJavaScript for quotes: double escape the double quote #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
22 changes: 18 additions & 4 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down