Skip to content

Instantly share code, notes, and snippets.

@intrnl
Created Jun 18, 2022
Embed
What would you like to do?
/**
* @param {HTMLElement} node
* @param {string} target
* @returns {?HTMLElement}
*/
export function query (node, target) {
if (node.matches(`[x-target~='this.${target}']`)) {
return node;
}
return node.querySelector(`[x-target~='${node.localName}.${target}']`) || null;
}
/**
* @param {HTMLElement} node
* @param {string} target
* @returns {HTMLElement[]}
*/
export function queryAll (node, target) {
const array = [];
const matches = node.querySelectorAll(`[x-targets~='${node.localName}.${target}']`);
if (node.matches(`[x-targets~='this.${target}']`)) {
array.push(node);
}
return array.concat(matches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment