mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
round viewBox too
This commit is contained in:
@ -37,24 +37,32 @@ exports.fn = function(item, params) {
|
|||||||
|
|
||||||
if (item.isElem()) {
|
if (item.isElem()) {
|
||||||
|
|
||||||
var match;
|
var floatPrecision = params.floatPrecision;
|
||||||
|
|
||||||
|
if (item.hasAttr('viewBox')) {
|
||||||
|
var nums = item.attr('viewBox').value.split(/[ ,]/g);
|
||||||
|
item.attr('viewBox').value = nums.map(function(num) {
|
||||||
|
return +num.toFixed(floatPrecision);
|
||||||
|
}).join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
item.eachAttr(function(attr) {
|
item.eachAttr(function(attr) {
|
||||||
match = attr.value.match(regNumericValues);
|
var match = attr.value.match(regNumericValues);
|
||||||
|
|
||||||
// if attribute value matches regNumericValues
|
// if attribute value matches regNumericValues
|
||||||
if (match) {
|
if (match) {
|
||||||
// round it to the fixed precision
|
// round it to the fixed precision
|
||||||
var num = +(+match[1]).toFixed(params.floatPrecision),
|
var num = +(+match[1]).toFixed(floatPrecision),
|
||||||
units = match[3] || '';
|
units = match[3] || '';
|
||||||
|
|
||||||
// convert absolute values to pixels
|
// convert absolute values to pixels
|
||||||
if (params.convertToPx && units && (units in absoluteLengths)) {
|
if (params.convertToPx && units && (units in absoluteLengths)) {
|
||||||
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
|
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(floatPrecision);
|
||||||
|
|
||||||
if (String(pxNum).length < match[0].length)
|
if (String(pxNum).length < match[0].length) {
|
||||||
num = pxNum,
|
num = pxNum;
|
||||||
units = 'px';
|
units = 'px';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// and remove leading zero
|
// and remove leading zero
|
||||||
|
@ -6,8 +6,7 @@ exports.active = false;
|
|||||||
|
|
||||||
exports.description = 'removes viewBox attribute when possible (disabled by default)';
|
exports.description = 'removes viewBox attribute when possible (disabled by default)';
|
||||||
|
|
||||||
var regViewBox = /^0\s*[,\s]\s*0\s*[,\s]\s*([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s*[,\s]\s*([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)$/,
|
var viewBoxElems = ['svg', 'pattern', 'symbol'];
|
||||||
viewBoxElems = ['svg', 'pattern'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove viewBox attr which coincides with a width/height box.
|
* Remove viewBox attr which coincides with a width/height box.
|
||||||
@ -33,15 +32,13 @@ exports.fn = function(item) {
|
|||||||
item.hasAttr('height')
|
item.hasAttr('height')
|
||||||
) {
|
) {
|
||||||
|
|
||||||
var match = item.attr('viewBox').value.match(regViewBox);
|
var nums = item.attr('viewBox').value.split(/[ ,]+/g);
|
||||||
|
|
||||||
if (match) {
|
if (
|
||||||
if (
|
item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
|
||||||
item.attr('width').value === match[1] &&
|
item.attr('height').value.replace(/px$/, '') === nums[3]
|
||||||
item.attr('height').value === match[3]
|
) {
|
||||||
) {
|
item.removeAttr('viewBox');
|
||||||
item.removeAttr('viewBox');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user