项目原始demo,不改动
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
Questo repository è archiviato. Puoi vedere i file e clonarli, ma non puoi effettuare richieste di pushj o aprire problemi/richieste di pull.
 
 
 
 

108 righe
2.3 KiB

  1. 'use strict';
  2. exports.type = 'full';
  3. exports.active = true;
  4. exports.description = 'removes unused namespaces declaration';
  5. /**
  6. * Remove unused namespaces declaration.
  7. *
  8. * @param {Object} item current iteration item
  9. * @return {Boolean} if false, item will be filtered out
  10. *
  11. * @author Kir Belevich
  12. */
  13. exports.fn = function(data) {
  14. var svgElem,
  15. xmlnsCollection = [];
  16. /**
  17. * Remove namespace from collection.
  18. *
  19. * @param {String} ns namescape name
  20. */
  21. function removeNSfromCollection(ns) {
  22. var pos = xmlnsCollection.indexOf(ns);
  23. // if found - remove ns from the namespaces collection
  24. if (pos > -1) {
  25. xmlnsCollection.splice(pos, 1);
  26. }
  27. }
  28. /**
  29. * Bananas!
  30. *
  31. * @param {Array} items input items
  32. *
  33. * @return {Array} output items
  34. */
  35. function monkeys(items) {
  36. var i = 0,
  37. length = items.content.length;
  38. while(i < length) {
  39. var item = items.content[i];
  40. if (item.isElem('svg')) {
  41. item.eachAttr(function(attr) {
  42. // collect namespaces
  43. if (attr.prefix === 'xmlns' && attr.local) {
  44. xmlnsCollection.push(attr.local);
  45. }
  46. });
  47. // if svg element has ns-attr
  48. if (xmlnsCollection.length) {
  49. // save svg element
  50. svgElem = item;
  51. }
  52. } else if (xmlnsCollection.length) {
  53. // check item for the ns-attrs
  54. if (item.prefix) {
  55. removeNSfromCollection(item.prefix);
  56. }
  57. // check each attr for the ns-attrs
  58. item.eachAttr(function(attr) {
  59. removeNSfromCollection(attr.prefix);
  60. });
  61. }
  62. // if nothing is found - go deeper
  63. if (xmlnsCollection.length && item.content) {
  64. monkeys(item);
  65. }
  66. i++;
  67. }
  68. return items;
  69. }
  70. data = monkeys(data);
  71. // remove svg element ns-attributes if they are not used even once
  72. if (xmlnsCollection.length) {
  73. xmlnsCollection.forEach(function(name) {
  74. svgElem.removeAttr('xmlns:' + name);
  75. });
  76. }
  77. return data;
  78. };