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
31 lines (30 loc) • 1.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const parseSelector_1 = __importDefault(require("./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" />'
* ```
*/
function selectorToHTML(selector) {
const { tagName, attributes } = parseSelector_1.default(selector);
const atts = Object.entries(attributes)
.map(([att, value]) => ` ${att}${value ? `="${value}"` : ''}`)
.join('');
const end = voidTags.includes(tagName) ? ' /' : `></${tagName}`;
return `<${tagName}${atts}${end}>`;
}
exports.default = selectorToHTML;