Skip to content

Commit

Permalink
feat(ast): add support for self closing tags
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 21, 2017
1 parent cb06a6b commit 016bf48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Ast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const singleLineComment = /({{--.*?--}})/g
class Ast {
constructor (tags, template) {
this._tags = tags
this._blockExpression = new RegExp(`^\\s*\\@(${_.keys(tags).join('|')})(?:\\((.*)\\))?`)
this._blockExpression = new RegExp(`^\\s*\\@(!?)(${_.keys(tags).join('|')})(?:\\((.*)\\))?`)
this._template = template
this._ast = []
this._insideBlockComment = false
Expand All @@ -55,15 +55,17 @@ class Ast {
* @param {String} tag
* @param {String} args
* @param {Number} index
* @param {Boolean} selfClosing
*
* @return {Object}
*
* @private
*/
_tokenForTag (line, tag, args, index) {
_tokenForTag (line, tag, args, index, selfClosing) {
return {
tag,
args,
selfClosing,
childs: [],
body: line,
lineno: index + 1,
Expand Down Expand Up @@ -117,9 +119,9 @@ class Ast {
/**
* Look for opening of a custom tag.
*/
const [, tag, args] = this._blockExpression.exec(line) || []
const [, selfClosing, tag, args] = this._blockExpression.exec(line) || []
if (tag) {
return this._tokenForTag(line, tag, args, index)
return this._tokenForTag(line, tag, args, index, !!selfClosing)
}

/**
Expand Down Expand Up @@ -245,7 +247,7 @@ class Ast {
* then we need to push it to list of opened tags and wait
* for it to close.
*/
if (token.tag && (token.tag === 'comment' || this._tags[token.tag].isBlock === true)) {
if (token.tag && this._tags[token.tag].isBlock === true && !token.selfClosing) {
this._openedTags.push(token)
}

Expand Down
21 changes: 21 additions & 0 deletions test/unit/ast.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,25 @@ test.group('Template Compiler', (group) => {
assert.lengthOf(ast[0].childs[1].childs, 1)
assert.equal(ast[0].childs[2].body.trim(), '</div>')
})

test('should be able to define self closing block tags', (assert) => {
const statement = `
@!yield('foo')
`
const tags = {
yield: {
name: 'yield',
isBlock: true,
fn: function () {}
}
}

const ast = new Ast(tags, statement).parse()
assert.equal(ast[0].tag, 'yield')
assert.deepEqual(ast[0].childs, [])
assert.equal(ast[0].args, `'foo'`)
assert.equal(ast[0].lineno, 1)
assert.equal(ast[0].selfClosing, true)
assert.equal(ast[0].body, `@!yield('foo')`)
})
})

0 comments on commit 016bf48

Please sign in to comment.