From 3204a06ca568e4812fbfb0863ab72d5a7fd53ce5 Mon Sep 17 00:00:00 2001 From: deepsweet Date: Mon, 15 Oct 2012 19:29:26 +0300 Subject: [PATCH] new plugin removeUnusedNS --- config.json | 5 +- plugins/removeUnusedNS.js | 100 ++++++++++++++++++++++ test/plugins/removeUnusedNS.01.orig.svg | 5 ++ test/plugins/removeUnusedNS.01.should.svg | 5 ++ test/plugins/removeUnusedNS.02.orig.svg | 5 ++ test/plugins/removeUnusedNS.02.should.svg | 5 ++ test/plugins/removeUnusedNS.03.orig.svg | 7 ++ test/plugins/removeUnusedNS.03.should.svg | 7 ++ test/plugins/removeUnusedNS.04.orig.svg | 7 ++ test/plugins/removeUnusedNS.04.should.svg | 7 ++ test/plugins/removeUnusedNS.05.orig.svg | 7 ++ test/plugins/removeUnusedNS.05.should.svg | 7 ++ test/plugins/removeUnusedNS.06.orig.svg | 7 ++ test/plugins/removeUnusedNS.06.should.svg | 7 ++ 14 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 plugins/removeUnusedNS.js create mode 100644 test/plugins/removeUnusedNS.01.orig.svg create mode 100644 test/plugins/removeUnusedNS.01.should.svg create mode 100644 test/plugins/removeUnusedNS.02.orig.svg create mode 100644 test/plugins/removeUnusedNS.02.should.svg create mode 100644 test/plugins/removeUnusedNS.03.orig.svg create mode 100644 test/plugins/removeUnusedNS.03.should.svg create mode 100644 test/plugins/removeUnusedNS.04.orig.svg create mode 100644 test/plugins/removeUnusedNS.04.should.svg create mode 100644 test/plugins/removeUnusedNS.05.orig.svg create mode 100644 test/plugins/removeUnusedNS.05.should.svg create mode 100644 test/plugins/removeUnusedNS.06.orig.svg create mode 100644 test/plugins/removeUnusedNS.06.should.svg diff --git a/config.json b/config.json index 3fcb2cf6..fb442cec 100644 --- a/config.json +++ b/config.json @@ -137,7 +137,10 @@ } ], "full": [ - + { + "name": "removeUnusedNS", + "active": true + } ] } } diff --git a/plugins/removeUnusedNS.js b/plugins/removeUnusedNS.js new file mode 100644 index 00000000..c5915898 --- /dev/null +++ b/plugins/removeUnusedNS.js @@ -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; + +}; diff --git a/test/plugins/removeUnusedNS.01.orig.svg b/test/plugins/removeUnusedNS.01.orig.svg new file mode 100644 index 00000000..f7b93967 --- /dev/null +++ b/test/plugins/removeUnusedNS.01.orig.svg @@ -0,0 +1,5 @@ + + + test + + diff --git a/test/plugins/removeUnusedNS.01.should.svg b/test/plugins/removeUnusedNS.01.should.svg new file mode 100644 index 00000000..39a3c864 --- /dev/null +++ b/test/plugins/removeUnusedNS.01.should.svg @@ -0,0 +1,5 @@ + + + test + + diff --git a/test/plugins/removeUnusedNS.02.orig.svg b/test/plugins/removeUnusedNS.02.orig.svg new file mode 100644 index 00000000..3664c553 --- /dev/null +++ b/test/plugins/removeUnusedNS.02.orig.svg @@ -0,0 +1,5 @@ + + + test + + diff --git a/test/plugins/removeUnusedNS.02.should.svg b/test/plugins/removeUnusedNS.02.should.svg new file mode 100644 index 00000000..3664c553 --- /dev/null +++ b/test/plugins/removeUnusedNS.02.should.svg @@ -0,0 +1,5 @@ + + + test + + diff --git a/test/plugins/removeUnusedNS.03.orig.svg b/test/plugins/removeUnusedNS.03.orig.svg new file mode 100644 index 00000000..dcd0fbd7 --- /dev/null +++ b/test/plugins/removeUnusedNS.03.orig.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.03.should.svg b/test/plugins/removeUnusedNS.03.should.svg new file mode 100644 index 00000000..d1685bb5 --- /dev/null +++ b/test/plugins/removeUnusedNS.03.should.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.04.orig.svg b/test/plugins/removeUnusedNS.04.orig.svg new file mode 100644 index 00000000..f1a17423 --- /dev/null +++ b/test/plugins/removeUnusedNS.04.orig.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.04.should.svg b/test/plugins/removeUnusedNS.04.should.svg new file mode 100644 index 00000000..f1a17423 --- /dev/null +++ b/test/plugins/removeUnusedNS.04.should.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.05.orig.svg b/test/plugins/removeUnusedNS.05.orig.svg new file mode 100644 index 00000000..4414bb07 --- /dev/null +++ b/test/plugins/removeUnusedNS.05.orig.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.05.should.svg b/test/plugins/removeUnusedNS.05.should.svg new file mode 100644 index 00000000..e7829225 --- /dev/null +++ b/test/plugins/removeUnusedNS.05.should.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.06.orig.svg b/test/plugins/removeUnusedNS.06.orig.svg new file mode 100644 index 00000000..12f397e2 --- /dev/null +++ b/test/plugins/removeUnusedNS.06.orig.svg @@ -0,0 +1,7 @@ + + + + test + + + diff --git a/test/plugins/removeUnusedNS.06.should.svg b/test/plugins/removeUnusedNS.06.should.svg new file mode 100644 index 00000000..12f397e2 --- /dev/null +++ b/test/plugins/removeUnusedNS.06.should.svg @@ -0,0 +1,7 @@ + + + + test + + +