Skip to content

Commit

Permalink
fixing twigjs#737 make sure that ternary expression does not change c…
Browse files Browse the repository at this point in the history
…ontext + exporting functions for test purposes
  • Loading branch information
oleg-andreyev committed Apr 20, 2020
1 parent c53bc1b commit 8311815
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/twig.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ module.exports = function (Twig) {
// Export our tests.
Twig.exports.tests = Twig.tests;

// Export our functions.
Twig.exports.functions = Twig.functions;

Twig.exports.Promise = Twig.Promise;

return Twig;
Expand Down
2 changes: 1 addition & 1 deletion src/twig.expression.operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ module.exports = function (Twig) {
a = a.length;
}

if (b && Array.isArray(b)) {
if (operator !== '?' && (b && Array.isArray(b))) {
b = b.length;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/test.expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ describe('Twig.js Expressions ->', function () {
testTemplate.render({test: undefined}).should.equal('false');
});

it('should not override context', function () {
const str = `{% set classes = ['a', 'b'] %}{% set classes = classes ? classes|merge(['c']) : '' %}{{ dump(classes) }}`;
const expected = Twig.functions.dump(['a', 'b', 'c']);
const testTemplate = twig({data: str});
testTemplate.render().should.equal(expected);
});

it('should support in/containment functionality for arrays', function () {
let testTemplate;

Expand Down

0 comments on commit 8311815

Please sign in to comment.