Skip to content

Instantly share code, notes, and snippets.

@yevhenorlov
Created July 25, 2018 08:46
Show Gist options
  • Save yevhenorlov/be25e935b7383db9211a87e6b5bb4a46 to your computer and use it in GitHub Desktop.
Save yevhenorlov/be25e935b7383db9211a87e6b5bb4a46 to your computer and use it in GitHub Desktop.
Print component's classes (as CSS selectors) to a String and return it. Log to console optionally.
function printSelectorList(selector, logToConsole) {
const _logToConsole = !!logToConsole || false
if (!selector) throw new TypeError('Selector argument is required')
if (typeof selector !== 'string') {
throw new TypeError('Attribute must be a string')
}
const component = document.querySelector(selector)
const descendants = Array.prototype.slice.call(
component.querySelectorAll('*'),
0
)
const classSelectorList = descendants
.map(el => {
if (!el.classList.length) return
return `\n// .${el.classList[0]} {}\n`
})
.filter(el => typeof el !== 'undefined')
.join('')
if (_logToConsole) console.log(classSelectorList)
return classSelectorList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment