Skip to content

Commit

Permalink
feat(tag): add raw tag
Browse files Browse the repository at this point in the history
raw tag will skip the code from being processed
  • Loading branch information
thetutlage committed Mar 28, 2017
1 parent 65ac9cd commit 9498e63
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
96 changes: 96 additions & 0 deletions src/Tags/Raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

/*
* edge
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const BaseTag = require('./BaseTag')

/**
* The raw tag for skipping the processing
* of a line
*
* @class RawTag
* @extends {BaseTag}
* @static
*/
class RawTag extends BaseTag {
/**
* Tag name to be used for registering
* the tag
*
* @method tagName
*
* @return {String}
*/
get tagName () {
return 'raw'
}

/**
* Whether or not the tag is block level
* tag. Which is no in this case.
*
* @method isBlock
*
* @return {Boolean}
*/
get isBlock () {
return true
}

/**
* Process a line by returning the raw contents from
* it. If line is a tag, process it's childs
* recursively.
*
* @method _processLine
*
* @param {Object} line
*
* @return {String}
*/
_processLine (line) {
if (line.tag) {
const output = [line.body]
line.childs.forEach((child) => {
output.push(this._processLine(child))
})
output.push(`@end${line.tag}`)
return output.join('\n')
}
return line.body
}

/**
* Compile the template
*
* @method compile
*
* @param {Object} compiler
* @param {Object} lexer
* @param {Object} buffer
* @param {String} options.body
* @param {Array} options.childs
* @param {Number} options.lineno
*
* @return {void}
*/
compile (compiler, lexer, buffer, { body, childs, lineno }) {
childs.forEach((child) => buffer.writeToOutput(this._processLine(child)))
}

/**
* Nothing needs to be done in runtime
* for debugger tag
*/
run () {
}
}

module.exports = RawTag
3 changes: 2 additions & 1 deletion src/Tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = {
slotTag: new (require('./SlotTag'))(),
sectionTag: new (require('./SectionTag'))(),
yieldTag: new (require('./YieldTag'))(),
debugger: new (require('./Debugger'))()
debugger: new (require('./Debugger'))(),
raw: new (require('./Raw'))()
}
57 changes: 57 additions & 0 deletions test/unit/tags/raw.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

/*
* adonis-edge
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const test = require('japa')
const Template = require('../../../src/Template')
const dedent = require('dedent-js')

test.group('Tags | Raw ', (group) => {
group.before(() => {
require('../../../test-helpers/transform-tags')(this, require('../../../src/Tags'))
})

test('parse simple raw block to compiled template', (assert) => {
const statement = dedent`
@raw
<p> Hello virk </p>
@endraw
`
const template = new Template(this.tags)
const output = template.compileString(statement)
assert.equal(output, dedent`
return (function templateFn () {
let out = new String()
out += \` <p> Hello virk </p>\\n\`
return out
}).bind(this)()`)
})

test('write tags within raw block to the output', (assert) => {
const statement = dedent`
@raw
@each(user in users)
<p> {{ user }} </p>
@endeach
@endraw
`
const template = new Template(this.tags)
const output = template.compileString(statement)

assert.equal(output, dedent`
return (function templateFn () {
let out = new String()
out += \` @each(user in users)
<p> {{ user }} </p>
@endeach\\n\`
return out
}).bind(this)()`)
})
})

0 comments on commit 9498e63

Please sign in to comment.