Permalink
Browse files

escape backslashes correctly, fixes #15

  • Loading branch information...
1 parent 6a93f98 commit 78e7df243a64798efc890600d2bd71ade3511861 @jlongster committed Oct 23, 2012
Showing with 7 additions and 2 deletions.
  1. +3 −2 src/compiler.js
  2. +4 −0 tests/compiler.js
View
5 src/compiler.js
@@ -132,7 +132,8 @@ var Compiler = Object.extend({
compileLiteral: function(node, frame) {
if(typeof node.value == "string") {
- var val = node.value.replace(/"/g, '\\"');
+ var val = node.value.replace(/\\/g, '\\\\');
+ val = val.replace(/"/g, '\\"');
val = val.replace(/\n/g, "\\n");
val = val.replace(/\r/g, "\\r");
val = val.replace(/\t/g, "\\t");
@@ -617,7 +618,7 @@ var Compiler = Object.extend({
// var fs = require("fs");
// var c = new Compiler();
-// var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}';
+// var src = 'foo\n \t \\\ efsdfsdkljflkj';
// var ns = parser.parse(src);
// nodes.printNodes(ns);
View
4 tests/compiler.js
@@ -16,6 +16,10 @@ describe('compiler', function() {
s.should.equal('Hello world, James Long, how are you');
});
+ it('should escape newlines', function() {
+ render('foo\\nbar').should.equal('foo\\nbar');
+ });
+
it('should compile references', function() {
var s = render('{{ foo.bar }}',
{ foo: { bar: 'baz' }});

0 comments on commit 78e7df2

Please sign in to comment.