Created
May 12, 2023 14:10
-
-
Save mflorida/da5ee8a0c5fd3150710d8f82c7ef9d81 to your computer and use it in GitHub Desktop.
Create new or select existing DOM element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find or make the DOM things | |
*/ | |
let createRE = /^<\s*|\s*\/*>$/g; | |
createRE = /^<\s*|\s*\/?>$/g; | |
function parseTag(tag) { | |
} | |
export class X0 { | |
constructor(...args) { | |
this.args = args; | |
} | |
query(selector, context = document) { | |
return context.querySelector(selector); | |
} | |
queryAll(selector, context = document) { | |
return [...context.querySelectorAll(selector)]; | |
} | |
queryId(id, context = document) { | |
return context.getElementById(id); | |
} | |
queryClass(className, context = document) { | |
return [...context.getElementsByName(className)]; | |
} | |
queryTag(tagName, context = document) { | |
return [...context.getElementsByTagName(tagName)]; | |
} | |
create(opts) { | |
Object.assign(this, {...opts}); | |
// this.tag | |
// this.attr | |
// this.prop | |
// this.children | |
const { | |
tag = '', | |
attr = {}, | |
prop = {}, | |
children = [] | |
} = opts; | |
if (/^</.test(tag)) { | |
this.element = document.createElement(tag.replace(createRE, '')); | |
} else { | |
// Not creating? RETURN! | |
return this; | |
} | |
return this; | |
} | |
attr(attrs) { | |
for (const [name, val] of Object.entries(attrs)) { | |
try { | |
this.element.setAttribute(name, String(val)); | |
} catch (e) { | |
console.warn(e); | |
} | |
} | |
return this; | |
} | |
prop(props) { | |
for (const [name, val] of Object.entries(props)) { | |
try { | |
this.element[name] = val; | |
} catch (e) { | |
console.warn(e); | |
} | |
} | |
return this; | |
} | |
appendItems(items = []) { | |
} | |
} | |
export function x0(tag, opts, children) { | |
// Start 'selector' with `<` to *create* an element | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment