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

Preverve viewBox in nested <svg>

This commit is contained in:
Bogdan Chadkin
2021-03-06 13:39:31 +03:00
parent a2b0e73767
commit 28c01cfe65
3 changed files with 52 additions and 22 deletions

View File

@ -23,26 +23,27 @@ var viewBoxElems = ['svg', 'pattern', 'symbol'];
*
* @author Kir Belevich
*/
exports.fn = function(item) {
if (
item.isElem(viewBoxElems) &&
item.hasAttr('viewBox') &&
item.hasAttr('width') &&
item.hasAttr('height')
) {
var nums = item.attr('viewBox').value.split(/[ ,]+/g);
if (
nums[0] === '0' &&
nums[1] === '0' &&
item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
item.attr('height').value.replace(/px$/, '') === nums[3]
) {
item.removeAttr('viewBox');
}
exports.fn = function (item) {
if (
item.isElem(viewBoxElems) &&
item.hasAttr('viewBox') &&
item.hasAttr('width') &&
item.hasAttr('height')
) {
// TODO remove width/height for such case instead
if (item.isElem('svg') && item.closestElem('svg')) {
return;
}
var nums = item.attr('viewBox').value.split(/[ ,]+/g);
if (
nums[0] === '0' &&
nums[1] === '0' &&
item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
item.attr('height').value.replace(/px$/, '') === nums[3]
) {
item.removeAttr('viewBox');
}
}
};