Skip to content

Commit 79d4bab

Browse files
authored
Replace closestByName utility with visitSkip (svg#1613)
The last usage of closestByName utility based on node.parentNode is removed here. One step closer to clean ast in v3.
1 parent 656bb09 commit 79d4bab

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

lib/xast.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,6 @@ const matches = (node, selector) => {
3939
};
4040
exports.matches = matches;
4141

42-
/**
43-
* @type {(node: XastChild, name: string) => null | XastChild}
44-
*/
45-
const closestByName = (node, name) => {
46-
let currentNode = node;
47-
while (currentNode) {
48-
if (currentNode.type === 'element' && currentNode.name === name) {
49-
return currentNode;
50-
}
51-
// @ts-ignore parentNode is hidden from public usage
52-
currentNode = currentNode.parentNode;
53-
}
54-
return null;
55-
};
56-
exports.closestByName = closestByName;
57-
5842
const visitSkip = Symbol();
5943
exports.visitSkip = visitSkip;
6044

plugins/removeHiddenElems.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
const {
4+
visit,
5+
visitSkip,
46
querySelector,
5-
closestByName,
67
detachNodeFromParent,
78
} = require('../lib/xast.js');
89
const { collectStylesheet, computeStyle } = require('../lib/style.js');
@@ -67,6 +68,30 @@ exports.fn = (root, params) => {
6768
} = params;
6869
const stylesheet = collectStylesheet(root);
6970

71+
visit(root, {
72+
element: {
73+
enter: (node, parentNode) => {
74+
// transparent element inside clipPath still affect clipped elements
75+
if (node.name === 'clipPath') {
76+
return visitSkip;
77+
}
78+
const computedStyle = computeStyle(stylesheet, node);
79+
// opacity="0"
80+
//
81+
// https://www.w3.org/TR/SVG11/masking.html#ObjectAndGroupOpacityProperties
82+
if (
83+
opacity0 &&
84+
computedStyle.opacity &&
85+
computedStyle.opacity.type === 'static' &&
86+
computedStyle.opacity.value === '0'
87+
) {
88+
detachNodeFromParent(node, parentNode);
89+
return;
90+
}
91+
},
92+
},
93+
});
94+
7095
return {
7196
element: {
7297
enter: (node, parentNode) => {
@@ -102,21 +127,6 @@ exports.fn = (root, params) => {
102127
return;
103128
}
104129

105-
// opacity="0"
106-
//
107-
// https://www.w3.org/TR/SVG11/masking.html#ObjectAndGroupOpacityProperties
108-
if (
109-
opacity0 &&
110-
computedStyle.opacity &&
111-
computedStyle.opacity.type === 'static' &&
112-
computedStyle.opacity.value === '0' &&
113-
// transparent element inside clipPath still affect clipped elements
114-
closestByName(node, 'clipPath') == null
115-
) {
116-
detachNodeFromParent(node, parentNode);
117-
return;
118-
}
119-
120130
// Circles with zero radius
121131
//
122132
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute

0 commit comments

Comments
 (0)