SPB Git

Repository Public

dom.js 4.0 KB 91 lines javascript

1// Construction du DOM sans innerHTML (aucune donnée de l'API n'est jamais interprétée comme du HTML).23const SVG_NS = "http://www.w3.org/2000/svg";45function append(el, children) {6  for (const c of children) {7    if (c === null || c === undefined || c === false) continue;8    if (Array.isArray(c)) append(el, c);9    else el.append(c instanceof Node ? c : document.createTextNode(String(c)));10  }11}1213function build(el, attrs, children, isSvg) {14  for (const [k, v] of Object.entries(attrs || {})) {15    if (v === null || v === undefined || v === false) continue;16    if (k === "class" && !isSvg) el.className = v;17    else if (k === "style" && typeof v === "object") for (const [p, val] of Object.entries(v)) el.style.setProperty(p, val);18    else if (k.startsWith("on") && typeof v === "function") el.addEventListener(k.slice(2), v);19    else if (k === "dataset") Object.assign(el.dataset, v);20    else if (v === true) el.setAttribute(k, "");21    else if (!isSvg && k in el && !k.startsWith("aria-") && k !== "list" && k !== "form" && k !== "type") el[k] = v;22    else el.setAttribute(k, v);23  }24  append(el, children);25  return el;26}2728export function h(tag, attrs, ...children) {29  return build(document.createElement(tag), attrs, children, false);30}3132export function s(tag, attrs, ...children) {33  return build(document.createElementNS(SVG_NS, tag), attrs, children, true);34}3536export function clear(el) {37  el.replaceChildren();38  return el;39}4041export function mount(el, ...children) {42  el.replaceChildren();43  append(el, children);44  return el;45}4647const ICONS = {48  grid: ["M4 4h7v7H4z", "M13 4h7v4h-7z", "M13 11h7v9h-7z", "M4 14h7v6H4z"],49  activity: ["M3 12h4l3-8 4 16 3-8h4"],50  chart: ["M4 20V10", "M10 20V4", "M16 20v-7", "M22 20H2"],51  key: ["M14.5 9.5a4 4 0 1 0-3.9 4.9L12 16h2v2h2v2h3v-3l-5.1-5.1a4 4 0 0 0 .6-2.4z", "M9.5 9.5h.01"],52  cube: ["M12 3l8 4.5v9L12 21l-8-4.5v-9z", "M12 12l8-4.5", "M12 12v9", "M12 12L4 7.5"],53  shield: ["M12 3l7 3v5c0 4.5-3 8.3-7 10-4-1.7-7-5.5-7-10V6z", "M9 12l2 2 4-4"],54  list: ["M8 6h12", "M8 12h12", "M8 18h12", "M4 6h.01", "M4 12h.01", "M4 18h.01"],55  sun: ["M12 8a4 4 0 1 0 0 8 4 4 0 0 0 0-8z", "M12 2v2", "M12 20v2", "M4.9 4.9l1.4 1.4", "M17.7 17.7l1.4 1.4", "M2 12h2", "M20 12h2", "M4.9 19.1l1.4-1.4", "M17.7 6.3l1.4-1.4"],56  moon: ["M20 14.5A8 8 0 0 1 9.5 4a8 8 0 1 0 10.5 10.5z"],57  logout: ["M15 4h3a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2h-3", "M10 16l-4-4 4-4", "M6 12h10"],58  more: ["M5 12h.01", "M12 12h.01", "M19 12h.01"],59  plus: ["M12 5v14", "M5 12h14"],60  copy: ["M9 9h10v11H9z", "M5 15V4h10"],61  check: ["M5 12.5l4.5 4.5L19 7.5"],62  alert: ["M12 4l9 16H3z", "M12 10v4", "M12 17h.01"],63  info: ["M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z", "M12 11v5", "M12 8h.01"],64  x: ["M6 6l12 12", "M18 6L6 18"],65  refresh: ["M20 11a8 8 0 0 0-14.5-4L4 9", "M4 4v5h5", "M4 13a8 8 0 0 0 14.5 4L20 15", "M20 20v-5h-5"],66  passkey: ["M10 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8z", "M3 21v-1a6 6 0 0 1 8.5-5.5", "M18 11a3 3 0 1 0 0 6 3 3 0 0 0 0-6z", "M18 17v5l1.5-1.5", "M18 14h.01"],67  trash: ["M4 7h16", "M9 7V4h6v3", "M6 7l1 13h10l1-13", "M10 11v5", "M14 11v5"],68  edit: ["M4 20h4L19 9l-4-4L4 16z", "M13.5 6.5l4 4"],69  pause: ["M8 5v14", "M16 5v14"],70  play: ["M7 4l12 8-12 8z"],71  clock: ["M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z", "M12 7v5l3 2"],72  device: ["M7 2h10a1 1 0 0 1 1 1v18a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z", "M11 18h2"],73  lock: ["M6 11h12v10H6z", "M8 11V8a4 4 0 0 1 8 0v3"],74  cloud: ["M7 18a4 4 0 0 1-.5-8 6 6 0 0 1 11.5 1.5 3.5 3.5 0 0 1 0 6.5z"],75  external: ["M14 4h6v6", "M20 4l-9 9", "M18 14v6H4V6h6"],76  stats: ["M4 19h16", "M6 15l4-5 4 3 5-7"],77};7879export function icon(name, cls = "") {80  const paths = ICONS[name] || ICONS.info;81  return s("svg", { class: `icon ${cls}`.trim(), viewBox: "0 0 24 24", "aria-hidden": "true", focusable: "false" },82    paths.map((d) => s("path", { d })));83}8485/** Remplit les éléments statiques <span data-icon="…"> du HTML. */86export function hydrateIcons(root = document) {87  for (const el of root.querySelectorAll("[data-icon]")) {88    el.replaceWith(icon(el.dataset.icon, el.dataset.iconClass || ""));89  }90}91
Files main
Menu

Theme