deleight
Version:
A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.
44 lines (43 loc) • 1.25 kB
JavaScript
;
/**
* htmldom
* Functions for building DOM from HTML text or file.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFragment = exports.createFragment = void 0;
/**
* Shorthand for creating a DocumentFragment from HTML text.
*
* @example
* import { createFragment } from 'inherent';
* const frag1 = createFragment(`
* <p>Para 1</p>
* <p>Para 2</p>
*`)
* // <p>Para 1</p><p>Para 2</p>
*
* @param html The `outerHTML` of what to create
* @returns
*/
function createFragment(html) {
const temp = document.createElement('template');
temp.innerHTML = html;
return temp.content;
}
exports.createFragment = createFragment;
/**
* Fetches markup from a remote link and creates a document fragment out of it.
*
* @example
* import { loadFragment } from 'inherent';
* const frag1 = await loadFragment(`./my/fragment.html`)
* // <p>Para 1</p><p>Para 2</p>
*
* @param href The link to load the markup from
* @param init The request init used with `fetch`
* @returns A promise that resolves to the document fragment.
*/
async function loadFragment(href, init) {
return fetch(href, init).then(r => r.text()).then(t => createFragment(t));
}
exports.loadFragment = loadFragment;