removeUselessDefs.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = true;
  4. exports.description = 'removes elements in <defs> without id';
  5. var nonRendering = require('./_collections').elemsGroups.nonRendering;
  6. /**
  7. * Removes content of defs and properties that aren't rendered directly without ids.
  8. *
  9. * @param {Object} item current iteration item
  10. * @return {Boolean} if false, item will be filtered out
  11. *
  12. * @author Lev Solntsev
  13. */
  14. exports.fn = function(item) {
  15. if (item.isElem('defs')) {
  16. if (item.content) {
  17. item.content = getUsefulItems(item, []);
  18. }
  19. if (item.isEmpty()) return false;
  20. } else if (item.isElem(nonRendering) && !item.hasAttr('id')) {
  21. return false;
  22. }
  23. };
  24. function getUsefulItems(item, usefulItems) {
  25. item.content.forEach(function(child) {
  26. if (child.hasAttr('id') || child.isElem('style')) {
  27. usefulItems.push(child);
  28. child.parentNode = item;
  29. } else if (!child.isEmpty()) {
  30. child.content = getUsefulItems(child, usefulItems);
  31. }
  32. });
  33. return usefulItems;
  34. }