mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Replace removeAttr with delete operator (#1432)
delete operator is more explicit and not much harder to use.
This commit is contained in:
@ -6,7 +6,7 @@ exports.active = true;
|
||||
|
||||
exports.description = 'removes viewBox attribute when possible';
|
||||
|
||||
var viewBoxElems = ['svg', 'pattern', 'symbol'];
|
||||
const viewBoxElems = ['svg', 'pattern', 'symbol'];
|
||||
|
||||
/**
|
||||
* Remove viewBox attr which coincides with a width/height box.
|
||||
@ -25,25 +25,26 @@ var viewBoxElems = ['svg', 'pattern', 'symbol'];
|
||||
*/
|
||||
exports.fn = function (item) {
|
||||
if (
|
||||
item.isElem(viewBoxElems) &&
|
||||
item.hasAttr('viewBox') &&
|
||||
item.hasAttr('width') &&
|
||||
item.hasAttr('height')
|
||||
item.type === 'element' &&
|
||||
viewBoxElems.includes(item.name) &&
|
||||
item.attributes.viewBox != null &&
|
||||
item.attributes.width != null &&
|
||||
item.attributes.height != null
|
||||
) {
|
||||
// TODO remove width/height for such case instead
|
||||
if (item.isElem('svg') && item.closestElem('svg')) {
|
||||
if (item.name === 'svg' && item.closestElem('svg')) {
|
||||
return;
|
||||
}
|
||||
|
||||
var nums = item.attr('viewBox').value.split(/[ ,]+/g);
|
||||
const nums = item.attributes.viewBox.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.attributes.width.replace(/px$/, '') === nums[2] && // could use parseFloat too
|
||||
item.attributes.height.replace(/px$/, '') === nums[3]
|
||||
) {
|
||||
item.removeAttr('viewBox');
|
||||
delete item.attributes.viewBox;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user