Skip to content

Commit

Permalink
removeUselessStrokeAndFill is back. Checks for inherited attrs and re…
Browse files Browse the repository at this point in the history
…ferences.
  • Loading branch information
GreLI committed Feb 23, 2015
1 parent 02dcf78 commit d3e4f63
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ plugins:
- removeMetadata
- removeEditorsNSData
- cleanupAttrs
- cleanupIDs
- convertStyleToAttrs
- removeRasterImages
- cleanupNumericValues
Expand All @@ -41,7 +42,6 @@ plugins:
- removeEmptyAttrs
- removeEmptyContainers
- mergePaths
- cleanupIDs
- removeUnusedNS
- transformsWithOnePath
- sortAttrs
Expand Down
9 changes: 6 additions & 3 deletions lib/svgo/jsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ JSAPI.prototype.isElem = function(param) {
JSAPI.prototype.computedAttr = function(name, val) {
if (!arguments.length) return;

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

if (elem && elem.hasAttr(name, val))
return elem.attrs[name];
if (val != null) {
return elem ? elem.hasAttr(name, val) : false;
} else if (elem && elem.hasAttr(name)) {
return elem.attrs[name].value;
}

};

Expand Down
4 changes: 2 additions & 2 deletions plugins/_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/,
transformsMultiply = require('./_transforms').transformsMultiply,
collections = require('./_collections.js'),
referencesProps = collections.referencesProps,
defaultStrokeWidth = { value: collections.attrsGroupsDefaults.presentation['stroke-width'] },
defaultStrokeWidth = collections.attrsGroupsDefaults.presentation['stroke-width'],
cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;

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

if (elem.hasAttr('stroke-width')){
elem.attrs['stroke-width'].value = elem.attrs['stroke-width'].value.trim()
Expand Down
49 changes: 34 additions & 15 deletions plugins/removeUselessStrokeAndFill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,41 +24,59 @@ 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)) {
item.removeAttr(attr.name);
}
});

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'
});
}
}

}
Expand Down
26 changes: 26 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 21 additions & 3 deletions test/plugins/removeUselessStrokeAndFill.02.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 d3e4f63

Please sign in to comment.