Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Added support for optional raw arguments #381

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ exports.parse = function (source, opts, tags, filters) {
escape = (args[0] !== 'false') ? args[0] : false;
break;
case 'raw':
inRaw = true;
inRaw = args[0] === undefined || args[0] === 'true';
break;
}

Expand Down
41 changes: 38 additions & 3 deletions lib/tags/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,49 @@
* {% raw %}{{ foobar }}{% endraw %}
* // => {{ foobar }}
*
* @example
* // bool = true
* {% raw bool %}{{ foobar }}{% endraw %}
* // => {{ foobar }}
*
* @exmaple
* // environment = 'pre-client'
* {% raw environment === 'pre-client' %}{{ foobar }}{% endraw %}
* // => {{ foobar }}
*/
exports.compile = function (compiler, args, content, parents, options, blockName) {
return compiler(content, parents, options, blockName);
};
exports.parse = function (str, line, parser) {
parser.on('*', function (token) {
throw new Error('Unexpected token "' + token.match + '" in raw tag on line ' + line + '.');
exports.parse = function (str, line, parser, types) {
parser.on(types.COMPARATOR, function (token) {
if (this.isLast) {
throw new Error('Unexpected logic "' + token.match + '" on line ' + line + '.');
}
if (this.prevToken.type === types.NOT) {
throw new Error('Attempted logic "not ' + token.match + '" on line ' + line + '. Use !(foo ' + token.match + ') instead.');
}
this.out.push(token.match);
});

parser.on(types.NOT, function (token) {
if (this.isLast) {
throw new Error('Unexpected logic "' + token.match + '" on line ' + line + '.');
}
this.out.push(token.match);
});

parser.on(types.BOOL, function (token) {
this.out.push(token.match);
});

parser.on(types.LOGIC, function (token) {
if (!this.out.length || this.isLast) {
throw new Error('Unexpected logic "' + token.match + '" on line ' + line + '.');
}
this.out.push(token.match);
this.filterApplyIdx.pop();
});

return true;
};
exports.ends = true;
25 changes: 21 additions & 4 deletions tests/tags/raw.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,26 @@ describe('Tag: raw', function () {
.to.equal('{# foo #}');
});

it('does not accept arguments', function () {
expect(function () {
swig.render('{% raw foobar %}foo{% endraw %}');
}).to.throwError(/Unexpected token "foobar" in raw tag on line 1\./);
it('{% raw true %}{{ foo }}{% endraw %}', function () {
expect(swig.render('{% raw true %}{{ foo }}{% endraw %}'))
.to.equal('{{ foo }}');
});

it('{% raw false %}{{ foo }}{% endraw %}', function () {
expect(swig.render('{% raw false %}{{ foo }}{% endraw %}', {
locals: { foo: 'foobar' }
})).to.equal('foobar');
});

it('{% raw bool %}{{ foo }}{% endraw %}', function () {
expect(swig.render('{% raw true %}{{ foo }}{% endraw %}', {
locals: { bool: true }
})).to.equal('{{ foo }}');
});

it('{% raw bool %}{{ foo }}{% endraw %}', function () {
expect(swig.render('{% raw false %}{{ foo }}{% endraw %}', {
locals: { foo: 'foobar', bool: false }
})).to.equal('foobar');
});
});