Last active
February 21, 2019 19:24
-
-
Save VitorLuizC/958361b6c218360e00f712499eb8160d to your computer and use it in GitHub Desktop.
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
/** | |
* Select an element that matches CSS selector. | |
* @param {string} selector - A CSS selector to match an element. | |
* @param {Document | HTMLElement} [from] | |
* @returns {HTMLElement} | |
*/ | |
export const select = (selector, from = document) => { | |
return from.querySelector(selector); | |
}; | |
/** | |
* Select elements that matches CSS selector. | |
* @param {string} selector - A CSS selector to match elements. | |
* @param {Document | HTMLElement} [from] | |
* @returns {HTMLElement[]} | |
*/ | |
export const selectAll = (selector, from = document) => { | |
return Array.from(from.querySelectorAll(selector)); | |
}; | |
/** | |
* Exclude element from DOM. | |
* @param {HTMLElement} element | |
*/ | |
export const exclude = (element) => { | |
if (element && element.parentNode) | |
element.parentNode.removeChild(element); | |
}; | |
/** | |
* Get an array of ascendancy nodes. | |
* @param {HTMLElement} element | |
* @returns {HTMLElement[]} | |
*/ | |
export const ascendancy = (element) => { | |
const parent = element.parentNode; | |
return !parent ? [] : [parent].concat(ascendancy(parent)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment