collapseGroups.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. exports.type = 'perItemReverse';
  3. exports.active = true;
  4. exports.description = 'collapses useless groups';
  5. var collections = require('./_collections'),
  6. attrsInheritable = collections.inheritableAttrs,
  7. animationElems = collections.elemsGroups.animation;
  8. function hasAnimatedAttr(item) {
  9. /* jshint validthis:true */
  10. return item.isElem(animationElems) && item.hasAttr('attributeName', this) ||
  11. !item.isEmpty() && item.content.some(hasAnimatedAttr, this);
  12. }
  13. /*
  14. * Collapse useless groups.
  15. *
  16. * @example
  17. * <g>
  18. * <g attr1="val1">
  19. * <path d="..."/>
  20. * </g>
  21. * </g>
  22. * ⬇
  23. * <g>
  24. * <g>
  25. * <path attr1="val1" d="..."/>
  26. * </g>
  27. * </g>
  28. * ⬇
  29. * <path attr1="val1" d="..."/>
  30. *
  31. * @param {Object} item current iteration item
  32. * @return {Boolean} if false, item will be filtered out
  33. *
  34. * @author Kir Belevich
  35. */
  36. exports.fn = function(item) {
  37. // non-empty elements
  38. if (item.isElem() && !item.isElem('switch') && !item.isEmpty()) {
  39. item.content.forEach(function(g, i) {
  40. // non-empty groups
  41. if (g.isElem('g') && !g.isEmpty()) {
  42. // move group attibutes to the single content element
  43. if (g.hasAttr() && g.content.length === 1) {
  44. var inner = g.content[0];
  45. if (inner.isElem() && !inner.hasAttr('id') && !g.hasAttr('filter') &&
  46. !(g.hasAttr('class') && inner.hasAttr('class')) && (
  47. !g.hasAttr('clip-path') && !g.hasAttr('mask') ||
  48. inner.isElem('g') && !g.hasAttr('transform') && !inner.hasAttr('transform')
  49. )
  50. ) {
  51. g.eachAttr(function(attr) {
  52. if (g.content.some(hasAnimatedAttr, attr.name)) return;
  53. if (!inner.hasAttr(attr.name)) {
  54. inner.addAttr(attr);
  55. } else if (attr.name == 'transform') {
  56. inner.attr(attr.name).value = attr.value + ' ' + inner.attr(attr.name).value;
  57. } else if (inner.hasAttr(attr.name, 'inherit')) {
  58. inner.attr(attr.name).value = attr.value;
  59. } else if (
  60. attrsInheritable.indexOf(attr.name) < 0 &&
  61. !inner.hasAttr(attr.name, attr.value)
  62. ) {
  63. return;
  64. }
  65. g.removeAttr(attr.name);
  66. });
  67. }
  68. }
  69. // collapse groups without attributes
  70. if (!g.hasAttr() && !g.content.some(function(item) { return item.isElem(animationElems) })) {
  71. item.spliceContent(i, 1, g.content);
  72. }
  73. }
  74. });
  75. }
  76. };