Created
March 6, 2014 15:26
-
-
Save caccialdo/9392095 to your computer and use it in GitHub Desktop.
CSS styled SVG to Standalone SVG
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
/*jslint vars:true, regexp: true*/ | |
/*global copy*/ | |
(function () { | |
var svg = document.querySelector('svg'), | |
styles = [ | |
'stroke-width', | |
'color', | |
'stroke', | |
'fill', | |
'opacity', | |
'font-family', | |
'font-weight', | |
'font-size', | |
'display' | |
], | |
children = svg.querySelectorAll('*'); | |
children = [].slice.call(children); | |
svg.setAttribute('title', 'test2'); | |
svg.setAttribute('version', 1.1); | |
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); | |
function camelToDash(str) { | |
return str.replace(/\W+/g, '-').replace(/([a-z\d])([A-Z])/g, '$1-$2'); | |
} | |
function dashToCamel(str) { | |
return str.replace(/\W+(.)/g, function (x, chr) { | |
return chr.toUpperCase(); | |
}); | |
} | |
children.forEach(function (el) { | |
var style = window.getComputedStyle(el); | |
styles.forEach(function (str) { | |
el.style[dashToCamel(str)] = style.getPropertyValue(str); | |
}); | |
}); | |
var html = svg.outerHTML; | |
copy(html); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment