@test-runner/web
Version:
34 lines (30 loc) • 1.02 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.dommo = factory());
}(this, function () { 'use strict';
/**
* @param {string} html
* @param {object} [options]
* @param {object} [options.document]
* @param {string} [options.parentEl]
*/
function domify (html, options) {
options = Object.assign({}, {
document: typeof document === 'undefined' ? undefined : document,
parentEl: 'container'
}, options);
const div = options.document.createElement(options.parentEl);
div.innerHTML = html.trim();
if (div.childNodes.length === 1) {
return div.firstChild
} else {
const frag = options.document.createDocumentFragment();
Array.from(div.childNodes).forEach(function (childNode) {
frag.appendChild(childNode);
});
return frag
}
}
return domify;
}));