diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 336492d96..f6229e299 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,15 +1,25 @@ import Exception from "../exception"; -export function ProgramNode(statements, inverse) { +export function ProgramNode(statements, inverseStrip, inverse) { this.type = "program"; this.statements = statements; - if(inverse) { this.inverse = new ProgramNode(inverse); } + this.strip = {}; + + if(inverse) { + this.inverse = new ProgramNode(inverse, inverseStrip); + this.strip.right = inverseStrip.left; + } else if (inverseStrip) { + this.strip.left = inverseStrip.right; + } } -export function MustacheNode(rawParams, hash, unescaped) { +export function MustacheNode(rawParams, hash, open, strip) { this.type = "mustache"; - this.escaped = !unescaped; this.hash = hash; + this.strip = strip; + + var escapeFlag = open[3] || open[2]; + this.escaped = escapeFlag !== '{' && escapeFlag !== '&'; var id = this.id = rawParams[0]; var params = this.params = rawParams.slice(1); @@ -28,15 +38,16 @@ export function MustacheNode(rawParams, hash, unescaped) { // pass or at runtime. } -export function PartialNode(partialName, context) { +export function PartialNode(partialName, context, strip) { this.type = "partial"; this.partialName = partialName; this.context = context; + this.strip = strip; } export function BlockNode(mustache, program, inverse, close) { - if(mustache.id.original !== close.original) { - throw new Exception(mustache.id.original + " doesn't match " + close.original); + if(mustache.id.original !== close.path.original) { + throw new Exception(mustache.id.original + " doesn't match " + close.path.original); } this.type = "block"; @@ -44,7 +55,15 @@ export function BlockNode(mustache, program, inverse, close) { this.program = program; this.inverse = inverse; - if (this.inverse && !this.program) { + this.strip = { + left: mustache.strip.left, + right: close.strip.right + }; + + (program || inverse).strip.left = mustache.strip.right; + (inverse || program).strip.right = close.strip.left; + + if (inverse && !program) { this.isInverse = true; } } diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 50195e3ab..4f232ebce 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -72,6 +72,7 @@ Compiler.prototype = { guid: 0, compile: function(program, options) { + this.opcodes = []; this.children = []; this.depths = {list: []}; this.options = options; @@ -93,20 +94,30 @@ Compiler.prototype = { } } - return this.program(program); + return this.accept(program); }, accept: function(node) { - return this[node.type](node); + var strip = node.strip || {}, + ret; + if (strip.left) { + this.opcode('strip'); + } + + ret = this[node.type](node); + + if (strip.right) { + this.opcode('strip'); + } + + return ret; }, program: function(program) { - var statements = program.statements, statement; - this.opcodes = []; + var statements = program.statements; for(var i=0, l=statements.length; i dude~}} ', [{}, {}, {dude: 'bar'}], true, 'foobar'); + shouldCompileToWithPartials('foo {{> dude~}} ', [{}, {}, {dude: 'bar'}], true, 'foo bar'); + shouldCompileToWithPartials('foo {{> dude}} ', [{}, {}, {dude: 'bar'}], true, 'foo bar '); + }); + + it('should only strip whitespace once', function() { + var hash = {foo: 'bar'}; + + shouldCompileTo(' {{~foo~}} {{foo}} {{foo}} ', hash, 'barbar bar '); + }); +}); diff --git a/src/handlebars.l b/src/handlebars.l index 7593189ef..ddb7fe9ca 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -9,6 +9,11 @@ function strip(start, end) { %} +LEFT_STRIP "~" +RIGHT_STRIP "~" + +LOOKAHEAD [=~}\s\/.] +LITERAL_LOOKAHEAD [~}\s] /* ID is the inverse of control characters. @@ -19,7 +24,7 @@ Control characters ranges: [\[-\^`] [, \, ], ^, `, Exceptions in range: _ [\{-~] {, |, }, ~ */ -ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.] +ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} %% @@ -46,30 +51,30 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.] [\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT'; -"{{>" return 'OPEN_PARTIAL'; -"{{#" return 'OPEN_BLOCK'; -"{{/" return 'OPEN_ENDBLOCK'; -"{{^" return 'OPEN_INVERSE'; -"{{"\s*"else" return 'OPEN_INVERSE'; -"{{{" return 'OPEN_UNESCAPED'; -"{{&" return 'OPEN'; +"{{"{LEFT_STRIP}?">" return 'OPEN_PARTIAL'; +"{{"{LEFT_STRIP}?"#" return 'OPEN_BLOCK'; +"{{"{LEFT_STRIP}?"/" return 'OPEN_ENDBLOCK'; +"{{"{LEFT_STRIP}?"^" return 'OPEN_INVERSE'; +"{{"{LEFT_STRIP}?\s*"else" return 'OPEN_INVERSE'; +"{{"{LEFT_STRIP}?"{" return 'OPEN_UNESCAPED'; +"{{"{LEFT_STRIP}?"&" return 'OPEN'; "{{!--" this.popState(); this.begin('com'); "{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT'; -"{{" return 'OPEN'; +"{{"{LEFT_STRIP}? return 'OPEN'; "=" return 'EQUALS'; -"."/[}\/ ] return 'ID'; ".." return 'ID'; +"."/{LOOKAHEAD} return 'ID'; [\/.] return 'SEP'; \s+ /*ignore whitespace*/ -"}}}" this.popState(); return 'CLOSE_UNESCAPED'; -"}}" this.popState(); return 'CLOSE'; +"}"{RIGHT_STRIP}?"}}" this.popState(); return 'CLOSE_UNESCAPED'; +{RIGHT_STRIP}?"}}" this.popState(); return 'CLOSE'; '"'("\\"["]|[^"])*'"' yytext = strip(1,2).replace(/\\"/g,'"'); return 'STRING'; "'"("\\"[']|[^'])*"'" yytext = strip(1,2).replace(/\\'/g,"'"); return 'STRING'; "@" return 'DATA'; -"true"/[}\s] return 'BOOLEAN'; -"false"/[}\s] return 'BOOLEAN'; -\-?[0-9]+/[}\s] return 'INTEGER'; +"true"/{LITERAL_LOOKAHEAD} return 'BOOLEAN'; +"false"/{LITERAL_LOOKAHEAD} return 'BOOLEAN'; +\-?[0-9]+/{LITERAL_LOOKAHEAD} return 'INTEGER'; {ID} return 'ID'; diff --git a/src/handlebars.yy b/src/handlebars.yy index d2f24c45d..0afd2cbbc 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -2,6 +2,17 @@ %ebnf +%{ + +function stripFlags(open, close) { + return { + left: open[2] === '~', + right: close[0] === '~' || close[1] === '~' + }; +} + +%} + %% root @@ -9,9 +20,9 @@ root ; program - : simpleInverse statements -> new yy.ProgramNode([], $2) - | statements simpleInverse statements -> new yy.ProgramNode($1, $3) - | statements simpleInverse -> new yy.ProgramNode($1, []) + : simpleInverse statements -> new yy.ProgramNode([], $1, $2) + | statements simpleInverse statements -> new yy.ProgramNode($1, $2, $3) + | statements simpleInverse -> new yy.ProgramNode($1, $2, []) | statements -> new yy.ProgramNode($1) | simpleInverse -> new yy.ProgramNode([]) | "" -> new yy.ProgramNode([]) @@ -32,32 +43,31 @@ statement ; openBlock - : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1]) + : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; openInverse - : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1]) + : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; closeBlock - : OPEN_ENDBLOCK path CLOSE -> $2 + : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: stripFlags($1, $3)} ; mustache - : OPEN inMustache CLOSE { - // Parsing out the '&' escape token at this level saves ~500 bytes after min due to the removal of one parser node. - $$ = new yy.MustacheNode($2[0], $2[1], $1[2] === '&'); - } - | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], true) + // Parsing out the '&' escape token at AST level saves ~500 bytes after min due to the removal of one parser node. + // This also allows for handler unification as all mustache node instances can utilize the same handler + : OPEN inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) + | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; partial - : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3) + : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3, stripFlags($1, $4)) ; simpleInverse - : OPEN_INVERSE CLOSE { } + : OPEN_INVERSE CLOSE -> stripFlags($1, $2) ; inMustache