Skip to content

Commit

Permalink
Fix issue #6 and another issue where backticked text within regular s…
Browse files Browse the repository at this point in the history
…trings would be incorrectly parsed. Bump to 0.3.6
  • Loading branch information
padolsey committed Jun 10, 2013
1 parent b0b357f commit ed453fd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.5
0.3.6
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 12 additions & 6 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@

// 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)]
}

// 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);
Expand Down Expand Up @@ -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"
Expand Down
19 changes: 10 additions & 9 deletions src/siml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('</' + this.tag + '>');

output.push('</' + this.tag + '>');
}

if (this.multiplier > 1) {
var all = output.join('');
Expand Down
16 changes: 16 additions & 0 deletions test/DefaultGeneratorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ describe('DefaultParser: HTML Generation', function() {
it('Does not escape backticked attribute values', function() {
expect('d { id: `<<` }').toGenerate('<d id="<<"></d>');
});
it('Does not escape/parse backticked strings within regular quoted strings', function() {
expect('"`ok` .. `ok2`"').toGenerate('`ok` .. `ok2`');
});
});

describe('Significant whitespace', function() {
Expand Down Expand Up @@ -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([
'<a href="f">ok</a>',
'<a href="f">ok</a>',
'<a href="f">ok</a>'
].join(''));
});
it('Should work on singular tags', function() {
expect('br:2').toGenerate('<br/><br/>');
});
});

});

0 comments on commit ed453fd

Please sign in to comment.