Skip to content

Instantly share code, notes, and snippets.

@KargJonas
Created June 20, 2019 15:23
Show Gist options
  • Select an option

  • Save KargJonas/69beb8b4c46869e76a3c6287c5d42517 to your computer and use it in GitHub Desktop.

Select an option

Save KargJonas/69beb8b4c46869e76a3c6287c5d42517 to your computer and use it in GitHub Desktop.
caseSensitiveOuterHTML
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