Skip to content

Commit

Permalink
fix template switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed May 2, 2012
1 parent c2b38dc commit 3064425
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/compiler.js
Expand Up @@ -400,20 +400,20 @@ var _compileIfStatement = function(blockCompileFn, context, ast) {
var _compileSwitchStatement = function(blockCompileFn, context, ast) {
return code(
';(function(branches) {',
' switch ({{ expression }}) {',
' switch ({{ expression }}.getContent()) {',
map(ast.cases, function(switchCase, i) {
var labels = switchCase.isDefault
? 'default:\n'
: map(switchCase.values, function(value) {
return 'case ' + compileExpression(context, value) + ':\n'
return 'case ' + compileExpression(context, value) + '.getContent():\n'
}).join('')
return labels
+ 'branches['+i+'](); break'
}).join('\n'),
' }',
'})([',
map(ast.cases, function(switchCase, i) {
return 'function branches'+i+'(){ ' + indent(blockCompileFn, switchContext, switchCase.statements) + '}'
return 'function branches'+i+'(){ ' + indent(blockCompileFn, context, switchCase.statements) + '}'
}).join(',\n'),
'])',
{
Expand Down
21 changes: 21 additions & 0 deletions test/tests/test-4-compiler.js
Expand Up @@ -279,6 +279,27 @@ test("For loop only executes once for a push").code(
.click('#output')
.textIs('#output', '5')

test('switch').code(
'foo = "foo"',
'<div id="output">switch foo {',
' case "foo": "foo"',
' case "cat": "cat"',
' default: foo',
'}</div onclick=handler() {',
' switch foo {',
' case "foo": foo set:"cat"',
' case "cat": foo set:"tag"',
' default: foo set:"wee"',
'}}>')
.textIs('#output', "foo")
.click('#output')
.textIs('#output', "cat")
.click('#output')
.textIs('#output', "tag")
.click('#output')
.textIs('#output', "wee")


test("copy of grouped expression (foo + 1).copy()").code(
'foo = 1',
'<div id="output">foo</div onclick=handler() { foo set: (foo + 2).copy() }>')
Expand Down

0 comments on commit 3064425

Please sign in to comment.