mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Replace closestByName utility with visitSkip (#1613)
The last usage of closestByName utility based on node.parentNode is removed here. One step closer to clean ast in v3.
This commit is contained in:
16
lib/xast.js
16
lib/xast.js
@ -39,22 +39,6 @@ const matches = (node, selector) => {
|
|||||||
};
|
};
|
||||||
exports.matches = matches;
|
exports.matches = matches;
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {(node: XastChild, name: string) => null | XastChild}
|
|
||||||
*/
|
|
||||||
const closestByName = (node, name) => {
|
|
||||||
let currentNode = node;
|
|
||||||
while (currentNode) {
|
|
||||||
if (currentNode.type === 'element' && currentNode.name === name) {
|
|
||||||
return currentNode;
|
|
||||||
}
|
|
||||||
// @ts-ignore parentNode is hidden from public usage
|
|
||||||
currentNode = currentNode.parentNode;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
exports.closestByName = closestByName;
|
|
||||||
|
|
||||||
const visitSkip = Symbol();
|
const visitSkip = Symbol();
|
||||||
exports.visitSkip = visitSkip;
|
exports.visitSkip = visitSkip;
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
visit,
|
||||||
|
visitSkip,
|
||||||
querySelector,
|
querySelector,
|
||||||
closestByName,
|
|
||||||
detachNodeFromParent,
|
detachNodeFromParent,
|
||||||
} = require('../lib/xast.js');
|
} = require('../lib/xast.js');
|
||||||
const { collectStylesheet, computeStyle } = require('../lib/style.js');
|
const { collectStylesheet, computeStyle } = require('../lib/style.js');
|
||||||
@ -67,6 +68,30 @@ exports.fn = (root, params) => {
|
|||||||
} = params;
|
} = params;
|
||||||
const stylesheet = collectStylesheet(root);
|
const stylesheet = collectStylesheet(root);
|
||||||
|
|
||||||
|
visit(root, {
|
||||||
|
element: {
|
||||||
|
enter: (node, parentNode) => {
|
||||||
|
// transparent element inside clipPath still affect clipped elements
|
||||||
|
if (node.name === 'clipPath') {
|
||||||
|
return visitSkip;
|
||||||
|
}
|
||||||
|
const computedStyle = computeStyle(stylesheet, node);
|
||||||
|
// opacity="0"
|
||||||
|
//
|
||||||
|
// https://www.w3.org/TR/SVG11/masking.html#ObjectAndGroupOpacityProperties
|
||||||
|
if (
|
||||||
|
opacity0 &&
|
||||||
|
computedStyle.opacity &&
|
||||||
|
computedStyle.opacity.type === 'static' &&
|
||||||
|
computedStyle.opacity.value === '0'
|
||||||
|
) {
|
||||||
|
detachNodeFromParent(node, parentNode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
element: {
|
element: {
|
||||||
enter: (node, parentNode) => {
|
enter: (node, parentNode) => {
|
||||||
@ -102,21 +127,6 @@ exports.fn = (root, params) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// opacity="0"
|
|
||||||
//
|
|
||||||
// https://www.w3.org/TR/SVG11/masking.html#ObjectAndGroupOpacityProperties
|
|
||||||
if (
|
|
||||||
opacity0 &&
|
|
||||||
computedStyle.opacity &&
|
|
||||||
computedStyle.opacity.type === 'static' &&
|
|
||||||
computedStyle.opacity.value === '0' &&
|
|
||||||
// transparent element inside clipPath still affect clipped elements
|
|
||||||
closestByName(node, 'clipPath') == null
|
|
||||||
) {
|
|
||||||
detachNodeFromParent(node, parentNode);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Circles with zero radius
|
// Circles with zero radius
|
||||||
//
|
//
|
||||||
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
|
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
|
||||||
|
Reference in New Issue
Block a user