mirror of
https://github.com/svg/svgo.git
synced 2025-08-09 02:22:08 +03:00
MinifyStyles: unix line-endings
This commit is contained in:
@@ -1,71 +1,71 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports.type = 'perItem';
|
exports.type = 'perItem';
|
||||||
|
|
||||||
exports.active = true;
|
exports.active = true;
|
||||||
|
|
||||||
exports.params = {
|
exports.params = {
|
||||||
svgo: {}
|
svgo: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.description = 'minifies existing styles in svg';
|
exports.description = 'minifies existing styles in svg';
|
||||||
|
|
||||||
var csso = require('csso');
|
var csso = require('csso');
|
||||||
|
|
||||||
|
|
||||||
// wraps css rules into a selector to make it parseable
|
// wraps css rules into a selector to make it parseable
|
||||||
var rulesToDummySelector = function(str) {
|
var rulesToDummySelector = function(str) {
|
||||||
return '.dummy { ' + str + ' }';
|
return '.dummy { ' + str + ' }';
|
||||||
};
|
};
|
||||||
|
|
||||||
// helper to extract css rules from full css selector
|
// helper to extract css rules from full css selector
|
||||||
var extractRuleCss = function(str) {
|
var extractRuleCss = function(str) {
|
||||||
var strEx = str.match(/\.dummy{(.*)}/i)[1];
|
var strEx = str.match(/\.dummy{(.*)}/i)[1];
|
||||||
return strEx;
|
return strEx;
|
||||||
};
|
};
|
||||||
|
|
||||||
// minifies css using csso
|
// minifies css using csso
|
||||||
var minifyCss = function(css, options) {
|
var minifyCss = function(css, options) {
|
||||||
return csso.minify(css, options);
|
return csso.minify(css, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minifies styles (<style> element + style attribute) using svgo
|
* Minifies styles (<style> element + style attribute) using svgo
|
||||||
*
|
*
|
||||||
* @param {Object} item current iteration item
|
* @param {Object} item current iteration item
|
||||||
* @return {Boolean} if false, item will be filtered out
|
* @return {Boolean} if false, item will be filtered out
|
||||||
*
|
*
|
||||||
* @author strarsis <strarsis@gmail.com>
|
* @author strarsis <strarsis@gmail.com>
|
||||||
*/
|
*/
|
||||||
exports.fn = function(item, svgoOptions) {
|
exports.fn = function(item, svgoOptions) {
|
||||||
|
|
||||||
if(item.elem) {
|
if(item.elem) {
|
||||||
if(item.isElem('style')) {
|
if(item.isElem('style')) {
|
||||||
var styleCss = item.content[0].text;
|
var styleCss = item.content[0].text;
|
||||||
if(styleCss.length > 0) {
|
if(styleCss.length > 0) {
|
||||||
var styleCssMinified = minifyCss(styleCss, svgoOptions);
|
var styleCssMinified = minifyCss(styleCss, svgoOptions);
|
||||||
item.content[0].text = styleCssMinified;
|
item.content[0].text = styleCssMinified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(item.hasAttr('style')) {
|
if(item.hasAttr('style')) {
|
||||||
var itemCss = item.attr('style').value;
|
var itemCss = item.attr('style').value;
|
||||||
if(itemCss.length > 0) {
|
if(itemCss.length > 0) {
|
||||||
var itemCssMinified =
|
var itemCssMinified =
|
||||||
extractRuleCss(
|
extractRuleCss(
|
||||||
minifyCss(
|
minifyCss(
|
||||||
rulesToDummySelector(
|
rulesToDummySelector(
|
||||||
itemCss
|
itemCss
|
||||||
),
|
),
|
||||||
svgoOptions
|
svgoOptions
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
item.attr('style').value = itemCssMinified;
|
item.attr('style').value = itemCssMinified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user