diff --git a/.svgo.yml b/.svgo.yml index 95b04ee7..bd224a5f 100644 --- a/.svgo.yml +++ b/.svgo.yml @@ -32,16 +32,13 @@ plugins: active: true type: perItem - - name: removeDefaultPx - active: true - type: perItem - - - name: roundNumericValues + - name: cleanupNumericValues active: true type: perItem params: floatPrecision: 3 leadingZero: true + defaultPx: true - name: removeUnknownsAndDefaults active: true diff --git a/plugins/roundNumericValues.js b/plugins/cleanupNumericValues.js similarity index 65% rename from plugins/roundNumericValues.js rename to plugins/cleanupNumericValues.js index fe5e4761..e4b1efa8 100644 --- a/plugins/roundNumericValues.js +++ b/plugins/cleanupNumericValues.js @@ -4,7 +4,8 @@ var regNumericValues = /^([\-+]?\d*\.?\d+)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/, removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero; /** - * Round numeric values to the fixed precision. + * Round numeric values to the fixed precision, + * remove default 'px' units. * * @param {Object} item current iteration item * @param {Object} params plugin params @@ -12,7 +13,7 @@ var regNumericValues = /^([\-+]?\d*\.?\d+)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/, * * @author Kir Belevich */ -exports.roundNumericValues = function(item, params) { +exports.cleanupNumericValues = function(item, params) { if (item.isElem()) { @@ -23,15 +24,21 @@ exports.roundNumericValues = function(item, params) { // if attribute value matches regNumericValues if (match) { - // then round it to the fixed precision - var num = +(+match[1]).toFixed(params.floatPrecision) + (match[2] || ''); + // round it to the fixed precision + var num = +(+match[1]).toFixed(params.floatPrecision), + units = match[2] || ''; // and remove leading zero if (params.leadingZero) { num = removeLeadingZero(num); } - attr.value = num; + // remove default 'px' units + if (params.defaultPx && units === 'px') { + units = ''; + } + + attr.value = num + units; } }); diff --git a/plugins/removeDefaultPx.js b/plugins/removeDefaultPx.js deleted file mode 100644 index 25215b83..00000000 --- a/plugins/removeDefaultPx.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -var regValPx = /^([\-+]?\d*\.?\d+)px$/; - -/** - * Remove default "px" unit from attributes values. - * - * "One px unit is defined to be equal to one user unit. - * Thus, a length of 5px is the same as a length of 5" - * http://www.w3.org/TR/SVG/coords.html#Units - * - * @param {Object} item current iteration item - * @return {Boolean} if false, item will be filtered out - * - * @author Kir Belevich - */ -exports.removeDefaultPx = function(item) { - - if (item.elem) { - - item.eachAttr(function(attr) { - attr.value = attr.value.replace(regValPx, '$1'); - }); - - } - -}; diff --git a/test/plugins/roundNumericValues.01.orig.svg b/test/plugins/cleanupNumericValues.01.orig.svg similarity index 100% rename from test/plugins/roundNumericValues.01.orig.svg rename to test/plugins/cleanupNumericValues.01.orig.svg diff --git a/test/plugins/roundNumericValues.01.should.svg b/test/plugins/cleanupNumericValues.01.should.svg similarity index 75% rename from test/plugins/roundNumericValues.01.should.svg rename to test/plugins/cleanupNumericValues.01.should.svg index 05a6ba6e..66fc086f 100644 --- a/test/plugins/roundNumericValues.01.should.svg +++ b/test/plugins/cleanupNumericValues.01.should.svg @@ -1,3 +1,3 @@ - + test diff --git a/test/plugins/removeDefaultPx.01.orig.svg b/test/plugins/removeDefaultPx.01.orig.svg deleted file mode 100644 index 7a630666..00000000 --- a/test/plugins/removeDefaultPx.01.orig.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/test/plugins/removeDefaultPx.01.should.svg b/test/plugins/removeDefaultPx.01.should.svg deleted file mode 100644 index 108bdb3d..00000000 --- a/test/plugins/removeDefaultPx.01.should.svg +++ /dev/null @@ -1,3 +0,0 @@ - - -