Added support for "body-only-if"

This commit is contained in:
Patrick Steele-Idem 2014-12-10 21:22:51 -07:00
parent e5498ac247
commit ddc6cd5de8
6 changed files with 25 additions and 5 deletions

View File

@ -411,10 +411,9 @@ Node.prototype = {
this.generateCodeForChildren(template);
return;
}
var nextStripVarId = template.getAttribute('nextStripVarId');
if (nextStripVarId == null) {
nextStripVarId = template.setAttribute('nextStripVarId', 0);
}
var nextStripVarId = template.data.nextStripVarId || (template.data.nextStripVarId = 0);
template.data.nextStripVarId++;
var varName = '__strip' + nextStripVarId++;
template.statement('var ' + varName + ' = !(' + this.stripExpression + ');');
template.statement('if (' + varName + ') {').indent(function () {

View File

@ -242,7 +242,7 @@ function TemplateBuilder(compiler, path, rootNode) {
this.dirname = nodePath.dirname(path);
this.options = compiler.options || {};
this.templateName = null;
this.attributes = {};
this.attributes = this.data = {};
this.concatWrites = this.options.concatWrites !== false;
this.writer = new CodeWriter(this.concatWrites);
this.staticVars = [];

View File

@ -142,6 +142,14 @@ var coreAttrHandlers = [
}
}
],
[
'body-only-if', function(attr, node) {
if (!node.setStripExpression) {
node.addError('The c-strip directive is not allowed for target node');
}
node.setStripExpression(attr);
}
],
[
'c-input', function(attr, node) {
this.inputAttr = attr;

View File

@ -350,4 +350,10 @@ describe('marko/marko' , function() {
name: '<label for="hello">Hello</label>'
}, done);
});
it.only("should handle 'body-only-if' correctly", function(done) {
testRender("test-project/html-templates/body-only-if.marko", {
url: '/foo'
}, done);
});
});

View File

@ -0,0 +1,6 @@
<a href="${data.url}" body-only-if="!data.url">
Some Link
</a>
<a href="${data.invalidUrl}" body-only-if="!data.invalidUrl">
Another Link
</a>

View File

@ -0,0 +1 @@
<a href="/foo">Some Link</a>Another Link