Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix {{else}} inside an attribute (#974) #1006

Merged
merged 2 commits into from May 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions view/stache/stache.js
Expand Up @@ -7,7 +7,7 @@ steal(
"./mustache_core.js",
"./mustache_helpers.js",
"can/view/callbacks",
function(can, parser, target, HTMLSection, TextSection, mustacheCore, mustacheHelpers, viewCallbacks ){
function(can, parser, target, HTMLSectionBuilder, TextSectionBuilder, mustacheCore, mustacheHelpers, viewCallbacks ){

// Make sure that we can also use our modules with Stache as a plugin
parser = parser || can.view.parser;
Expand All @@ -19,7 +19,7 @@ steal(
template = mustacheCore.cleanLineEndings(template);

// The HTML section that is the root section for the entire template.
var section = new HTMLSection(),
var section = new HTMLSectionBuilder(),

// This function is a catch all for taking a section and figuring out
// how to create a "renderer" that handles the functionality for a
Expand Down Expand Up @@ -48,7 +48,7 @@ steal(
// the mustache text, and sets up live binding if an observable is read.
// A StringBranchRenderer function processes the mustache text and returns a
// text value.
var makeRenderer = section instanceof HTMLSection ?
var makeRenderer = section instanceof HTMLSectionBuilder ?

mustacheCore.makeLiveBindingBranchRenderer:
mustacheCore.makeStringBranchRenderer;
Expand Down Expand Up @@ -210,7 +210,7 @@ steal(


if(expression === "else") {
section.inverse();
(state.attr && state.attr.section ? state.attr.section : section).inverse();
return;
}

Expand All @@ -232,7 +232,7 @@ steal(
else if(state.attr) {

if(!state.attr.section) {
state.attr.section = new TextSection();
state.attr.section = new TextSectionBuilder();
if(state.attr.value) {
state.attr.section.add(state.attr.value);
}
Expand All @@ -249,7 +249,7 @@ steal(
state.node.attributes.push( mustacheCore.makeLiveBindingBranchRenderer( null,expression, copyState() ) );
} else if( mode === "#" || mode === "^" ) {
if(!state.node.section) {
state.node.section = new TextSection();
state.node.section = new TextSectionBuilder();
}
makeRendererAndUpdateSection(state.node.section, mode, expression );
} else {
Expand Down
12 changes: 12 additions & 0 deletions view/stache/stache_test.js
Expand Up @@ -3467,4 +3467,16 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu
var frag = can.stache(tmpl)({ noData: true });
equal(frag.childNodes[0].innerHTML, 'no data', 'else with unless worked');
});

test("{{else}} within an attribute (#974)", function(){
var tmpl = '<div class="{{#if color}}{{color}}{{else}}red{{/if}}"></div>',
data = new can.Map({
color: 'orange'
}),
frag = can.stache(tmpl)(data);

equal(frag.childNodes[0].getAttribute('class'), 'orange', 'if branch');
data.attr('color', false);
equal(frag.childNodes[0].getAttribute('class'), 'red', 'else branch');
});
});
2 changes: 1 addition & 1 deletion view/stache/text_section.js
Expand Up @@ -19,7 +19,7 @@ steal("can/util", "can/view/live","./utils.js",function(can, live, utils){
this.stack.pop();
},
inverse: function(){
this.pop();
this.stack.pop();
var falseySection = new TextSection();
this.last().last().falsey = falseySection;
this.stack.push(falseySection);
Expand Down