Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// http://jdbartlett.github.com/innershiv | WTFPL License
window.innerShiv = (function(document) {
var d, r,
// test should pass if unknown elements innerHTML'd off-dom don't collapse.
needsShiv = (function(){
var omg = document.createElement('div');
omg.innerHTML = '<sh><iv></iv></sh>';
return omg.childNodes.length === 1;
})();
return function(/* string */ h, /* optional false boolean */ u) {
// don't make more divs than necessary
if (!d) {
d = document.createElement('div');
r = document.createDocumentFragment();
// in IE the div has to be appended to the shim'd body, so this hides it.
if (needsShiv) d.style.display = 'none';
}
// fast track
if (!needsShiv && u === false){
d.innerHTML = h;
return d.childNodes;
}
if (needsShiv) {
// maybe do a regex on the string to.... (probably combine this with the parent if statement.)
// if (/abbr|article|aside|audio|canvas|datalist|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video/.test(h) == false) THEN DONT DO ALL of THISSSSSS
var e = d.cloneNode(true);
document.body.appendChild(e);
// string trim to avoid whitespace text nodes
e.innerHTML = h.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
document.body.removeChild(e);
// if false was passed in as 2nd arg, return nodeList of childNodes
if (u === false) return e.childNodes;
} else {
e.innerHTML = h;
}
var f = r.cloneNode(true),
i = e.childNodes.length;
while (i--) f.appendChild(e.firstChild);
return f; // have to return docFrag
}
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment