SteveSanderson/knockout — Issue #364: <=IE8 expando attribute in innerHTML
Author: vamp
Created: 2012-03-07 11:59:48+00:00
State: open

reproduce:

``` javascript
var div = document.createElement('div'),
parent = document.createElement('div'),
property = "__ko_" + (+new Date);

div[property] = 1;
parent.appendChild(div);

if(parent.innerHTML.indexOf(property)!==-1){ alert("BUGGY"); }
```

my fix (without overwriting knockout.js)

function getExpandoAttribute(){
var node = document.createElement('div');

// set some random data
ko.utils.domData.set(node, 'key', 'value');

// expando attribute
var attribute = (0!==node.attributes.length ? ko.utils.arrayFirst(node.attributes, function(one){
return ('__ko__'===one.name.substring(0,6));
}) : {'name': false})['name'];

// prevent memory leaks
ko.utils.domData.clear(node);
node = null;

return attribute;
}

function applyExpandoAttributePatch(){
var sources = ko.templateSources,
attribute = getExpandoAttribute();

if(attribute){
var regex = new RegExp('\\s+'+attribute+'="[^"]+"', 'g');
for(var template in sources){
if(sources.hasOwnProperty(template) && sources[template]['prototype'] && sources[template]['prototype']['text']){
sources[template].prototype['text'] = (function(safe){
return function(){
return 0===arguments.length
? safe.call(this).replace(regex, '')
: safe.apply(this, arguments);
}

})(sources[template].prototype['text']);
}
}
}
}

```