1 | 'use strict';
|
---|
2 |
|
---|
3 | const { detachNodeFromParent } = require('../lib/xast.js');
|
---|
4 |
|
---|
5 | exports.name = 'removeEmptyText';
|
---|
6 | exports.type = 'visitor';
|
---|
7 | exports.active = true;
|
---|
8 | exports.description = 'removes empty <text> elements';
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Remove empty Text elements.
|
---|
12 | *
|
---|
13 | * @see https://www.w3.org/TR/SVG11/text.html
|
---|
14 | *
|
---|
15 | * @example
|
---|
16 | * Remove empty text element:
|
---|
17 | * <text/>
|
---|
18 | *
|
---|
19 | * Remove empty tspan element:
|
---|
20 | * <tspan/>
|
---|
21 | *
|
---|
22 | * Remove tref with empty xlink:href attribute:
|
---|
23 | * <tref xlink:href=""/>
|
---|
24 | *
|
---|
25 | * @author Kir Belevich
|
---|
26 | *
|
---|
27 | * @type {import('../lib/types').Plugin<{
|
---|
28 | * text?: boolean,
|
---|
29 | * tspan?: boolean,
|
---|
30 | * tref?: boolean
|
---|
31 | * }>}
|
---|
32 | */
|
---|
33 | exports.fn = (root, params) => {
|
---|
34 | const { text = true, tspan = true, tref = true } = params;
|
---|
35 | return {
|
---|
36 | element: {
|
---|
37 | enter: (node, parentNode) => {
|
---|
38 | // Remove empty text element
|
---|
39 | if (text && node.name === 'text' && node.children.length === 0) {
|
---|
40 | detachNodeFromParent(node, parentNode);
|
---|
41 | }
|
---|
42 | // Remove empty tspan element
|
---|
43 | if (tspan && node.name === 'tspan' && node.children.length === 0) {
|
---|
44 | detachNodeFromParent(node, parentNode);
|
---|
45 | }
|
---|
46 | // Remove tref with empty xlink:href attribute
|
---|
47 | if (
|
---|
48 | tref &&
|
---|
49 | node.name === 'tref' &&
|
---|
50 | node.attributes['xlink:href'] == null
|
---|
51 | ) {
|
---|
52 | detachNodeFromParent(node, parentNode);
|
---|
53 | }
|
---|
54 | },
|
---|
55 | },
|
---|
56 | };
|
---|
57 | };
|
---|