From ed453fd2c7c0fa978f1d393fb8ecbf7535bac1dd Mon Sep 17 00:00:00 2001 From: James Padolsey Date: Mon, 10 Jun 2013 10:15:32 +0100 Subject: [PATCH] Fix issue #6 and another issue where backticked text within regular strings would be incorrectly parsed. Bump to 0.3.6 --- VERSION | 2 +- package.json | 2 +- readme.md | 3 +++ src/parser.pegjs | 18 ++++++++++++------ src/siml.js | 19 ++++++++++--------- test/DefaultGeneratorSpec.js | 16 ++++++++++++++++ 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/VERSION b/VERSION index 09e9157..53b61ec 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.5 \ No newline at end of file +0.3.6 \ No newline at end of file diff --git a/package.json b/package.json index 5e864b6..14d971d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "siml", "title": "SIML", "description": "SIML, Simpified markup inspired by CSS", - "version": "0.3.5", + "version": "0.3.6", "author": "James Padolsey (http://git.io/padolsey)", "main": "dist/siml.all.js", "dependencies": { diff --git a/readme.md b/readme.md index a80e269..6708006 100644 --- a/readme.md +++ b/readme.md @@ -274,3 +274,6 @@ More to come... * Fixed issue where attribute/directive values did not support backtick (no-escape) quotes. See [Issue #4](https://github.com/padolsey/SIML/issues/4) * 0.3.5 * Fixed using quoted strings in 'heredoc' blocks. See [Issue #5](https://github.com/padolsey/SIML/issues/5) + * 0.3.6 + * Fixed issue where singular tags would not multiply (`selector:n`). See [Issue #6](https://github.com/padolsey/SIML/issues/6) + * Fixed issue where backtick quotes within regular quotes would get parsed and result in `%HTML_TOKEN%` tokens in the output. diff --git a/src/parser.pegjs b/src/parser.pegjs index 92e8c67..60492cd 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -21,7 +21,9 @@ // Replace all strings with recoverable string tokens: // This is done to make comment-removal possible and safe. - var stringTokens = []; + var stringTokens = [ + // [ 'QUOTE', 'ACTUAL_STRING' ] ... + ]; function resolveStringToken(tok) { return stringTokens[tok.substring('%%__STRING_TOKEN___%%'.length)] } @@ -29,18 +31,18 @@ // 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, $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, $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); + return $1 + '%%__STRING_TOKEN___%%' + (stringTokens.push([$1, $2]) - 1); }); var isCurly = /\/\*\s*siml:curly=true\s*\*\//i.test(input); @@ -463,12 +465,16 @@ value string "String" = '%%__STRING_TOKEN___%%' d:[0-9]+ { - return stringTokens[ d.join('') ]; + // Replace any `...` quotes within the String: + return stringTokens[ d.join('') ][1].replace(/%%__HTML_TOKEN___%%(\d+)/g, function(_, $1) { + var str = stringTokens[ $1 ]; + return str[0] + str[1] + str[0]; + }); } html "HTML" = '%%__HTML_TOKEN___%%' d:[0-9]+ { - return stringTokens[ d.join('') ]; + return stringTokens[ d.join('') ][1]; } simpleString "SimpleString" diff --git a/src/siml.js b/src/siml.js index b779f2f..bb5ed96 100644 --- a/src/siml.js +++ b/src/siml.js @@ -243,18 +243,19 @@ var siml = typeof module != 'undefined' && module.exports ? module.exports : win if (this.isSingular) { output.push('/>'); - return; - } + } else { - output.push('>'); + output.push('>'); - if (content.length) { - isPretty && output.push('\n'); - output.push(content.join(isPretty ? '\n': '')); - isPretty && output.push('\n' + indent); - } + if (content.length) { + isPretty && output.push('\n'); + output.push(content.join(isPretty ? '\n': '')); + isPretty && output.push('\n' + indent); + } + + output.push(''); - output.push(''); + } if (this.multiplier > 1) { var all = output.join(''); diff --git a/test/DefaultGeneratorSpec.js b/test/DefaultGeneratorSpec.js index d3af393..40670ba 100644 --- a/test/DefaultGeneratorSpec.js +++ b/test/DefaultGeneratorSpec.js @@ -300,6 +300,9 @@ describe('DefaultParser: HTML Generation', function() { it('Does not escape backticked attribute values', function() { expect('d { id: `<<` }').toGenerate(''); }); + it('Does not escape/parse backticked strings within regular quoted strings', function() { + expect('"`ok` .. `ok2`"').toGenerate('`ok` .. `ok2`'); + }); }); describe('Significant whitespace', function() { @@ -463,4 +466,17 @@ html\n\ }); }); + describe('Multiplier [SimplerSelector:Number]', function() { + it('Should multiply subject element by N times', function() { + expect('a:3{href:"f";text:"ok"}').toGenerate([ + 'ok', + 'ok', + 'ok' + ].join('')); + }); + it('Should work on singular tags', function() { + expect('br:2').toGenerate('

'); + }); + }); + }); \ No newline at end of file