Commit fd006409 authored by Akari Labs's avatar Akari Labs

drop in-built inert

parent 34a862a5
/*
inertDOM
a ridiculously simple jsx dom function provider
Copyright 2019 astra akari (nocebo)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function reduceChild(child) {
if (Array.isArray(child)) return child.map(reduceChild);
return [child];
}
function dom(tag, attrs, ...children) {
if (children.length === 1 && Array.isArray(children[0])) children = children[0];
if (children.length !== 0)
children = children.map(reduceChild)
.reduce((p, c) => p.concat(c));
var element;
switch (typeof tag) {
case 'function':
element = tag(attrs || {}, children);
break;
case 'string':
element = document.createElement(tag);
if (attrs) Object.keys(attrs).forEach(k => {
const a = attrs[k];
switch (typeof a) {
case 'boolean':
if (a) element.setAttribute(k, '');
break;
case 'function':
// a bit of a hack, but it works
if (k.startsWith('on')) {
element[k] = a;
break;
}
default:
element.setAttribute(k, a);
break;
}
});
function addChild(child) {
if (Array.isArray(child)) child.forEach(addChild);
else if (child instanceof HTMLElement) element.appendChild(child);
else if (typeof child === 'string') element.append(child);
}
addChild(children);
break;
}
return element;
}
module.exports = dom;
\ No newline at end of file
{
"friendly_name": "kwm",
"modules": {
"inertdom": {
"filename": "modules/inertdom.js",
"provides": "jsx"
},
"inject": {
"filename": "modules/inject.js"
},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment