Skip to content

Instantly share code, notes, and snippets.

@caccialdo
Created March 6, 2014 15:26
Show Gist options
  • Save caccialdo/9392095 to your computer and use it in GitHub Desktop.
Save caccialdo/9392095 to your computer and use it in GitHub Desktop.
CSS styled SVG to Standalone SVG
/*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