source: trip-planner-front/node_modules/svgo/plugins/removeUnusedNS.js@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[6a3a178]1'use strict';
2
3exports.type = 'visitor';
4exports.name = 'removeUnusedNS';
5exports.active = true;
6exports.description = 'removes unused namespaces declaration';
7
8/**
9 * Remove unused namespaces declaration from svg element
10 * which are not used in elements or attributes
11 *
12 * @author Kir Belevich
13 *
14 * @type {import('../lib/types').Plugin<void>}
15 */
16exports.fn = () => {
17 /**
18 * @type {Set<string>}
19 */
20 const unusedNamespaces = new Set();
21 return {
22 element: {
23 enter: (node, parentNode) => {
24 // collect all namespaces from svg element
25 // (such as xmlns:xlink="http://www.w3.org/1999/xlink")
26 if (node.name === 'svg' && parentNode.type === 'root') {
27 for (const name of Object.keys(node.attributes)) {
28 if (name.startsWith('xmlns:')) {
29 const local = name.slice('xmlns:'.length);
30 unusedNamespaces.add(local);
31 }
32 }
33 }
34 if (unusedNamespaces.size !== 0) {
35 // preserve namespace used in nested elements names
36 if (node.name.includes(':')) {
37 const [ns] = node.name.split(':');
38 if (unusedNamespaces.has(ns)) {
39 unusedNamespaces.delete(ns);
40 }
41 }
42 // preserve namespace used in nested elements attributes
43 for (const name of Object.keys(node.attributes)) {
44 if (name.includes(':')) {
45 const [ns] = name.split(':');
46 unusedNamespaces.delete(ns);
47 }
48 }
49 }
50 },
51 exit: (node, parentNode) => {
52 // remove unused namespace attributes from svg element
53 if (node.name === 'svg' && parentNode.type === 'root') {
54 for (const name of unusedNamespaces) {
55 delete node.attributes[`xmlns:${name}`];
56 }
57 }
58 },
59 },
60 };
61};
Note: See TracBrowser for help on using the repository browser.