UNPKG

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