1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

removeUselessStrokeAndFill is back. Checks for inherited attrs and references.

This commit is contained in:
GreLI
2015-02-23 19:53:48 +03:00
parent 02dcf78d74
commit d3e4f63ae5
6 changed files with 90 additions and 24 deletions

View File

@ -2,15 +2,16 @@
exports.type = 'perItem';
exports.active = false;
exports.active = true;
exports.params = {
stroke: true,
fill: true
};
var regStrokeProps = /^stroke/,
regFillProps = /^fill/;
var shape = require('./_collections').elemsGroups.shape,
regStrokeProps = /^stroke/,
regFillProps = /^fill-/;
/**
* Remove useless stroke and fill attrs.
@ -23,28 +24,41 @@ var regStrokeProps = /^stroke/,
*/
exports.fn = function(item, params) {
if (item.isElem()) {
if (item.isElem(shape) && !item.computedAttr('id')) {
var stroke = params.stroke && item.computedAttr('stroke'),
fill = params.fill && !item.computedAttr('fill', 'none');
// remove stroke*
if (
params.stroke &&
(!item.hasAttr('stroke') ||
item.hasAttr('stroke-opacity', '0') ||
item.hasAttr('stroke-width', '0')
(!stroke ||
stroke == 'none' ||
item.computedAttr('stroke-opacity', '0') ||
item.computedAttr('stroke-width', '0')
)
) {
var parentStroke = item.parentNode.computedAttr('stroke'),
declineStroke = parentStroke && parentStroke != 'none';
item.eachAttr(function(attr) {
if (regStrokeProps.test(attr.name)) {
item.removeAttr(attr.name);
}
});
if (declineStroke) item.addAttr({
name: 'stroke',
value: 'none',
prefix: '',
local: 'stroke'
});
}
// remove fill*
if (
params.fill &&
item.hasAttr('fill', 'none') ||
item.hasAttr('fill-opacity', '0')
(!fill || item.computedAttr('fill-opacity', '0'))
) {
item.eachAttr(function(attr) {
if (regFillProps.test(attr.name)) {
@ -52,12 +66,17 @@ exports.fn = function(item, params) {
}
});
item.addAttr({
name: 'fill',
value: 'none',
prefix: '',
local: 'fill'
});
if (fill) {
if (item.hasAttr('fill'))
item.attr('fill').value = 'none';
else
item.addAttr({
name: 'fill',
value: 'none',
prefix: '',
local: 'fill'
});
}
}
}