Last active
July 28, 2019 00:54
-
-
Save amadeuszblanik/f71fa4b8daa6c83dc5a62df7232e2408 to your computer and use it in GitHub Desktop.
Test if provided element is DOM element
This file contains 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
const isDOMelement = (element, nodeListAllowed = true) => { | |
if (typeof element !== "object" || element === null) { | |
console.debug("Element is not an object.\nIt's fine you are in debug mode."); | |
return false; | |
} else { | |
if (!element instanceof HTMLElement) { | |
console.warn("element is not an instanceof HTMLElement"); | |
return false; | |
} else if (typeof element.classList !== "object") { | |
if (!NodeList.prototype.isPrototypeOf(element)) { | |
console.warn("Provided element isn't NodeList and HTMLElement instanceof"); | |
return false; | |
} else { | |
if (!nodeListAllowed) { | |
console.debug("Provided element is NodeList and it allowed"); | |
return false; | |
} else { | |
console.warn("Provide element is NodeList and it's allowed"); | |
if(typeof element.addEventListener !== "undefined") { | |
console.warn("Provided element doesn't supports Event Listeners"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} | |
console.warn("classList of element is not an object"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
console.error("Unknown error."); | |
return false; | |
} | |
export default isDOMelement; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment