Skip to content

Commit

Permalink
Fix bug where escapes were escaping too much.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Murdoch committed Jun 8, 2012
1 parent a08c17a commit 952527f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
35 changes: 27 additions & 8 deletions lib/parser.js
Expand Up @@ -241,19 +241,34 @@ Parser.prototype = {
// advance up to the next @ sign
var sliced = this.input.slice( this.position ),
match = this.rtag.exec( sliced ),
pos, str, future;
pos, str, future,
advance = false;

// if we have a match, and the number of this.tags preceding it does not escape the match
// we have a real match!
if( match && (match[2] === undefined || match[2].length % 2 === 0)){
// our regex can match the end of the string
pos = match.index + (match[1]==="" ? 0 : 1);
if( match ){
if((match[2] === undefined || match[2].length % 2 === 0)){
// our regex can match the end of the string
pos = match.index + (match[1]==="" ? 0 : 1);

// append any characters we are skipping over to the output
str = sliced.slice( 0, pos );
pos++;
// append any characters we are skipping over to the output
str = sliced.slice( 0, pos );
pos++;

future = sliced.slice( pos );
future = sliced.slice( pos );
}
else{
// that @ was a false alarm. tell this function to advance again.
advance = true;

pos = match.index + match[0].length;

// we are at the end of the input so we add
// the remaining text to the output
str = sliced.slice( 0, pos ).replace(new RegExp(this.tag + this.tag, "g"), this.tag);

future = sliced.slice( pos );
}
}
else {
// we are at the end of the input so we add
Expand All @@ -269,6 +284,10 @@ Parser.prototype = {
this.position += pos;
this.push( "'" + str.replace(/\n/g, "\\n").replace(/'/g, "\\'") + "'", true );
this.future = future;

if(advance){
this.advance();
}
},
"next": function() {
// go to the next position
Expand Down
4 changes: 2 additions & 2 deletions test.js
Expand Up @@ -12,7 +12,7 @@ walker.on("file", function(root, fileStats, next){
},
"tests": "./test/" + fileStats.name
});

next();
});

Expand All @@ -28,5 +28,5 @@ walker.on("end", function(){
console.dir(report);
}
});

});
14 changes: 9 additions & 5 deletions test/razor.js
Expand Up @@ -6,7 +6,7 @@ module("Razor");

test("test razor.js functions", function() {

expect(15);
expect(16);

var compile, object = {};
try{
Expand Down Expand Up @@ -92,14 +92,18 @@ test("test razor.js functions", function() {
equal(compile(object), "@works", "Complex @ escape");


compile = Razor.compile("@@@@@@@@hello");
compile = Razor.compile("@@@@@@@@escaped");
object = {};
equal(compile(object), "@@@@hello", "Multiple @ escapes");
equal(compile(object), "@@@@escaped", "Multiple @ escapes");


compile = Razor.compile("@if(true){<span>@@@@hello</span>}");
compile = Razor.compile("@if(true){<span>@@@@escaped</span>}");
object = {};
equal(compile(object), "<span>@@hello</span>", "Nested @ escapes in blocks");
equal(compile(object), "<span>@@escaped</span>", "Nested @ escapes in blocks");

compile = Razor.compile("email@@escaped.com value='@foo'");
object = {"foo":"b@r"};
equal(compile(object), "email@escaped.com value='b@r'", "Simple @ escape with additional vars");

}
catch(e){
Expand Down

0 comments on commit 952527f

Please sign in to comment.