knockout/knockout — pull request #972 by SteveSanderson (2014-06-11 12:21:36+00:00)
Title: bug in 'style'-binding

Hi,

we use knockout a lot in our Project, and have noticed that when you try to update css properties, which have no unit, you can end up that knockout removes the property, because the value you want to set is ```0```. (like setting ```"opacity: 0"```)

```javascript
ko.bindingHandlers['style'] = {
'update': function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor() || {});
for (var styleName in value) {
if (typeof styleName == "string") {
var styleValue = ko.utils.unwrapObservable(value[styleName]);
element.style[styleName] = styleValue || ""; // Empty string removes the value, whereas null/undefined have no effect
}
}
}
};
```

The problem lies on the folowing line
```javascript
element.style[styleName] = styleValue || "";
```

since ```styleValue``` is ```0``` it will default to ```""```.

we overwrote the 'style'-binding with the following code:
```javascript
var cssNumberTest = /columnCount|fillOpacity|fontWeight|lineHeight|opacity|orphans|widows|zIndex|zoom/;
ko.bindingHandlers['style'] = {
'update': function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor() || {});
for (var styleName in value) {
if (typeof styleName == "string") {
var styleValue = ko.utils.unwrapObservable(value[styleName]);
if(!cssNumberTest.test(styleName)){
styleValue = styleValue || ""; // Empty string removes the value, whereas null/undefined have no effect
}
element.style[styleName] = styleValue;
}
}
}
};
```

Please consider updating knockout.js to make it an even greater, more stable library!

Thanks