MCPcopy
hub / github.com/arfct/itty-bitty / el

Function el

docs/js/util.js:1–36  ·  view source on GitHub ↗
(selector, ...args)

Source from the content-addressed store, hash-verified

1const el = (selector, ...args) => {
2 var attrs = (args[0] && typeof args[0] === 'object' && !Array.isArray(args[0]) && !(args[0] instanceof HTMLElement)) ? args.shift() : {};
3
4 let classes = selector.split(".");
5 if (classes.length > 0) selector = classes.shift();
6 if (classes.length) attrs.className = classes.join(" ")
7
8 let id = selector.split("#");
9 if (id.length > 0) selector = id.shift();
10 if (id.length) attrs.id = id[0];
11
12 var node = document.createElement(selector.length > 0 ? selector : "div");
13 for (let prop in attrs) {
14 if (attrs.hasOwnProperty(prop) && attrs[prop] != undefined) {
15 if (prop.indexOf("data-") == 0) {
16 let dataProp = prop.substring(5).replace(/-([a-z])/g, function(g) { return g[1].toUpperCase(); });
17 node.dataset[dataProp] = attrs[prop];
18 } else {
19 if (typeof attrs[prop] === 'function' || prop == "className") {
20 node[prop] = attrs[prop];
21 } else {
22 node.setAttribute(prop, attrs[prop]);
23 }
24 }
25 }
26 }
27
28 const append = (child) => {
29 if (Array.isArray(child)) return child.forEach(append);
30 if (typeof child == "string") child = document.createTextNode(child);
31 if (child) node.appendChild(child);
32 };
33 args.forEach(append);
34
35 return node;
36};
37el.trust = function (html) {
38 if (!html?.length) return undefined;
39 var template = document.createElement('template');

Callers 7

renderFunction · 0.50
download.jsFile · 0.50
renderFunction · 0.50
bookmarklet.jsFile · 0.50
contact.jsFile · 0.50
renderFunction · 0.50
renderFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected