Skip to content
Open
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
13 changes: 10 additions & 3 deletions js/src/x-dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ THE SOFTWARE.
var BREAK = false,
UNDEFINED,
OPERATORS = ['~', '#', '?', '@', ':', '<', '>', '+', '/', '^'],
OPERATORS_WITH_BODY = ['#', '?', '@', ':', '+', '<'],
OPERATORS_WITH_BODY = ['#', '?', '@', ':', '+', '<', '^'],
dust;

function _hasProp(obj, name) {
Expand Down Expand Up @@ -482,8 +482,13 @@ var XDustContext = _extend(function(head, tail, params) {
XDustNotExistsNode = _extend(XDustExistsNode, function() {
XDustExistsNode.apply(this, arguments);
}, {
prepareModel: function(chain, context, model) {
return this.context.resolve(context, model) ? 'else' : 'block';
operator: '^',
chooseBodyName: function(context, model) {
var resolved = this.context.resolve(context, model);
if (_isInstance(resolved, Array) && resolved.length < 1) {
resolved = false;
}
return resolved ? 'else' : 'block';
}
}),
XDustHelperNode = _extend(XDustLogicNode, function(name, scope, params, blocks, bodies) {
Expand Down Expand Up @@ -657,6 +662,8 @@ var XDustContext = _extend(function(head, tail, params) {
}
} else if (operator == '?') {
node = new XDustExistsNode(tagName, scope, params);
} else if (operator == '^') {
node = new XDustNotExistsNode(tagName, scope, params);
} else if (operator == '@') {
name = tag[0].slice(1);
if (name == 'idx') {
Expand Down
7 changes: 7 additions & 0 deletions js/test/unit/x-dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ describe('x-dust', function() {
it('can test logic without consuming scope', render('ex', '{?tags}<ul class="{foo}">{#tags}<li>{.}</li>{/tags}</ul>{:else}No tags!{/tags}', {foo: 'bar', tags: ['a', 'b', 'c']}, '<ul class="bar"><li>a</li><li>b</li><li>c</li></ul>'));
it('can support an else block', render('ex', '{?tags}<ul class="{foo}">{#tags}<li>{.}</li>{/tags}</ul>{:else}No tags!{/tags}', {foo: 'bar', tags: []}, 'No tags!'));
});
describe('^ operator', function() {
it('can output inner content when tested against an undefined value', render('ex', '{^tags}No tags!{:else}Yes tags!{/tags}', {foo: 'bar'}, 'No tags!'));
it('can output inner content when tested against a defined but falsey value', render('ex', '{^tags}No tags!{/tags}', {foo: 'bar', tags: 0}, 'No tags!'));
it('can output inner content when tested against an empty array', render('ex', '{^tags}No tags!{/tags}', {foo: 'bar', tags: []}, 'No tags!'));
it('can ignore inner content when tested against a truthy value', render('ex', '{^tags}no {/tags}tags here', {foo: 'bar', tags: ['a', 'b', 'c']}, 'tags here'));
it('can support an else block', render('ex', '{^tags}No tags!{:else}<ul class="{foo}">{#tags}<li>{.}</li>{/tags}</ul>{/tags}', {foo: 'bar', tags: ['a', 'b', 'c']}, '<ul class="bar"><li>a</li><li>b</li><li>c</li></ul>'));
});
describe('! operator', function() {
it('can comment out multiple lines of text', render('ex', '{!\nMultiline {#foo}{bar}{/foo}!}{!before!}Hello{!after!}', {}, 'Hello'));
});
Expand Down