MCPcopy Index your code
hub / github.com/arfct/itty-bitty / el

Function el

docs/render.js:7–42  ·  view source on GitHub ↗
(selector, ...args)

Source from the content-addressed store, hash-verified

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

Callers 3

loadScriptFunction · 0.70
loadSyleFunction · 0.70
renderScriptContentFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected