From bf67f2e3939d97ba2f0585fc4f0729611e3a5022 Mon Sep 17 00:00:00 2001 From: James Padolsey Date: Sun, 9 Jun 2013 22:14:10 +0100 Subject: [PATCH] Fix issue #5 --- src/parser.pegjs | 16 +++++++++------- test/DefaultGeneratorSpec.js | 3 +++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index 9bebb2f..92e8c67 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -25,22 +25,24 @@ function resolveStringToken(tok) { return stringTokens[tok.substring('%%__STRING_TOKEN___%%'.length)] } + + // Replace HTML with string tokens first + input = input.replace(/(`+)((?:\\\1|[^\1])*?)\1/g, function($0, $1, $2) { + return '%%__HTML_TOKEN___%%' + (stringTokens.push( + $2.replace(/\\`/g, '\`') + ) - 1); + }); + input = input.replace(/(["'])((?:\\\1|[^\1])*?)\1/g, function($0, $1, $2) { return '%%__STRING_TOKEN___%%' + (stringTokens.push( $2.replace(/\\'/g, '\'').replace(/\\"/g, '"') ) - 1); }); + input = input.replace(/(^|\n)\s*\\([^\n\r]+)/g, function($0, $1, $2) { return $1 + '%%__STRING_TOKEN___%%' + (stringTokens.push($2) - 1); }); - // Replace HTML with string tokens too - input = input.replace(/(`+)((?:\\\1|[^\1])*?)\1/g, function($0, $1, $2) { - return '%%__HTML_TOKEN___%%' + (stringTokens.push( - $2.replace(/\\`/g, '\`') - ) - 1); - }); - var isCurly = /\/\*\s*siml:curly=true\s*\*\//i.test(input); // Remove comments: diff --git a/test/DefaultGeneratorSpec.js b/test/DefaultGeneratorSpec.js index 45ccfeb..d3af393 100644 --- a/test/DefaultGeneratorSpec.js +++ b/test/DefaultGeneratorSpec.js @@ -294,6 +294,9 @@ describe('DefaultParser: HTML Generation', function() { it('Does not escape backticked text', function() { expect('`O < K`').toGenerate('O < K'); }); + it('Quotes strings are not parsed within backticks', function() { + expect('` "this" and \'this ... \' ..`').toGenerate(' "this" and \'this ... \' ..'); + }); it('Does not escape backticked attribute values', function() { expect('d { id: `<<` }').toGenerate(''); });