diff --git a/plugins/removeEmptyContainers.js b/plugins/removeEmptyContainers.js index cb4ce0c0f..5eeeac2da 100644 --- a/plugins/removeEmptyContainers.js +++ b/plugins/removeEmptyContainers.js @@ -1,5 +1,6 @@ import { elemsGroups } from './_collections.js'; import { detachNodeFromParent } from '../lib/xast.js'; +import { collectStylesheet, computeStyle } from '../lib/style.js'; export const name = 'removeEmptyContainers'; export const description = 'removes empty container elements'; @@ -19,7 +20,9 @@ export const description = 'removes empty container elements'; * * @type {import('./plugins-types.js').Plugin<'removeEmptyContainers'>} */ -export const fn = () => { +export const fn = (root) => { + const stylesheet = collectStylesheet(root); + return { element: { exit: (node, parentNode) => { @@ -38,11 +41,7 @@ export const fn = () => { ) { return; } - // The may not have content, but the filter may cause a rectangle - // to be created and filled with pattern. - if (node.name === 'g' && node.attributes.filter != null) { - return; - } + // empty hides masked element if (node.name === 'mask' && node.attributes.id != null) { return; @@ -50,6 +49,17 @@ export const fn = () => { if (parentNode.type === 'element' && parentNode.name === 'switch') { return; } + + // The may not have content, but the filter may cause a rectangle + // to be created and filled with pattern. + if ( + node.name === 'g' && + (node.attributes.filter != null || + computeStyle(stylesheet, node).filter) + ) { + return; + } + detachNodeFromParent(node, parentNode); }, }, diff --git a/test/plugins/removeEmptyContainers.07.svg.txt b/test/plugins/removeEmptyContainers.07.svg.txt new file mode 100644 index 000000000..d3e6f876d --- /dev/null +++ b/test/plugins/removeEmptyContainers.07.svg.txt @@ -0,0 +1,27 @@ +Empty nodes should not be removed if they contain a filter, including +filters applied via CSS. + +=== + + + + + + + + + •ᴗ• + + + +@@@ + + + + + + + + + •ᴗ• +