mirror of
https://github.com/svg/svgo.git
synced 2025-07-28 09:22:00 +03:00
Convert cleanupListOfValues and cleanupNumericValues to visitor (#1521)
SLightly refactored
This commit is contained in:
@ -3,20 +3,10 @@
|
||||
const { removeLeadingZero } = require('../lib/svgo/tools.js');
|
||||
|
||||
exports.name = 'cleanupListOfValues';
|
||||
|
||||
exports.type = 'perItem';
|
||||
|
||||
exports.type = 'visitor';
|
||||
exports.active = false;
|
||||
|
||||
exports.description = 'rounds list of values to the fixed precision';
|
||||
|
||||
exports.params = {
|
||||
floatPrecision: 3,
|
||||
leadingZero: true,
|
||||
defaultPx: true,
|
||||
convertToPx: true,
|
||||
};
|
||||
|
||||
const regNumericValues = /^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/;
|
||||
const regSeparator = /\s+,?\s*|,\s*/;
|
||||
const absoluteLengths = {
|
||||
@ -42,88 +32,48 @@ const absoluteLengths = {
|
||||
* <polygon points="208.251 77.131 223.069 ... "/>
|
||||
*
|
||||
*
|
||||
* @param {Object} item current iteration item
|
||||
* @param {Object} params plugin params
|
||||
* @return {Boolean} if false, item will be filtered out
|
||||
*
|
||||
* @author kiyopikko
|
||||
*/
|
||||
exports.fn = function (item, params) {
|
||||
if (item.type !== 'element') {
|
||||
return;
|
||||
}
|
||||
exports.fn = (root, params) => {
|
||||
const {
|
||||
floatPrecision = 3,
|
||||
leadingZero = true,
|
||||
defaultPx = true,
|
||||
convertToPx = true,
|
||||
} = params;
|
||||
|
||||
if (item.attributes.points != null) {
|
||||
item.attributes.points = roundValues(item.attributes.points);
|
||||
}
|
||||
const roundValues = (lists) => {
|
||||
const roundedList = [];
|
||||
|
||||
if (item.attributes['enable-background'] != null) {
|
||||
item.attributes['enable-background'] = roundValues(
|
||||
item.attributes['enable-background']
|
||||
);
|
||||
}
|
||||
|
||||
if (item.attributes.viewBox != null) {
|
||||
item.attributes.viewBox = roundValues(item.attributes.viewBox);
|
||||
}
|
||||
|
||||
if (item.attributes['stroke-dasharray'] != null) {
|
||||
item.attributes['stroke-dasharray'] = roundValues(
|
||||
item.attributes['stroke-dasharray']
|
||||
);
|
||||
}
|
||||
|
||||
if (item.attributes.dx != null) {
|
||||
item.attributes.dx = roundValues(item.attributes.dx);
|
||||
}
|
||||
|
||||
if (item.attributes.dy != null) {
|
||||
item.attributes.dy = roundValues(item.attributes.dy);
|
||||
}
|
||||
|
||||
if (item.attributes.x != null) {
|
||||
item.attributes.x = roundValues(item.attributes.x);
|
||||
}
|
||||
|
||||
if (item.attributes.y != null) {
|
||||
item.attributes.y = roundValues(item.attributes.y);
|
||||
}
|
||||
|
||||
function roundValues(lists) {
|
||||
var num,
|
||||
units,
|
||||
match,
|
||||
matchNew,
|
||||
listsArr = lists.split(regSeparator),
|
||||
roundedList = [];
|
||||
|
||||
for (const elem of listsArr) {
|
||||
match = elem.match(regNumericValues);
|
||||
matchNew = elem.match(/new/);
|
||||
for (const elem of lists.split(regSeparator)) {
|
||||
const match = elem.match(regNumericValues);
|
||||
const matchNew = elem.match(/new/);
|
||||
|
||||
// if attribute value matches regNumericValues
|
||||
if (match) {
|
||||
// round it to the fixed precision
|
||||
(num = +(+match[1]).toFixed(params.floatPrecision)),
|
||||
(units = match[3] || '');
|
||||
let num = Number(Number(match[1]).toFixed(floatPrecision));
|
||||
let units = match[3] || '';
|
||||
|
||||
// convert absolute values to pixels
|
||||
if (params.convertToPx && units && units in absoluteLengths) {
|
||||
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(
|
||||
params.floatPrecision
|
||||
if (convertToPx && units && units in absoluteLengths) {
|
||||
const pxNum = Number(
|
||||
(absoluteLengths[units] * match[1]).toFixed(floatPrecision)
|
||||
);
|
||||
|
||||
if (String(pxNum).length < match[0].length)
|
||||
(num = pxNum), (units = 'px');
|
||||
if (pxNum.toString().length < match[0].length) {
|
||||
num = pxNum;
|
||||
units = 'px';
|
||||
}
|
||||
}
|
||||
|
||||
// and remove leading zero
|
||||
if (params.leadingZero) {
|
||||
if (leadingZero) {
|
||||
num = removeLeadingZero(num);
|
||||
}
|
||||
|
||||
// remove default 'px' units
|
||||
if (params.defaultPx && units === 'px') {
|
||||
if (defaultPx && units === 'px') {
|
||||
units = '';
|
||||
}
|
||||
|
||||
@ -138,5 +88,47 @@ exports.fn = function (item, params) {
|
||||
}
|
||||
|
||||
return roundedList.join(' ');
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
element: {
|
||||
enter: (node) => {
|
||||
if (node.attributes.points != null) {
|
||||
node.attributes.points = roundValues(node.attributes.points);
|
||||
}
|
||||
|
||||
if (node.attributes['enable-background'] != null) {
|
||||
node.attributes['enable-background'] = roundValues(
|
||||
node.attributes['enable-background']
|
||||
);
|
||||
}
|
||||
|
||||
if (node.attributes.viewBox != null) {
|
||||
node.attributes.viewBox = roundValues(node.attributes.viewBox);
|
||||
}
|
||||
|
||||
if (node.attributes['stroke-dasharray'] != null) {
|
||||
node.attributes['stroke-dasharray'] = roundValues(
|
||||
node.attributes['stroke-dasharray']
|
||||
);
|
||||
}
|
||||
|
||||
if (node.attributes.dx != null) {
|
||||
node.attributes.dx = roundValues(node.attributes.dx);
|
||||
}
|
||||
|
||||
if (node.attributes.dy != null) {
|
||||
node.attributes.dy = roundValues(node.attributes.dy);
|
||||
}
|
||||
|
||||
if (node.attributes.x != null) {
|
||||
node.attributes.x = roundValues(node.attributes.x);
|
||||
}
|
||||
|
||||
if (node.attributes.y != null) {
|
||||
node.attributes.y = roundValues(node.attributes.y);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user