Skip to content

Instantly share code, notes, and snippets.

@jennz0r
Created January 20, 2015 23:51
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 jennz0r/1078a71f1e6b763a0890 to your computer and use it in GitHub Desktop.
Save jennz0r/1078a71f1e6b763a0890 to your computer and use it in GitHub Desktop.
Syntax Highlighting
function cloneElementWithStyles(element) {
var clonedEl = $(document.createElement(element.tagName));
// CSS styles to copy over from the generated node.
var stylesToCopy = ['font', 'color'];
// Copy css styles.
clonedEl.attr('style', getComputedStyles(stylesToCopy, element));
// Clone children.
$(element).contents().each(function() {
if (this.nodeType === Node.ELEMENT_NODE) {
clonedEl.append(cloneElementWithStyles(this));
} else {
clonedEl.append($(this).clone());
}
});
return clonedEl;
}
function getComputedStyles(stylesToCopy, element) {
var cssText = stylesToCopy.reduce(function(cssText, property) {
cssText += property + ':' + window.getComputedStyle(element)[property] + ';';
return cssText;
}, '');
return cssText;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment