vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
25 lines (24 loc) • 891 B
JavaScript
import parseSelector from './parseSelector';
const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
/**
* Converts a given CSS selector into HTML
* (No this is not an Emmet substitute, so it is limited to only one element)
*
* @param selector - The CSS selector to convert
* @return The parsed HTML
*
* @example
*
* ```ts
* selectorToHTML('#id.class-name');
* // -> '<div id="id" class="class-name" />'
* ```
*/
export default function selectorToHTML(selector) {
const { tagName, attributes } = parseSelector(selector);
const atts = Object.entries(attributes)
.map(([att, value]) => ` ${att}${value ? `="${value}"` : ''}`)
.join('');
const end = voidTags.includes(tagName) ? ' /' : `></${tagName}`;
return `<${tagName}${atts}${end}>`;
}