UNPKG

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