Skip to content

Instantly share code, notes, and snippets.

@kahunacohen
Created October 11, 2018 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahunacohen/467f5cc259b5d4a85eb201518dcb15ec to your computer and use it in GitHub Desktop.
Save kahunacohen/467f5cc259b5d4a85eb201518dcb15ec to your computer and use it in GitHub Desktop.
A function that stringifies a DOM element. Option to truncate the string representation if it's too big
/**
* Stringifies a DOM node.
* @param {Object} el - A DOM node.
* @param {Number} truncate - How much to truncate innerHTML of element.
* @returns {String} - A stringified node with attributes
* retained.
*/
function stringifyEl(el, truncate) {
var truncateLen = truncate || 50;
var outerHTML = el.outerHTML;
var ret = outerHTML;
ret = ret.substring(0, truncateLen);
// If we've truncated, add an elipsis.
if (outerHTML.length > truncateLen) {
ret += "...";
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment