new plugin removeUnusedNS
@ -137,7 +137,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"full": [
|
"full": [
|
||||||
|
{
|
||||||
|
"name": "removeUnusedNS",
|
||||||
|
"active": true
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
100
plugins/removeUnusedNS.js
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
* Remove unused XLink namespace declaration.
|
||||||
|
*
|
||||||
|
* @param {Object} item current iteration item
|
||||||
|
* @param {Object} params plugin params
|
||||||
|
* @return {Boolean} if false, item will be filtered out
|
||||||
|
*
|
||||||
|
* @author Kir Belevich
|
||||||
|
*/
|
||||||
|
exports.removeUnusedNS = function(data, params) {
|
||||||
|
|
||||||
|
var svgElem,
|
||||||
|
xmlnsCollection = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove namespace from collection.
|
||||||
|
*
|
||||||
|
* @param {String} ns namescape name
|
||||||
|
*/
|
||||||
|
function removeNSfromCollection(ns) {
|
||||||
|
|
||||||
|
var pos = xmlnsCollection.indexOf(ns);
|
||||||
|
|
||||||
|
// if found - remove ns from the namespaces collection
|
||||||
|
if (pos > -1) {
|
||||||
|
xmlnsCollection.splice(pos, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bananas!
|
||||||
|
*
|
||||||
|
* @param {Array} items input items
|
||||||
|
*
|
||||||
|
* @return {Array} output items
|
||||||
|
*/
|
||||||
|
function monkeys(items) {
|
||||||
|
|
||||||
|
var i = 0,
|
||||||
|
length = items.content.length;
|
||||||
|
|
||||||
|
while(i < length) {
|
||||||
|
|
||||||
|
var item = items.content[i];
|
||||||
|
|
||||||
|
if (item.isElem('svg')) {
|
||||||
|
|
||||||
|
item.eachAttr(function(attr) {
|
||||||
|
// collect namespaces
|
||||||
|
if (attr.prefix === 'xmlns' && attr.local) {
|
||||||
|
xmlnsCollection.push(attr.local);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// if svg element has ns-attr
|
||||||
|
if (xmlnsCollection.length) {
|
||||||
|
// save svg element
|
||||||
|
svgElem = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (xmlnsCollection.length) {
|
||||||
|
|
||||||
|
// check item for the ns-attrs
|
||||||
|
if (item.prefix) {
|
||||||
|
removeNSfromCollection(item.prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check each attr for the ns-attrs
|
||||||
|
item.eachAttr(function(attr) {
|
||||||
|
removeNSfromCollection(attr.prefix);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// if nothing is found - go deeper
|
||||||
|
if (xmlnsCollection.length && item.content) {
|
||||||
|
monkeys(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
data = monkeys(data);
|
||||||
|
|
||||||
|
// remove svg element ns-attributes if they are not used even once
|
||||||
|
if (xmlnsCollection.length) {
|
||||||
|
xmlnsCollection.forEach(function(name) {
|
||||||
|
svgElem.removeAttr('xmlns:' + name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
|
||||||
|
};
|
5
test/plugins/removeUnusedNS.01.orig.svg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/">
|
||||||
|
<g>
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 127 B |
5
test/plugins/removeUnusedNS.01.should.svg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g>
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 78 B |
5
test/plugins/removeUnusedNS.02.orig.svg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 143 B |
5
test/plugins/removeUnusedNS.02.should.svg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 143 B |
7
test/plugins/removeUnusedNS.03.orig.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
<g>
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 222 B |
7
test/plugins/removeUnusedNS.03.should.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
<g>
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 172 B |
7
test/plugins/removeUnusedNS.04.orig.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
<g test2:attr="val">
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 239 B |
7
test/plugins/removeUnusedNS.04.should.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<g test:attr="val">
|
||||||
|
<g test2:attr="val">
|
||||||
|
test
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 239 B |
7
test/plugins/removeUnusedNS.05.orig.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<g>
|
||||||
|
<test:elem>
|
||||||
|
test
|
||||||
|
</test:elem>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 222 B |
7
test/plugins/removeUnusedNS.05.should.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/">
|
||||||
|
<g>
|
||||||
|
<test:elem>
|
||||||
|
test
|
||||||
|
</test:elem>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 172 B |
7
test/plugins/removeUnusedNS.06.orig.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<test:elem>
|
||||||
|
<test2:elem>
|
||||||
|
test
|
||||||
|
</test2:elem>
|
||||||
|
</test:elem>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 240 B |
7
test/plugins/removeUnusedNS.06.should.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://trololololololololololo.com/" xmlns:test2="http://trololololololololololo.com/">
|
||||||
|
<test:elem>
|
||||||
|
<test2:elem>
|
||||||
|
test
|
||||||
|
</test2:elem>
|
||||||
|
</test:elem>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 240 B |