Skip to content

Commit

Permalink
Replace closestByName utility with visitSkip (svg#1613)
Browse files Browse the repository at this point in the history
The last usage of closestByName utility based on node.parentNode
is removed here. One step closer to clean ast in v3.
  • Loading branch information
TrySound authored Dec 7, 2021
1 parent 656bb09 commit 79d4bab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
16 changes: 0 additions & 16 deletions lib/xast.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ const matches = (node, selector) => {
};
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();
exports.visitSkip = visitSkip;

Expand Down
42 changes: 26 additions & 16 deletions plugins/removeHiddenElems.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const {
visit,
visitSkip,
querySelector,
closestByName,
detachNodeFromParent,
} = require('../lib/xast.js');
const { collectStylesheet, computeStyle } = require('../lib/style.js');
Expand Down Expand Up @@ -67,6 +68,30 @@ exports.fn = (root, params) => {
} = params;
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 {
element: {
enter: (node, parentNode) => {
Expand Down Expand Up @@ -102,21 +127,6 @@ exports.fn = (root, params) => {
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
//
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
Expand Down

0 comments on commit 79d4bab

Please sign in to comment.