|
| 1 | +/* |
| 2 | +Copyright 2020 Bruno Windels <[email protected]> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// DOM helper functions |
| 18 | + |
| 19 | +export function isChildren(children) { |
| 20 | + // children should be an not-object (that's the attributes), or a domnode, or an array |
| 21 | + return typeof children !== "object" || !!children.nodeType || Array.isArray(children); |
| 22 | +} |
| 23 | + |
| 24 | +export function classNames(obj, value) { |
| 25 | + return Object.entries(obj).reduce((cn, [name, enabled]) => { |
| 26 | + if (typeof enabled === "function") { |
| 27 | + enabled = enabled(value); |
| 28 | + } |
| 29 | + if (enabled) { |
| 30 | + return cn + (cn.length ? " " : "") + name; |
| 31 | + } else { |
| 32 | + return cn; |
| 33 | + } |
| 34 | + }, ""); |
| 35 | +} |
| 36 | + |
| 37 | +export function setAttribute(el, name, value) { |
| 38 | + if (name === "className") { |
| 39 | + name = "class"; |
| 40 | + } |
| 41 | + if (value === false) { |
| 42 | + el.removeAttribute(name); |
| 43 | + } else { |
| 44 | + if (value === true) { |
| 45 | + value = name; |
| 46 | + } |
| 47 | + el.setAttribute(name, value); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function el(elementName, attributes, children) { |
| 52 | + return elNS(HTML_NS, elementName, attributes, children); |
| 53 | +} |
| 54 | + |
| 55 | +export function elNS(ns, elementName, attributes, children) { |
| 56 | + if (attributes && isChildren(attributes)) { |
| 57 | + children = attributes; |
| 58 | + attributes = null; |
| 59 | + } |
| 60 | + |
| 61 | + const e = document.createElementNS(ns, elementName); |
| 62 | + |
| 63 | + if (attributes) { |
| 64 | + for (let [name, value] of Object.entries(attributes)) { |
| 65 | + if (name === "className" && typeof value === "object" && value !== null) { |
| 66 | + value = classNames(value); |
| 67 | + } |
| 68 | + setAttribute(e, name, value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (children) { |
| 73 | + if (!Array.isArray(children)) { |
| 74 | + children = [children]; |
| 75 | + } |
| 76 | + for (let c of children) { |
| 77 | + if (!c.nodeType) { |
| 78 | + c = text(c); |
| 79 | + } |
| 80 | + e.appendChild(c); |
| 81 | + } |
| 82 | + } |
| 83 | + return e; |
| 84 | +} |
| 85 | + |
| 86 | +export function text(str) { |
| 87 | + return document.createTextNode(str); |
| 88 | +} |
| 89 | + |
| 90 | +export const HTML_NS = "http://www.w3.org/1999/xhtml"; |
| 91 | +export const SVG_NS = "http://www.w3.org/2000/svg"; |
| 92 | + |
| 93 | +export const TAG_NAMES = { |
| 94 | + [HTML_NS]: [ |
| 95 | + "br", "a", "ol", "ul", "li", "div", "h1", "h2", "h3", "h4", "h5", "h6", |
| 96 | + "p", "strong", "em", "span", "img", "section", "main", "article", "aside", |
| 97 | + "pre", "button", "time", "input", "textarea", "label", "form", "progress", "output"], |
| 98 | + [SVG_NS]: ["svg", "circle"] |
| 99 | +}; |
| 100 | + |
| 101 | +export const tag = {}; |
| 102 | + |
| 103 | + |
| 104 | +for (const [ns, tags] of Object.entries(TAG_NAMES)) { |
| 105 | + for (const tagName of tags) { |
| 106 | + tag[tagName] = function(attributes, children) { |
| 107 | + return elNS(ns, tagName, attributes, children); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments