Created
June 20, 2019 15:23
-
-
Save KargJonas/69beb8b4c46869e76a3c6287c5d42517 to your computer and use it in GitHub Desktop.
caseSensitiveOuterHTML
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
| function caseSensitiveOuterHTML(element) { | |
| if (element.nodeType === Node.TEXT_NODE) { | |
| return element.wholeText; | |
| } | |
| const tagName = element.tagName; | |
| let attributes = element.attributes; | |
| let children = [...element.childNodes]; | |
| if (children.length) { | |
| children = children.map(caseSensitiveOuterHTML); | |
| } | |
| children = children.join(""); | |
| if (attributes && attributes.length) { | |
| const keys = Object.keys(attributes); | |
| attributes = keys.map((key) => attributes[key]); | |
| attributes = attributes.map((attribute) => { | |
| const name = attribute.name; | |
| const value = attribute.value; | |
| return `${name}="${value}"`; | |
| }).join(" "); | |
| } | |
| return `<${tagName} ${attributes}>${children}</${tagName}>`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment