(type, opts, children)
| 30 | // Create a new DOM node of `type` with `opts` attributes and with given children. |
| 31 | // If children is a string, or an array with string elements, they become text nodes. |
| 32 | function createElement(type, opts, children) { |
| 33 | var el = document.createElement(type); |
| 34 | if (opts) { |
| 35 | for (let [key, value] of Object.entries(opts)) { |
| 36 | if (typeof value === "object") { |
| 37 | for (let [subkey, subvalue] of Object.entries(value)) { |
| 38 | el[key][subkey] = subvalue; |
| 39 | } |
| 40 | } else { |
| 41 | el[key] = value; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | if (children) { |
| 46 | if (typeof children === "string") { |
| 47 | el.textContent = children; |
| 48 | } else { |
| 49 | for (let child of children) { |
| 50 | if (typeof child === "string") |
| 51 | child = document.createTextNode(child); |
| 52 | el.appendChild(child); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | return el; |
| 57 | } |
| 58 | |
| 59 | // Like createElement, but also appends the new node to parent's children. |
| 60 | function addElement(parent, type, opts, children) { |
no outgoing calls
no test coverage detected