Created
June 3, 2019 09:54
-
-
Save basshelal/0e6dd320cec78888005052b1739fda2c to your computer and use it in GitHub Desktop.
Check whether a node is editable or not in TypeScript
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
/** | |
* Checks whether the passed in node is editable or not. | |
* An editable node is one that returns true to isContentEditable or has a tag name as | |
* any one of the following: | |
* "textarea", "input", "text", "email", "number", "search", "tel", "url", "password" | |
* | |
* @param node the node to check | |
* @return true if the node is editable and false otherwise | |
*/ | |
function isEditable(node: Node): boolean { | |
let element = node as HTMLElement; | |
let nodeName: string = element.nodeName.toLowerCase(); | |
let editables = ["textarea", "input", "text", "email", "number", "search", "tel", "url", "password"]; | |
return (element.isContentEditable || (element.nodeType === Node.ELEMENT_NODE && editables.contains(nodeName))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment