| 1 | 'use strict';
|
|---|
| 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 | }
|
|---|
| 70 |
|
|---|
| 71 | if (xmlnsCollection.length) {
|
|---|
| 72 |
|
|---|
| 73 | // check item for the ns-attrs
|
|---|
| 74 | if (item.prefix) {
|
|---|
| 75 | removeNSfromCollection(item.prefix);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | // check each attr for the ns-attrs
|
|---|
| 79 | item.eachAttr(function(attr) {
|
|---|
| 80 | removeNSfromCollection(attr.prefix);
|
|---|
| 81 | });
|
|---|
| 82 |
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // if nothing is found - go deeper
|
|---|
| 86 | if (xmlnsCollection.length && item.content) {
|
|---|
| 87 | monkeys(item);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | i++;
|
|---|
| 91 |
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return items;
|
|---|
| 95 |
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | data = monkeys(data);
|
|---|
| 99 |
|
|---|
| 100 | // remove svg element ns-attributes if they are not used even once
|
|---|
| 101 | if (xmlnsCollection.length) {
|
|---|
| 102 | xmlnsCollection.forEach(function(name) {
|
|---|
| 103 | svgElem.removeAttr('xmlns:' + name);
|
|---|
| 104 | });
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return data;
|
|---|
| 108 |
|
|---|
| 109 | };
|
|---|