Skip to content

Commit

Permalink
Merge pull request svg#523 from lahmatiy/master
Browse files Browse the repository at this point in the history
Bump csso to 1.8.0 and use minifyBlock() for style attribute content compression instead of hack
  • Loading branch information
GreLI committed Mar 24, 2016
2 parents e69f0a2 + df9cd39 commit b80944c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"colors": "~1.1.2",
"whet.extend": "~0.9.9",
"mkdirp": "~0.5.1",
"csso": "~1.6.4"
"csso": "~1.8.0"
},
"devDependencies": {
"mocha": "~2.4.5",
Expand Down
31 changes: 2 additions & 29 deletions plugins/minifyStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ exports.description = 'minifies existing styles in svg';

var csso = require('csso');


// wraps css rules into a selector to make it parseable
var rulesToDummySelector = function(str) {
return '.dummy { ' + str + ' }';
};

// helper to extract css rules from full css selector
var extractRuleCss = function(str) {
var strEx = str.match(/\.dummy{(.*)}/i)[1];
return strEx;
};

// minifies css using csso
var minifyCss = function(css, options) {
return csso.minify(css, options);
};



/**
* Minifies styles (<style> element + style attribute) using svgo
*
Expand All @@ -46,23 +27,15 @@ exports.fn = function(item, svgoOptions) {
var styleCss = item.content[0].text || item.content[0].cdata || [],
DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';
if(styleCss.length > 0) {
var styleCssMinified = minifyCss(styleCss, svgoOptions);
var styleCssMinified = csso.minify(styleCss, svgoOptions);
item.content[0][DATA] = styleCssMinified;
}
}

if(item.hasAttr('style')) {
var itemCss = item.attr('style').value;
if(itemCss.length > 0) {
var itemCssMinified =
extractRuleCss(
minifyCss(
rulesToDummySelector(
itemCss
),
svgoOptions
)
);
var itemCssMinified = csso.minifyBlock(itemCss, svgoOptions);
item.attr('style').value = itemCssMinified;
}
}
Expand Down

0 comments on commit b80944c

Please sign in to comment.