Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const { detachNodeFromParent } = require('../lib/xast.js');
|
---|
| 4 | const { elemsGroups } = require('./_collections.js');
|
---|
| 5 |
|
---|
| 6 | exports.type = 'visitor';
|
---|
| 7 | exports.name = 'removeEmptyContainers';
|
---|
| 8 | exports.active = true;
|
---|
| 9 | exports.description = 'removes empty container elements';
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Remove empty containers.
|
---|
| 13 | *
|
---|
| 14 | * @see https://www.w3.org/TR/SVG11/intro.html#TermContainerElement
|
---|
| 15 | *
|
---|
| 16 | * @example
|
---|
| 17 | * <defs/>
|
---|
| 18 | *
|
---|
| 19 | * @example
|
---|
| 20 | * <g><marker><a/></marker></g>
|
---|
| 21 | *
|
---|
| 22 | * @author Kir Belevich
|
---|
| 23 | *
|
---|
| 24 | * @type {import('../lib/types').Plugin<void>}
|
---|
| 25 | */
|
---|
| 26 | exports.fn = () => {
|
---|
| 27 | return {
|
---|
| 28 | element: {
|
---|
| 29 | exit: (node, parentNode) => {
|
---|
| 30 | // remove only empty non-svg containers
|
---|
| 31 | if (
|
---|
| 32 | node.name === 'svg' ||
|
---|
| 33 | elemsGroups.container.includes(node.name) === false ||
|
---|
| 34 | node.children.length !== 0
|
---|
| 35 | ) {
|
---|
| 36 | return;
|
---|
| 37 | }
|
---|
| 38 | // empty patterns may contain reusable configuration
|
---|
| 39 | if (
|
---|
| 40 | node.name === 'pattern' &&
|
---|
| 41 | Object.keys(node.attributes).length !== 0
|
---|
| 42 | ) {
|
---|
| 43 | return;
|
---|
| 44 | }
|
---|
| 45 | // The <g> may not have content, but the filter may cause a rectangle
|
---|
| 46 | // to be created and filled with pattern.
|
---|
| 47 | if (node.name === 'g' && node.attributes.filter != null) {
|
---|
| 48 | return;
|
---|
| 49 | }
|
---|
| 50 | // empty <mask> hides masked element
|
---|
| 51 | if (node.name === 'mask' && node.attributes.id != null) {
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
| 54 | detachNodeFromParent(node, parentNode);
|
---|
| 55 | },
|
---|
| 56 | },
|
---|
| 57 | };
|
---|
| 58 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.