Created
October 11, 2018 08:20
-
-
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
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
/** | |
* 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