Skip to content

Instantly share code, notes, and snippets.

@qwtel
Last active December 6, 2017 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwtel/84f0c074457716b31a99a1480c530595 to your computer and use it in GitHub Desktop.
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.
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