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
50 lines (49 loc) • 1.74 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var parseSelector_1 = __importDefault(require("./parseSelector"));
var 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) {
var _a = parseSelector_1.default(selector), tagName = _a.tagName, attributes = _a.attributes;
var atts = Object.entries(attributes)
.map(function (_a) {
var _b = __read(_a, 2), att = _b[0], value = _b[1];
return " " + att + (value ? "=\"" + value + "\"" : '');
})
.join('');
var end = voidTags.includes(tagName) ? ' /' : "></" + tagName;
return "<" + tagName + atts + end + ">";
}
exports.default = selectorToHTML;