Skip to content

Commit

Permalink
Merge branch 'zero298-css-variable-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Aug 4, 2018
2 parents 4d1101b + f9cbec0 commit 8886ec4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion spec/defaultBindings/styleBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ describe('Binding: CSS style', function() {
expect(testNode.childNodes[0].style.width).toBe("");
});

if (window.CSS && window.CSS.supports && window.CSS.supports('--a', 0)) {
it('Should be able to assign values to custom CSS properties', function() {
var customWidth = ko.observable();
testNode.innerHTML = "<div style=\"width: var(--custom-width)\" data-bind=\"style: {'--custom-width': customWidth}\"></div>";

ko.applyBindings({customWidth: customWidth}, testNode);
expect(testNode.childNodes[0].style.getPropertyValue("--custom-width")).toBe("");

customWidth("100px");
expect(testNode.childNodes[0].style.getPropertyValue("--custom-width")).toBe("100px");

customWidth(false);
expect(testNode.childNodes[0].style.getPropertyValue("--custom-width")).toBe("");
});
}

it('Should properly respond to changes in the observable, adding px when appropriate', function() {
var width = ko.observable();
testNode.innerHTML = "<div data-bind='style: { width: width }'></div>";
Expand All @@ -68,4 +84,4 @@ describe('Binding: CSS style', function() {
width(false);
expect(testNode.childNodes[0].style.width).toBe("");
});
});
});
3 changes: 3 additions & 0 deletions src/binding/defaultBindings/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ ko.bindingHandlers['style'] = {

if (jQueryInstance) {
jQueryInstance(element)['css'](styleName, styleValue);
} else if (/^--/.test(styleName)) {
// Is styleName a custom CSS property?
element.style.setProperty(styleName, styleValue);
} else {
styleName = styleName.replace(/-(\w)/g, function (all, letter) {
return letter.toUpperCase();
Expand Down

0 comments on commit 8886ec4

Please sign in to comment.