Last active
December 6, 2017 10:27
-
-
Save qwtel/84f0c074457716b31a99a1480c530595 to your computer and use it in GitHub Desktop.
Patches the default document.createElement function to follow the JSX/React.createElement function signature.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createElement = document.createElement.bind(document); | |
function appendChild(child) { | |
this.appendChild( | |
child instanceof Element ? child : document.createTextNode(child) | |
); | |
} | |
document.createElement = function(tagName, attrs, children) { | |
var el = createElement(tagName); | |
if (attrs) { | |
Object.keys(attrs).forEach(function(attr) { | |
el.setAttribute(attr, attrs[attr]) | |
}); | |
} | |
if (children) { | |
if (children.forEach) children.forEach(appendChild, el); | |
else appendChild.call(el, children); | |
} | |
return el; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment