Skip to content

Commit 768248b

Browse files
committedFeb 26, 2012
Merge branch '323-IE-class-attr'
2 parents ba7d91e + 1f93a58 commit 768248b

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
 

‎spec/defaultBindingsBehaviors.js

+12
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,18 @@ describe('Binding: Attr', {
913913
model.myprop(testValue);
914914
value_of(testNode.childNodes[0].getAttribute("someAttrib")).should_be(null);
915915
});
916+
},
917+
918+
'Should be able to set class attribute and access it using className property': function() {
919+
var model = { myprop : ko.observable("newClass") };
920+
testNode.innerHTML = "<div class='oldClass' data-bind=\"attr: {'class': myprop}\"></div>";
921+
value_of(testNode.childNodes[0].className).should_be("oldClass");
922+
ko.applyBindings(model, testNode);
923+
value_of(testNode.childNodes[0].className).should_be("newClass");
924+
// Should be able to clear class also
925+
model.myprop(undefined);
926+
value_of(testNode.childNodes[0].className).should_be("");
927+
value_of(testNode.childNodes[0].getAttribute("class")).should_be(null);
916928
}
917929
});
918930

‎src/binding/defaultBindings.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,34 @@ ko.bindingHandlers['checked'] = {
425425
}
426426
};
427427

428+
var attrHtmlToJavascriptMap = { 'class': 'className', 'for': 'htmlFor' };
428429
ko.bindingHandlers['attr'] = {
429430
'update': function(element, valueAccessor, allBindingsAccessor) {
430431
var value = ko.utils.unwrapObservable(valueAccessor()) || {};
431432
for (var attrName in value) {
432433
if (typeof attrName == "string") {
433434
var attrValue = ko.utils.unwrapObservable(value[attrName]);
434-
435+
435436
// To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely
436437
// when someProp is a "no value"-like value (strictly null, false, or undefined)
437438
// (because the absence of the "checked" attr is how to mark an element as not checked, etc.)
438-
if ((attrValue === false) || (attrValue === null) || (attrValue === undefined))
439+
var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined);
440+
if (toRemove)
439441
element.removeAttribute(attrName);
440-
else
442+
443+
// In IE <= 7 and IE8 Quirks Mode, you have to use the Javascript property name instead of the
444+
// HTML attribute name for certain attributes. IE8 Standards Mode supports the correct behavior,
445+
// but instead of figuring out the mode, we'll just set the attribute through the Javascript
446+
// property for IE <= 8.
447+
if (ko.utils.ieVersion <= 8 && attrName in attrHtmlToJavascriptMap) {
448+
attrName = attrHtmlToJavascriptMap[attrName];
449+
if (toRemove)
450+
element.removeAttribute(attrName);
451+
else
452+
element[attrName] = attrValue;
453+
} else if (!toRemove) {
441454
element.setAttribute(attrName, attrValue.toString());
455+
}
442456
}
443457
}
444458
}

0 commit comments

Comments
 (0)
Please sign in to comment.