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:
@ -32,16 +32,13 @@ plugins:
|
|||||||
active: true
|
active: true
|
||||||
type: perItem
|
type: perItem
|
||||||
|
|
||||||
- name: removeDefaultPx
|
- name: cleanupNumericValues
|
||||||
active: true
|
|
||||||
type: perItem
|
|
||||||
|
|
||||||
- name: roundNumericValues
|
|
||||||
active: true
|
active: true
|
||||||
type: perItem
|
type: perItem
|
||||||
params:
|
params:
|
||||||
floatPrecision: 3
|
floatPrecision: 3
|
||||||
leadingZero: true
|
leadingZero: true
|
||||||
|
defaultPx: true
|
||||||
|
|
||||||
- name: removeUnknownsAndDefaults
|
- name: removeUnknownsAndDefaults
|
||||||
active: true
|
active: true
|
||||||
|
@ -4,7 +4,8 @@ var regNumericValues = /^([\-+]?\d*\.?\d+)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
|
|||||||
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;
|
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} item current iteration item
|
||||||
* @param {Object} params plugin params
|
* @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
|
* @author Kir Belevich
|
||||||
*/
|
*/
|
||||||
exports.roundNumericValues = function(item, params) {
|
exports.cleanupNumericValues = function(item, params) {
|
||||||
|
|
||||||
if (item.isElem()) {
|
if (item.isElem()) {
|
||||||
|
|
||||||
@ -23,15 +24,21 @@ exports.roundNumericValues = function(item, params) {
|
|||||||
|
|
||||||
// if attribute value matches regNumericValues
|
// if attribute value matches regNumericValues
|
||||||
if (match) {
|
if (match) {
|
||||||
// then round it to the fixed precision
|
// round it to the fixed precision
|
||||||
var num = +(+match[1]).toFixed(params.floatPrecision) + (match[2] || '');
|
var num = +(+match[1]).toFixed(params.floatPrecision),
|
||||||
|
units = match[2] || '';
|
||||||
|
|
||||||
// and remove leading zero
|
// and remove leading zero
|
||||||
if (params.leadingZero) {
|
if (params.leadingZero) {
|
||||||
num = removeLeadingZero(num);
|
num = removeLeadingZero(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
attr.value = num;
|
// remove default 'px' units
|
||||||
|
if (params.defaultPx && units === 'px') {
|
||||||
|
units = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
attr.value = num + units;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -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');
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
Before Width: | Height: | Size: 110 B After Width: | Height: | Size: 110 B |
@ -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
|
test
|
||||||
</svg>
|
</svg>
|
Before Width: | Height: | Size: 106 B After Width: | Height: | Size: 104 B |
@ -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 |
@ -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 |
Reference in New Issue
Block a user