1 var attrHtmlToJavascriptMap = { 'class': 'className', 'for': 'htmlFor' };
2 ko.bindingHandlers['attr'] = {
3 'update': function(element, valueAccessor, allBindings) {
4 var value = ko.utils.unwrapObservable(valueAccessor()) || {};
5 ko.utils.objectForEach(value, function(attrName, attrValue) {
6 attrValue = ko.utils.unwrapObservable(attrValue);
8 // To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely
9 // when someProp is a "no value"-like value (strictly null, false, or undefined)
10 // (because the absence of the "checked" attr is how to mark an element as not checked, etc.)
11 var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined);
13 element.removeAttribute(attrName);
15 // In IE <= 7 and IE8 Quirks Mode, you have to use the Javascript property name instead of the
16 // HTML attribute name for certain attributes. IE8 Standards Mode supports the correct behavior,
17 // but instead of figuring out the mode, we'll just set the attribute through the Javascript
18 // property for IE <= 8.
19 if (ko.utils.ieVersion <= 8 && attrName in attrHtmlToJavascriptMap) {
20 attrName = attrHtmlToJavascriptMap[attrName];
22 element.removeAttribute(attrName);
24 element[attrName] = attrValue;
25 } else if (!toRemove) {
26 element.setAttribute(attrName, attrValue.toString());
29 // Treat "name" specially - although you can think of it as an attribute, it also needs
30 // special handling on older versions of IE (https://github.com/SteveSanderson/knockout/pull/333)
31 // Deliberately being case-sensitive here because XHTML would regard "Name" as a different thing
32 // entirely, and there's no strong reason to allow for such casing in HTML.
33 if (attrName === "name") {
34 ko.utils.setElementName(element, toRemove ? "" : attrValue.toString());