Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3e4f63

Browse files
committed
removeUselessStrokeAndFill is back. Checks for inherited attrs and references.
1 parent 02dcf78 commit d3e4f63

File tree

6 files changed

+90
-24
lines changed

6 files changed

+90
-24
lines changed

.svgo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ plugins:
2020
- removeMetadata
2121
- removeEditorsNSData
2222
- cleanupAttrs
23+
- cleanupIDs
2324
- convertStyleToAttrs
2425
- removeRasterImages
2526
- cleanupNumericValues
@@ -41,7 +42,6 @@ plugins:
4142
- removeEmptyAttrs
4243
- removeEmptyContainers
4344
- mergePaths
44-
- cleanupIDs
4545
- removeUnusedNS
4646
- transformsWithOnePath
4747
- sortAttrs

lib/svgo/jsAPI.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ JSAPI.prototype.isElem = function(param) {
8484
JSAPI.prototype.computedAttr = function(name, val) {
8585
if (!arguments.length) return;
8686

87-
for (var elem = this; elem && !elem.hasAttr(name); elem = elem.parentNode);
87+
for (var elem = this; elem && (!elem.hasAttr(name) || !elem.attr(name).value); elem = elem.parentNode);
8888

89-
if (elem && elem.hasAttr(name, val))
90-
return elem.attrs[name];
89+
if (val != null) {
90+
return elem ? elem.hasAttr(name, val) : false;
91+
} else if (elem && elem.hasAttr(name)) {
92+
return elem.attrs[name].value;
93+
}
9194

9295
};
9396

plugins/_path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/,
77
transformsMultiply = require('./_transforms').transformsMultiply,
88
collections = require('./_collections.js'),
99
referencesProps = collections.referencesProps,
10-
defaultStrokeWidth = { value: collections.attrsGroupsDefaults.presentation['stroke-width'] },
10+
defaultStrokeWidth = collections.attrsGroupsDefaults.presentation['stroke-width'],
1111
cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
1212
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;
1313

@@ -222,7 +222,7 @@ exports.applyTransforms = function(elem, path, applyTransformsStroked, floatPrec
222222
return path;
223223
}
224224
if (sx !== 1){
225-
var strokeWidth = (elem.computedAttr('stroke-width') || defaultStrokeWidth).value;
225+
var strokeWidth = elem.computedAttr('stroke-width') || defaultStrokeWidth;
226226

227227
if (elem.hasAttr('stroke-width')){
228228
elem.attrs['stroke-width'].value = elem.attrs['stroke-width'].value.trim()

plugins/removeUselessStrokeAndFill.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
exports.type = 'perItem';
44

5-
exports.active = false;
5+
exports.active = true;
66

77
exports.params = {
88
stroke: true,
99
fill: true
1010
};
1111

12-
var regStrokeProps = /^stroke/,
13-
regFillProps = /^fill/;
12+
var shape = require('./_collections').elemsGroups.shape,
13+
regStrokeProps = /^stroke/,
14+
regFillProps = /^fill-/;
1415

1516
/**
1617
* Remove useless stroke and fill attrs.
@@ -23,41 +24,59 @@ var regStrokeProps = /^stroke/,
2324
*/
2425
exports.fn = function(item, params) {
2526

26-
if (item.isElem()) {
27+
if (item.isElem(shape) && !item.computedAttr('id')) {
28+
29+
var stroke = params.stroke && item.computedAttr('stroke'),
30+
fill = params.fill && !item.computedAttr('fill', 'none');
2731

2832
// remove stroke*
2933
if (
3034
params.stroke &&
31-
(!item.hasAttr('stroke') ||
32-
item.hasAttr('stroke-opacity', '0') ||
33-
item.hasAttr('stroke-width', '0')
35+
(!stroke ||
36+
stroke == 'none' ||
37+
item.computedAttr('stroke-opacity', '0') ||
38+
item.computedAttr('stroke-width', '0')
3439
)
3540
) {
41+
var parentStroke = item.parentNode.computedAttr('stroke'),
42+
declineStroke = parentStroke && parentStroke != 'none';
43+
3644
item.eachAttr(function(attr) {
3745
if (regStrokeProps.test(attr.name)) {
3846
item.removeAttr(attr.name);
3947
}
4048
});
49+
50+
if (declineStroke) item.addAttr({
51+
name: 'stroke',
52+
value: 'none',
53+
prefix: '',
54+
local: 'stroke'
55+
});
4156
}
4257

4358
// remove fill*
4459
if (
4560
params.fill &&
46-
item.hasAttr('fill', 'none') ||
47-
item.hasAttr('fill-opacity', '0')
61+
(!fill || item.computedAttr('fill-opacity', '0'))
4862
) {
4963
item.eachAttr(function(attr) {
5064
if (regFillProps.test(attr.name)) {
5165
item.removeAttr(attr.name);
5266
}
5367
});
5468

55-
item.addAttr({
56-
name: 'fill',
57-
value: 'none',
58-
prefix: '',
59-
local: 'fill'
60-
});
69+
if (fill) {
70+
if (item.hasAttr('fill'))
71+
item.attr('fill').value = 'none';
72+
else
73+
item.addAttr({
74+
name: 'fill',
75+
value: 'none',
76+
prefix: '',
77+
local: 'fill'
78+
});
79+
}
6180
}
6281

6382
}
Lines changed: 26 additions & 0 deletions
Loading
Lines changed: 21 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)