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

Fixing one-line-ifs expected logic #540

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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,19 @@ One line ifs
If you need a more concise conditional to control which attributes are applied to a given element, then use this syntax:

```html
<p if-something true="class='present'" false="class='not-present'">One line if.</p>
<p if-something true="class='shown'" false="class='hidden'">One line if.</p>
```

In that structure, the attribute `if-something` checks to see if the variable `something` is present. If so, the class delcared in the `true` attribute is written to the element, resulting in the following output:
In that structure, the attribute `if-something` checks to see if the variable `something` is truthy. This means it will check for either variable presence in the model or the boolean value `true`. If so, the class delcared in the `true` attribute is written to the element, resulting in the following output:

```html
<p class='present'>One line if.</p>
<p class='shown'>One line if.</p>
```

If not, the class declared in the `false` attribute is written to the element, resulting in the following output:
If not, this will check for non-presence in the model or the boolean value `false`. If so, the class declared in the `false` attribute is written to the element, resulting in the following output:

```html
<p class='not-present'>One line if.</p>
<p class='hidden'>One line if.</p>
```

Like the `<if>` tag you can check for both the presence of a variable as well as its value. To check the value of a variable, use this syntax:
Expand Down
5 changes: 4 additions & 1 deletion conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ function parseOneLineIf (charList, model) {
return insertValue(charList, condition.false.split('').reverse().join(''), startIndex, endIndex)
}
} else { // There is no value to compare against
if (varVal && varVal[0] === '{' && varVal[varVal.length - 1] === '}') {
// Cases for when there isn't a condition.varLiteral value
// case 1: `false` literal or variable not present in model (resolving to var name within brackets, ex: {notInModel}) -> insert false condition
// case 2: non-empty string present in model or `true` literal -> insert true condition
if (varVal === false || (varVal[0] === '{' && varVal[varVal.length - 1] === '}')) {
return insertValue(charList, condition.false.split('').reverse().join(''), startIndex, endIndex)
} else {
return insertValue(charList, condition.true.split('').reverse().join(''), startIndex, endIndex)
Expand Down
2 changes: 1 addition & 1 deletion test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('Conditionals', function () {
done()
})

it.skip('should evaluate one line if "if-somethingFalse" as false (conditionals/oneLineIfBooleanValue.html)', function (done) {
it('should evaluate one line if "if-somethingFalse" as false (conditionals/oneLineIfBooleanValue.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineIfBooleanValue.html', model), '<p></p>')
done()
})
Expand Down