diff --git a/.svgo.yml b/.svgo.yml index 4a13b153..67dc8874 100644 --- a/.svgo.yml +++ b/.svgo.yml @@ -20,6 +20,7 @@ plugins: - removeMetadata - removeEditorsNSData - cleanupAttrs + - cleanupIDs - convertStyleToAttrs - removeRasterImages - cleanupNumericValues @@ -41,7 +42,6 @@ plugins: - removeEmptyAttrs - removeEmptyContainers - mergePaths - - cleanupIDs - removeUnusedNS - transformsWithOnePath - sortAttrs diff --git a/lib/svgo/jsAPI.js b/lib/svgo/jsAPI.js index edc779ee..a5441d5f 100644 --- a/lib/svgo/jsAPI.js +++ b/lib/svgo/jsAPI.js @@ -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; + } }; diff --git a/plugins/_path.js b/plugins/_path.js index b1aa4f84..2ee5392c 100644 --- a/plugins/_path.js +++ b/plugins/_path.js @@ -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; @@ -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() diff --git a/plugins/removeUselessStrokeAndFill.js b/plugins/removeUselessStrokeAndFill.js index e8e93a94..5e2819c8 100644 --- a/plugins/removeUselessStrokeAndFill.js +++ b/plugins/removeUselessStrokeAndFill.js @@ -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' + }); + } } } diff --git a/test/plugins/removeUselessStrokeAndFill.01.svg b/test/plugins/removeUselessStrokeAndFill.01.svg index f656e821..d2fd0e2b 100644 --- a/test/plugins/removeUselessStrokeAndFill.01.svg +++ b/test/plugins/removeUselessStrokeAndFill.01.svg @@ -1,15 +1,41 @@ + + + + + + + + + + + + + @@@ + + + + + + + + + + + + + diff --git a/test/plugins/removeUselessStrokeAndFill.02.svg b/test/plugins/removeUselessStrokeAndFill.02.svg index 60eb8673..7faca215 100644 --- a/test/plugins/removeUselessStrokeAndFill.02.svg +++ b/test/plugins/removeUselessStrokeAndFill.02.svg @@ -1,13 +1,31 @@ + + + + + - + + + + + @@@ - - + + + + + + + + + + +