Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Created January 3, 2023 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigamonkey/94f2df2e718f22a8c5ae3d23e6d56743 to your computer and use it in GitHub Desktop.
Save gigamonkey/94f2df2e718f22a8c5ae3d23e6d56743 to your computer and use it in GitHub Desktop.
/*
* Regexp to match a single HTML tag.
*/
const tagPattern = /^<(\w+)>$/;
/*
* Get or create a single element. If the query is in the form of a tag, e.g.
* '<p>' it creates an element of that type. Otherwise it queries the document
* using the argument as a selector.
*/
const $ = (q) => {
const tag = q.match(tagPattern)?.[1];
return tag ? document.createElement(tag) : document.querySelector(q);
};
/*
* Get all elements matching selector.
*/
const $$ = (q) => document.querySelectorAll(q);
/*
* Create a text node.
*/
const text = (t) => document.createTextNode(t);
/*
* Create an element or elements from literal HTML. If the HTML specifies just
* one element it is returned. Otherwise returns an array of elements.
*/
const html = (html) => {
const t = document.createElement('template');
t.innerHTML = html.trim();
const children = [...t.content.children];
return children.length == 1 ? children[0] : children;
};
export { $, $$, text, html };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment