12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 'use strict';
- exports.type = 'visitor';
- exports.name = 'removeUnusedNS';
- exports.active = true;
- exports.description = 'removes unused namespaces declaration';
- exports.fn = () => {
-
- const unusedNamespaces = new Set();
- return {
- element: {
- enter: (node, parentNode) => {
-
-
- if (node.name === 'svg' && parentNode.type === 'root') {
- for (const name of Object.keys(node.attributes)) {
- if (name.startsWith('xmlns:')) {
- const local = name.slice('xmlns:'.length);
- unusedNamespaces.add(local);
- }
- }
- }
- if (unusedNamespaces.size !== 0) {
-
- if (node.name.includes(':')) {
- const [ns] = node.name.split(':');
- if (unusedNamespaces.has(ns)) {
- unusedNamespaces.delete(ns);
- }
- }
-
- for (const name of Object.keys(node.attributes)) {
- if (name.includes(':')) {
- const [ns] = name.split(':');
- unusedNamespaces.delete(ns);
- }
- }
- }
- },
- exit: (node, parentNode) => {
-
- if (node.name === 'svg' && parentNode.type === 'root') {
- for (const name of unusedNamespaces) {
- delete node.attributes[`xmlns:${name}`];
- }
- }
- },
- },
- };
- };
|