UNPKG

tsdom

Version:

Fast, lightweight JavaScript DOM manipulation utility

43 lines (33 loc) 829 B
/* ----------------------------------- * * Variables * * -------------------------------- */ const regex = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/; /* ----------------------------------- * * Query * * -------------------------------- */ function query(qry: string, ctx: Element | Document) { let test; let match; if ((test = regex.exec(qry))) { if ((match = test[3])) { return ctx.getElementsByClassName(match); } if ((match = test[2])) { return ctx.getElementsByTagName(match); } if ((match = test[1])) { return document.getElementById(match); } } return ctx.querySelectorAll(qry); } /* ----------------------------------- * * Export * * -------------------------------- */ export { query };