diff --git a/.svgo.yml b/.svgo.yml index 5586aed0..9cccbb2e 100644 --- a/.svgo.yml +++ b/.svgo.yml @@ -103,6 +103,14 @@ plugins: tspan: true tref: true + - name: moveElemsAttrsToGroup + active: true + type: perItemReverse + + - name: moveGroupAttrsToElems + active: true + type: perItemReverse + - name: convertPathData active: true type: perItem @@ -117,10 +125,6 @@ plugins: leadingZero: true negativeExtraSpace: true - - name: moveElemsAttrsToGroup - active: true - type: perItemReverse - - name: convertTransform active: true type: perItem diff --git a/plugins/_collections.js b/plugins/_collections.js index 73a43d23..3c305ea9 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -13,6 +13,8 @@ exports.elemsGroups = { filterPrimitive: ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence'] }; +exports.pathElems = ['path', 'glyph', 'missing-glyph']; + // var defaults = exports.defaults = { // 'externalResourcesRequired': 'false', // 'xlink:type': 'simple' diff --git a/plugins/convertPathData.js b/plugins/convertPathData.js index c2bde214..e749a462 100644 --- a/plugins/convertPathData.js +++ b/plugins/convertPathData.js @@ -3,7 +3,7 @@ var cleanupOutData = require('../lib/svgo/tools').cleanupOutData, regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/, regPathData = /[\-+]?\d*\.?\d+([eE][\-+]?\d+)?/g, - pathElems = ['path', 'glyph', 'missing-glyph'], + pathElems = require('./_collections.js').pathElems, hasMarkerMid, transform2js = require('./_transforms.js').transform2js, transformsMultiply = require('./_transforms.js').transformsMultiply; diff --git a/plugins/moveElemsAttrsToGroup.js b/plugins/moveElemsAttrsToGroup.js index ccf0d532..37a82a97 100644 --- a/plugins/moveElemsAttrsToGroup.js +++ b/plugins/moveElemsAttrsToGroup.js @@ -1,6 +1,7 @@ 'use strict'; -var inheritableAttrs = require('./_collections').inheritableAttrs; +var inheritableAttrs = require('./_collections').inheritableAttrs, + pathElems = require('./_collections.js').pathElems; /** * Collapse content's intersected and inheritable @@ -32,34 +33,47 @@ exports.moveElemsAttrsToGroup = function(item) { var intersection = {}, hasTransform = false, - every = item.content.every(function(g) { - if (g.elem && g.attrs) { + intersected = item.content.every(function(inner) { + if (inner.isElem() && inner.hasAttr()) { if (!Object.keys(intersection).length) { - intersection = g.attrs; + intersection = inner.attrs; } else { - intersection = intersectInheritableAttrs(intersection, g.attrs); + intersection = intersectInheritableAttrs(intersection, inner.attrs); if (!intersection) return false; } return true; } + }), + allPath = item.content.every(function(inner) { + return inner.isElem(pathElems); }); - if (every) { + if (intersected) { item.content.forEach(function(g) { for (var name in intersection) { - g.removeAttr(name); - if (name === 'transform') { - if (!hasTransform && item.hasAttr('transform')) { - item.attr('transform').value += ' ' + intersection[name].value; - hasTransform = true; + if (!allPath || name !== 'transform') { + + g.removeAttr(name); + + if (name === 'transform') { + if (!hasTransform) { + if (item.hasAttr('transform')) { + item.attr('transform').value += ' ' + intersection[name].value; + } else { + item.addAttr(intersection[name]); + } + + hasTransform = true; + } + } else { + item.addAttr(intersection[name]); } - } else { - item.addAttr(intersection[name]); + } } diff --git a/plugins/moveGroupAttrsToElems.js b/plugins/moveGroupAttrsToElems.js new file mode 100644 index 00000000..0a9f2aff --- /dev/null +++ b/plugins/moveGroupAttrsToElems.js @@ -0,0 +1,46 @@ +'use strict'; + +var pathElems = require('./_collections.js').pathElems; + +/** + * Move group attrs to the content elements. + * + * @example + * + * + * + * + * ⬇ + * + * + * + * + * + * @param {Object} item current iteration item + * @return {Boolean} if false, item will be filtered out + * + * @author Kir Belevich + */ +exports.moveGroupAttrsToElems = function(item) { + + // move group transform attr to content's pathElems + if ( + item.isElem('g') && + item.hasAttr('transform') && + !item.isEmpty() && + item.content.every(function(inner) { + return inner.isElem(pathElems); + }) + ) { + item.content.forEach(function(inner) { + if (inner.hasAttr('transform')) { + inner.attr('transform').value = item.attr('transform').value + ' ' + inner.attr('transform').value; + } else { + inner.addAttr(item.attr('transform')); + } + }); + + item.removeAttr('transform'); + } + +}; diff --git a/test/plugins/moveElemsAttrsToGroup.06.orig.svg b/test/plugins/moveElemsAttrsToGroup.06.orig.svg new file mode 100644 index 00000000..ca0ba468 --- /dev/null +++ b/test/plugins/moveElemsAttrsToGroup.06.orig.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/plugins/moveElemsAttrsToGroup.06.should.svg b/test/plugins/moveElemsAttrsToGroup.06.should.svg new file mode 100644 index 00000000..ca0ba468 --- /dev/null +++ b/test/plugins/moveElemsAttrsToGroup.06.should.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/plugins/moveGroupAttrsToElems.01.orig.svg b/test/plugins/moveGroupAttrsToElems.01.orig.svg new file mode 100644 index 00000000..d3e21944 --- /dev/null +++ b/test/plugins/moveGroupAttrsToElems.01.orig.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/plugins/moveGroupAttrsToElems.01.should.svg b/test/plugins/moveGroupAttrsToElems.01.should.svg new file mode 100644 index 00000000..799b0d78 --- /dev/null +++ b/test/plugins/moveGroupAttrsToElems.01.should.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/plugins/moveGroupAttrsToElems.02.orig.svg b/test/plugins/moveGroupAttrsToElems.02.orig.svg new file mode 100644 index 00000000..a1c1457c --- /dev/null +++ b/test/plugins/moveGroupAttrsToElems.02.orig.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/plugins/moveGroupAttrsToElems.02.should.svg b/test/plugins/moveGroupAttrsToElems.02.should.svg new file mode 100644 index 00000000..6a2edc16 --- /dev/null +++ b/test/plugins/moveGroupAttrsToElems.02.should.svg @@ -0,0 +1,6 @@ + + + + + +