Skip to content

Commit

Permalink
new plugin: remove useless 'stroke' and 'fill' attrs (close svg#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Dec 11, 2012
1 parent d75c830 commit db91885
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 8 deletions.
23 changes: 15 additions & 8 deletions .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ plugins:
leadingZero: true
defaultPx: true

- name: convertColors
active: true
type: perItem
params:
names2hex: true
rgb2hex: true
shorthex: true

- name: removeUnknownsAndDefaults
active: true
type: perItem
Expand All @@ -49,6 +57,13 @@ plugins:
unknownAttrs: true
defaultAttrs: true

- name: removeUselessStrokeAndFill
active: true
type: perItem
params:
stroke: true
fill: true

- name: removeViewBox
active: true
type: perItem
Expand Down Expand Up @@ -88,14 +103,6 @@ plugins:
tspan: true
tref: true

- name: convertColors
active: true
type: perItem
params:
names2hex: true
rgb2hex: true
shorthex: true

- name: convertPathData
active: true
type: perItem
Expand Down
56 changes: 56 additions & 0 deletions plugins/removeUselessStrokeAndFill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var regStrokeProps = /^stroke/,
regFillProps = /^fill/;

/**
* Remove useless stroke and fill attrs.
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*/
exports.removeUselessStrokeAndFill = function(item, params) {

if (item.isElem()) {

// remove stroke*
if (
params.stroke &&
(!item.hasAttr('stroke') ||
item.hasAttr('stroke-opacity', '0') ||
item.hasAttr('stroke-width', '0')
)
) {
item.eachAttr(function(attr) {
if (regStrokeProps.test(attr.name)) {
item.removeAttr(attr.name);
}
});
}

// remove fill*
if (
params.fill &&
item.hasAttr('fill', 'none') ||
item.hasAttr('fill-opacity', '0')
) {
item.eachAttr(function(attr) {
if (regFillProps.test(attr.name)) {
item.removeAttr(attr.name);
}
});

item.addAttr({
name: 'fill',
value: 'none',
prefix: '',
local: 'fill'
});
}

}

};
5 changes: 5 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.01.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.01.should.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.02.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.02.should.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit db91885

Please sign in to comment.