1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Add minifyStyles plugin, test and dependencies.

This commit is contained in:
strarsis
2015-11-02 00:32:23 +01:00
parent 46e1d7b893
commit 16ea8c8057
4 changed files with 89 additions and 1 deletions

71
plugins/minifyStyles.js Executable file
View File

@ -0,0 +1,71 @@
'use strict';
exports.type = 'perItem';
exports.active = true;
exports.params = {
svgo: {}
};
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);
};
/**
* Minifies styles (<style> element + style attribute) using svgo
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author strarsis <strarsis@gmail.com>
*/
exports.fn = function(item, svgoOptions) {
if(item.elem) {
if(item.isElem('style')) {
var styleCss = item.content[0].text;
if(styleCss.length > 0) {
var styleCssMinified = minifyCss(styleCss, svgoOptions);
item.content[0].text = styleCssMinified;
}
}
if(item.hasAttr('style')) {
var itemCss = item.attr('style').value;
if(itemCss.length > 0) {
var itemCssMinified =
extractRuleCss(
minifyCss(
rulesToDummySelector(
itemCss
),
svgoOptions
)
);
item.attr('style').value = itemCssMinified;
}
}
}
return item;
};