-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
raw tag will skip the code from being processed
- Loading branch information
1 parent
65ac9cd
commit 9498e63
Showing
3 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)()`) | ||
}) | ||
}) |