1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

merge plugins/roundNumericValues and plugins/removeDefaultPx into one plugins/cleanupNumericValues

This commit is contained in:
deepsweet
2012-11-29 18:42:41 +02:00
parent 67daff27b1
commit 140daaeaef
7 changed files with 15 additions and 44 deletions

View File

@ -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

View File

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

View File

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

View File

Before

Width:  |  Height:  |  Size: 110 B

After

Width:  |  Height:  |  Size: 110 B

View File

@ -1,3 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="50.124%" height="20px" x=".266" y="-.235">
<svg xmlns="http://www.w3.org/2000/svg" width="50.124%" height="20" x=".266" y="-.235">
test
</svg>

Before

Width:  |  Height:  |  Size: 106 B

After

Width:  |  Height:  |  Size: 104 B

View File

@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50.123px">
<path d="..."/>
</svg>

Before

Width:  |  Height:  |  Size: 100 B

View File

@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50.123">
<path d="..."/>
</svg>

Before

Width:  |  Height:  |  Size: 96 B