Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// | 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) = '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);
// string trim to avoid whitespace text nodes
e.innerHTML = h.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment