-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minifyStyles plugin, test and dependencies.
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
*/ | ||
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; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.