Add decorator parsing
This commit is contained in:
@@ -120,6 +120,33 @@ interface CommentStatement <: Statement {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
```java
|
||||
interface Decorator <: Statement {
|
||||
type: "Decorator";
|
||||
|
||||
path: PathExpression | Literal;
|
||||
params: [ Expression ];
|
||||
hash: Hash;
|
||||
|
||||
strip: StripFlags | null;
|
||||
}
|
||||
|
||||
interface DecoratorBlock <: Statement {
|
||||
type: "DecoratorBlock";
|
||||
path: PathExpression | Literal;
|
||||
params: [ Expression ];
|
||||
hash: Hash;
|
||||
|
||||
program: Program | null;
|
||||
|
||||
openStrip: StripFlags | null;
|
||||
closeStrip: StripFlags | null;
|
||||
}
|
||||
```
|
||||
|
||||
Decorator paths only utilize the `path.original` value and as a consequence do not support depthed evaluation.
|
||||
|
||||
### Expressions
|
||||
|
||||
```java
|
||||
|
||||
@@ -84,8 +84,9 @@ export function prepareMustache(path, params, hash, open, strip, locInfo) {
|
||||
let escapeFlag = open.charAt(3) || open.charAt(2),
|
||||
escaped = escapeFlag !== '{' && escapeFlag !== '&';
|
||||
|
||||
let decorator = (/\*/.test(open));
|
||||
return {
|
||||
type: 'MustacheStatement',
|
||||
type: decorator ? 'Decorator' : 'MustacheStatement',
|
||||
path,
|
||||
params,
|
||||
hash,
|
||||
@@ -124,12 +125,18 @@ export function prepareBlock(openBlock, program, inverseAndProgram, close, inver
|
||||
validateClose(openBlock, close);
|
||||
}
|
||||
|
||||
let decorator = (/\*/.test(openBlock.open));
|
||||
|
||||
program.blockParams = openBlock.blockParams;
|
||||
|
||||
let inverse,
|
||||
inverseStrip;
|
||||
|
||||
if (inverseAndProgram) {
|
||||
if (decorator) {
|
||||
throw new Exception('Unexpected inverse block on decorator', inverseAndProgram);
|
||||
}
|
||||
|
||||
if (inverseAndProgram.chain) {
|
||||
inverseAndProgram.program.body[0].closeStrip = close.strip;
|
||||
}
|
||||
@@ -145,7 +152,7 @@ export function prepareBlock(openBlock, program, inverseAndProgram, close, inver
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'BlockStatement',
|
||||
type: decorator ? 'DecoratorBlock' : 'BlockStatement',
|
||||
path: openBlock.path,
|
||||
params: openBlock.params,
|
||||
hash: openBlock.hash,
|
||||
|
||||
@@ -48,11 +48,15 @@ PrintVisitor.prototype.Program = function(program) {
|
||||
PrintVisitor.prototype.MustacheStatement = function(mustache) {
|
||||
return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
|
||||
};
|
||||
PrintVisitor.prototype.Decorator = function(mustache) {
|
||||
return this.pad('{{ DIRECTIVE ' + this.SubExpression(mustache) + ' }}');
|
||||
};
|
||||
|
||||
PrintVisitor.prototype.BlockStatement = function(block) {
|
||||
PrintVisitor.prototype.BlockStatement =
|
||||
PrintVisitor.prototype.DecoratorBlock = function(block) {
|
||||
let out = '';
|
||||
|
||||
out += this.pad('BLOCK:');
|
||||
out += this.pad((block.type === 'DecoratorBlock' ? 'DIRECTIVE ' : '') + 'BLOCK:');
|
||||
this.padding++;
|
||||
out += this.pad(this.SubExpression(block));
|
||||
if (block.program) {
|
||||
|
||||
@@ -76,8 +76,10 @@ Visitor.prototype = {
|
||||
},
|
||||
|
||||
MustacheStatement: visitSubExpression,
|
||||
Decorator: visitSubExpression,
|
||||
|
||||
BlockStatement: visitBlock,
|
||||
DecoratorBlock: visitBlock,
|
||||
|
||||
PartialStatement: visitPartial,
|
||||
PartialBlockStatement: function(partial) {
|
||||
|
||||
@@ -63,6 +63,7 @@ WhitespaceControl.prototype.Program = function(program) {
|
||||
};
|
||||
|
||||
WhitespaceControl.prototype.BlockStatement =
|
||||
WhitespaceControl.prototype.DecoratorBlock =
|
||||
WhitespaceControl.prototype.PartialBlockStatement = function(block) {
|
||||
this.accept(block.program);
|
||||
this.accept(block.inverse);
|
||||
@@ -124,6 +125,7 @@ WhitespaceControl.prototype.PartialBlockStatement = function(block) {
|
||||
return strip;
|
||||
};
|
||||
|
||||
WhitespaceControl.prototype.Decorator =
|
||||
WhitespaceControl.prototype.MustacheStatement = function(mustache) {
|
||||
return mustache.strip;
|
||||
};
|
||||
|
||||
@@ -247,6 +247,20 @@ describe('parser', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('directives', function() {
|
||||
it('should parse block directives', function() {
|
||||
equals(astFor('{{#* foo}}{{/foo}}'), 'DIRECTIVE BLOCK:\n PATH:foo []\n PROGRAM:\n');
|
||||
});
|
||||
it('should parse directives', function() {
|
||||
equals(astFor('{{* foo}}'), '{{ DIRECTIVE PATH:foo [] }}\n');
|
||||
});
|
||||
it('should fail if directives have inverse', function() {
|
||||
shouldThrow(function() {
|
||||
astFor('{{#* foo}}{{^}}{{/foo}}');
|
||||
}, Error, /Unexpected inverse/);
|
||||
});
|
||||
});
|
||||
|
||||
it('GH1024 - should track program location properly', function() {
|
||||
var p = Handlebars.parse('\n'
|
||||
+ ' {{#if foo}}\n'
|
||||
|
||||
@@ -241,6 +241,15 @@ describe('Tokenizer', function() {
|
||||
shouldMatchTokens(result, ['OPEN_BLOCK', 'ID', 'CLOSE', 'CONTENT', 'OPEN_ENDBLOCK', 'ID', 'CLOSE']);
|
||||
});
|
||||
|
||||
it('tokenizes directives', function() {
|
||||
shouldMatchTokens(
|
||||
tokenize('{{#*foo}}content{{/foo}}'),
|
||||
['OPEN_BLOCK', 'ID', 'CLOSE', 'CONTENT', 'OPEN_ENDBLOCK', 'ID', 'CLOSE']);
|
||||
shouldMatchTokens(
|
||||
tokenize('{{*foo}}'),
|
||||
['OPEN', 'ID', 'CLOSE']);
|
||||
});
|
||||
|
||||
it('tokenizes inverse sections as "INVERSE"', function() {
|
||||
shouldMatchTokens(tokenize('{{^}}'), ['INVERSE']);
|
||||
shouldMatchTokens(tokenize('{{else}}'), ['INVERSE']);
|
||||
|
||||
@@ -9,6 +9,8 @@ describe('Visitor', function() {
|
||||
var visitor = new Handlebars.Visitor();
|
||||
visitor.accept(Handlebars.parse('{{foo}}{{#foo (bar 1 "1" true undefined null) foo=@data}}{{!comment}}{{> bar }} {{/foo}}'));
|
||||
visitor.accept(Handlebars.parse('{{#> bar }} {{/bar}}'));
|
||||
visitor.accept(Handlebars.parse('{{#* bar }} {{/bar}}'));
|
||||
visitor.accept(Handlebars.parse('{{* bar }}'));
|
||||
});
|
||||
|
||||
it('should traverse to stubs', function() {
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
|
||||
}
|
||||
<mu>"{{"{LEFT_STRIP}?">" return 'OPEN_PARTIAL';
|
||||
<mu>"{{"{LEFT_STRIP}?"#>" return 'OPEN_PARTIAL_BLOCK';
|
||||
<mu>"{{"{LEFT_STRIP}?"#" return 'OPEN_BLOCK';
|
||||
<mu>"{{"{LEFT_STRIP}?"#""*"? return 'OPEN_BLOCK';
|
||||
<mu>"{{"{LEFT_STRIP}?"/" return 'OPEN_ENDBLOCK';
|
||||
<mu>"{{"{LEFT_STRIP}?"^"\s*{RIGHT_STRIP}?"}}" this.popState(); return 'INVERSE';
|
||||
<mu>"{{"{LEFT_STRIP}?\s*"else"\s*{RIGHT_STRIP}?"}}" this.popState(); return 'INVERSE';
|
||||
@@ -98,7 +98,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
|
||||
this.popState();
|
||||
return 'COMMENT';
|
||||
}
|
||||
<mu>"{{"{LEFT_STRIP}? return 'OPEN';
|
||||
<mu>"{{"{LEFT_STRIP}?"*"? return 'OPEN';
|
||||
|
||||
<mu>"=" return 'EQUALS';
|
||||
<mu>".." return 'ID';
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ block
|
||||
;
|
||||
|
||||
openBlock
|
||||
: OPEN_BLOCK helperName param* hash? blockParams? CLOSE -> { path: $2, params: $3, hash: $4, blockParams: $5, strip: yy.stripFlags($1, $6) }
|
||||
: OPEN_BLOCK helperName param* hash? blockParams? CLOSE -> { open: $1, path: $2, params: $3, hash: $4, blockParams: $5, strip: yy.stripFlags($1, $6) }
|
||||
;
|
||||
|
||||
openInverse
|
||||
|
||||
Reference in New Issue
Block a user