aboutsummaryrefslogtreecommitdiff
path: root/node_modules/svgo/plugins/removeUnusedNS.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/svgo/plugins/removeUnusedNS.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/svgo/plugins/removeUnusedNS.js')
-rw-r--r--node_modules/svgo/plugins/removeUnusedNS.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/node_modules/svgo/plugins/removeUnusedNS.js b/node_modules/svgo/plugins/removeUnusedNS.js
new file mode 100644
index 00000000..c4787915
--- /dev/null
+++ b/node_modules/svgo/plugins/removeUnusedNS.js
@@ -0,0 +1,107 @@
+'use strict';
+
+exports.type = 'full';
+
+exports.active = true;
+
+exports.description = 'removes unused namespaces declaration';
+
+/**
+ * Remove unused namespaces declaration.
+ *
+ * @param {Object} item current iteration item
+ * @return {Boolean} if false, item will be filtered out
+ *
+ * @author Kir Belevich
+ */
+exports.fn = function(data) {
+
+ 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;
+
+};