1 | ;
|
2 |
|
3 | exports.type = 'full';
|
4 |
|
5 | exports.active = true;
|
6 |
|
7 | exports.description = 'removes unused namespaces declaration';
|
8 |
|
9 | /**
|
10 | * Remove unused namespaces declaration.
|
11 | *
|
12 | * @param {Object} item current iteration item
|
13 | * @return {Boolean} if false, item will be filtered out
|
14 | *
|
15 | * @author Kir Belevich
|
16 | */
|
17 | exports.fn = function(data) {
|
18 |
|
19 | var svgElem,
|
20 | xmlnsCollection = [];
|
21 |
|
22 | /**
|
23 | * Remove namespace from collection.
|
24 | *
|
25 | * @param {String} ns namescape name
|
26 | */
|
27 | function removeNSfromCollection(ns) {
|
28 |
|
29 | var pos = xmlnsCollection.indexOf(ns);
|
30 |
|
31 | // if found - remove ns from the namespaces collection
|
32 | if (pos > -1) {
|
33 | xmlnsCollection.splice(pos, 1);
|
34 | }
|
35 |
|
36 | }
|
37 |
|
38 | /**
|
39 | * Bananas!
|
40 | *
|
41 | * @param {Array} items input items
|
42 | *
|
43 | * @return {Array} output items
|
44 | */
|
45 | function monkeys(items) {
|
46 |
|
47 | var i = 0,
|
48 | length = items.content.length;
|
49 |
|
50 | while(i < length) {
|
51 |
|
52 | var item = items.content[i];
|
53 |
|
54 | if (item.isElem('svg')) {
|
55 |
|
56 | item.eachAttr(function(attr) {
|
57 | // collect namespaces
|
58 | if (attr.prefix === 'xmlns' && attr.local) {
|
59 | xmlnsCollection.push(attr.local);
|
60 | }
|
61 | });
|
62 |
|
63 | // if svg element has ns-attr
|
64 | if (xmlnsCollection.length) {
|
65 | // save svg element
|
66 | svgElem = item;
|
67 | }
|
68 |
|
69 | } else if (xmlnsCollection.length) {
|
70 |
|
71 | // check item for the ns-attrs
|
72 | if (item.prefix) {
|
73 | removeNSfromCollection(item.prefix);
|
74 | }
|
75 |
|
76 | // check each attr for the ns-attrs
|
77 | item.eachAttr(function(attr) {
|
78 | removeNSfromCollection(attr.prefix);
|
79 | });
|
80 |
|
81 | }
|
82 |
|
83 | // if nothing is found - go deeper
|
84 | if (xmlnsCollection.length && item.content) {
|
85 | monkeys(item);
|
86 | }
|
87 |
|
88 | i++;
|
89 |
|
90 | }
|
91 |
|
92 | return items;
|
93 |
|
94 | }
|
95 |
|
96 | data = monkeys(data);
|
97 |
|
98 | // remove svg element ns-attributes if they are not used even once
|
99 | if (xmlnsCollection.length) {
|
100 | xmlnsCollection.forEach(function(name) {
|
101 | svgElem.removeAttr('xmlns:' + name);
|
102 | });
|
103 | }
|
104 |
|
105 | return data;
|
106 |
|
107 | };
|