removeUnusedNS.js 1.8 KB

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