Make the ID node capable of handling paths

TODO: handle invalid paths in the tokenizer or parser
  TODO: invalidate "{{ foo/ bar }}"
This commit is contained in:
wycats
2010-11-25 16:12:50 -08:00
parent 7d0204bf9f
commit f8b245896d
5 changed files with 31 additions and 10 deletions
+6 -1
View File
@@ -33,6 +33,11 @@ Handlebars.HandlebarsLexer.prototype.lex = function() {
if(lookahead === "") return;
if(this.state == "MUSTACHE") {
if(this.peek() === "/") {
this.getchar();
return "SEP";
}
// chomp optional whitespace
while(this.peek() === " ") { this.ignorechar(); }
@@ -64,7 +69,7 @@ Handlebars.HandlebarsLexer.prototype.lex = function() {
// All other cases are IDs or errors
} else {
// grab alphanumeric characters
while(this.peek().match(/[0-9A-Za-z]/)) { this.getchar() }
while(this.peek().match(/[0-9A-Za-z\.]/)) { this.getchar() }
// if any characters were grabbed => ID
if(this.yytext.length) { return "ID" }
+6 -1
View File
@@ -79,7 +79,12 @@ Handlebars.PrintVisitor.prototype.STRING = function(string) {
};
Handlebars.PrintVisitor.prototype.ID = function(id) {
return "ID:" + id.id;
var path = id.id.join("/");
if(id.id.length > 1) {
return "PATH:" + path;
} else {
return "ID:" + path;
}
};
Handlebars.PrintVisitor.prototype.content = function(content) {
+6
View File
@@ -36,6 +36,12 @@ describe "Tokenizer" do
result[1].should be_token("ID", "foo")
end
it "tokenizes a path as 'OPEN ID CLOSE'" do
result = tokenize("{{../foo/bar}}")
result.should match_tokens(%w(OPEN ID SEP ID SEP ID CLOSE))
result[1].should be_token("ID", "..")
end
it "tokenizes a simple mustahe with spaces as 'OPEN ID CLOSE'" do
result = tokenize("{{ foo }}")
result.should match_tokens(%w(OPEN ID CLOSE))
+12 -7
View File
@@ -29,7 +29,7 @@ openBlock
;
closeBlock
: OPEN_ENDBLOCK id CLOSE { }
: OPEN_ENDBLOCK path CLOSE { }
;
mustache
@@ -37,7 +37,7 @@ mustache
;
partial
: OPEN_PARTIAL id CLOSE { $$ = new yy.PartialNode($2) }
: OPEN_PARTIAL path CLOSE { $$ = new yy.PartialNode($2) }
;
simpleInverse
@@ -45,8 +45,8 @@ simpleInverse
;
inMustache
: id params { $$ = [$1].concat($2) }
| id { $$ = [$1] }
: path params { $$ = [$1].concat($2) }
| path { $$ = [$1] }
;
params
@@ -55,11 +55,16 @@ params
;
param
: id { $$ = $1 }
: path { $$ = $1 }
| STRING { $$ = new yy.StringNode($1) }
;
id
: ID { $$ = new yy.IdNode($1) }
path
: pathSegments { $$ = new yy.IdNode($1) }
;
pathSegments
: pathSegments SEP ID { $1.push($3); $$ = $1; }
| ID { $$ = [$1] }
;
+1 -1
View File
@@ -2,7 +2,7 @@ require.paths.push(__dirname + "/lib");
var Handlebars = require("handlebars").Handlebars;
var string = "foo {{ bar baz \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"
var string = "foo {{ bar ../baz/bat/bam \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"
var ast = Handlebars.parse(string);
require("sys").print(Handlebars.print(ast));