removeHiddenElems.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = true;
  4. exports.description = 'removes hidden elements (zero sized, with absent attributes)';
  5. exports.params = {
  6. displayNone: true,
  7. opacity0: true,
  8. circleR0: true,
  9. ellipseRX0: true,
  10. ellipseRY0: true,
  11. rectWidth0: true,
  12. rectHeight0: true,
  13. patternWidth0: true,
  14. patternHeight0: true,
  15. imageWidth0: true,
  16. imageHeight0: true,
  17. pathEmptyD: true,
  18. polylineEmptyPoints: true,
  19. polygonEmptyPoints: true
  20. };
  21. var regValidPath = /M\s*(?:[-+]?(?:\d*\.\d+|\d+(?:\.|(?!\.)))([eE][-+]?\d+)?(?!\d)\s*,?\s*){2}\D*\d/i;
  22. /**
  23. * Remove hidden elements with disabled rendering:
  24. * - display="none"
  25. * - opacity="0"
  26. * - circle with zero radius
  27. * - ellipse with zero x-axis or y-axis radius
  28. * - rectangle with zero width or height
  29. * - pattern with zero width or height
  30. * - image with zero width or height
  31. * - path with empty data
  32. * - polyline with empty points
  33. * - polygon with empty points
  34. *
  35. * @param {Object} item current iteration item
  36. * @param {Object} params plugin params
  37. * @return {Boolean} if false, item will be filtered out
  38. *
  39. * @author Kir Belevich
  40. */
  41. exports.fn = function(item, params) {
  42. if (item.elem) {
  43. // display="none"
  44. //
  45. // http://www.w3.org/TR/SVG/painting.html#DisplayProperty
  46. // "A value of display: none indicates that the given element
  47. // and its children shall not be rendered directly"
  48. if (
  49. params.displayNone &&
  50. item.hasAttr('display', 'none')
  51. ) return false;
  52. // opacity="0"
  53. //
  54. // http://www.w3.org/TR/SVG/masking.html#ObjectAndGroupOpacityProperties
  55. if (
  56. params.opacity0 &&
  57. item.hasAttr('opacity', '0')
  58. ) return false;
  59. // Circles with zero radius
  60. //
  61. // http://www.w3.org/TR/SVG/shapes.html#CircleElementRAttribute
  62. // "A value of zero disables rendering of the element"
  63. //
  64. // <circle r="0">
  65. if (
  66. params.circleR0 &&
  67. item.isElem('circle') &&
  68. item.isEmpty() &&
  69. item.hasAttr('r', '0')
  70. ) return false;
  71. // Ellipse with zero x-axis radius
  72. //
  73. // http://www.w3.org/TR/SVG/shapes.html#EllipseElementRXAttribute
  74. // "A value of zero disables rendering of the element"
  75. //
  76. // <ellipse rx="0">
  77. if (
  78. params.ellipseRX0 &&
  79. item.isElem('ellipse') &&
  80. item.isEmpty() &&
  81. item.hasAttr('rx', '0')
  82. ) return false;
  83. // Ellipse with zero y-axis radius
  84. //
  85. // http://www.w3.org/TR/SVG/shapes.html#EllipseElementRYAttribute
  86. // "A value of zero disables rendering of the element"
  87. //
  88. // <ellipse ry="0">
  89. if (
  90. params.ellipseRY0 &&
  91. item.isElem('ellipse') &&
  92. item.isEmpty() &&
  93. item.hasAttr('ry', '0')
  94. ) return false;
  95. // Rectangle with zero width
  96. //
  97. // http://www.w3.org/TR/SVG/shapes.html#RectElementWidthAttribute
  98. // "A value of zero disables rendering of the element"
  99. //
  100. // <rect width="0">
  101. if (
  102. params.rectWidth0 &&
  103. item.isElem('rect') &&
  104. item.isEmpty() &&
  105. item.hasAttr('width', '0')
  106. ) return false;
  107. // Rectangle with zero height
  108. //
  109. // http://www.w3.org/TR/SVG/shapes.html#RectElementHeightAttribute
  110. // "A value of zero disables rendering of the element"
  111. //
  112. // <rect height="0">
  113. if (
  114. params.rectHeight0 &&
  115. params.rectWidth0 &&
  116. item.isElem('rect') &&
  117. item.isEmpty() &&
  118. item.hasAttr('height', '0')
  119. ) return false;
  120. // Pattern with zero width
  121. //
  122. // http://www.w3.org/TR/SVG/pservers.html#PatternElementWidthAttribute
  123. // "A value of zero disables rendering of the element (i.e., no paint is applied)"
  124. //
  125. // <pattern width="0">
  126. if (
  127. params.patternWidth0 &&
  128. item.isElem('pattern') &&
  129. item.hasAttr('width', '0')
  130. ) return false;
  131. // Pattern with zero height
  132. //
  133. // http://www.w3.org/TR/SVG/pservers.html#PatternElementHeightAttribute
  134. // "A value of zero disables rendering of the element (i.e., no paint is applied)"
  135. //
  136. // <pattern height="0">
  137. if (
  138. params.patternHeight0 &&
  139. item.isElem('pattern') &&
  140. item.hasAttr('height', '0')
  141. ) return false;
  142. // Image with zero width
  143. //
  144. // http://www.w3.org/TR/SVG/struct.html#ImageElementWidthAttribute
  145. // "A value of zero disables rendering of the element"
  146. //
  147. // <image width="0">
  148. if (
  149. params.imageWidth0 &&
  150. item.isElem('image') &&
  151. item.hasAttr('width', '0')
  152. ) return false;
  153. // Image with zero height
  154. //
  155. // http://www.w3.org/TR/SVG/struct.html#ImageElementHeightAttribute
  156. // "A value of zero disables rendering of the element"
  157. //
  158. // <image height="0">
  159. if (
  160. params.imageHeight0 &&
  161. item.isElem('image') &&
  162. item.hasAttr('height', '0')
  163. ) return false;
  164. // Path with empty data
  165. //
  166. // http://www.w3.org/TR/SVG/paths.html#DAttribute
  167. //
  168. // <path d=""/>
  169. if (
  170. params.pathEmptyD &&
  171. item.isElem('path') &&
  172. (!item.hasAttr('d') || !regValidPath.test(item.attr('d').value))
  173. ) return false;
  174. // Polyline with empty points
  175. //
  176. // http://www.w3.org/TR/SVG/shapes.html#PolylineElementPointsAttribute
  177. //
  178. // <polyline points="">
  179. if (
  180. params.polylineEmptyPoints &&
  181. item.isElem('polyline') &&
  182. !item.hasAttr('points')
  183. ) return false;
  184. // Polygon with empty points
  185. //
  186. // http://www.w3.org/TR/SVG/shapes.html#PolygonElementPointsAttribute
  187. //
  188. // <polygon points="">
  189. if (
  190. params.polygonEmptyPoints &&
  191. item.isElem('polygon') &&
  192. !item.hasAttr('points')
  193. ) return false;
  194. }
  195. };