Repository: svg/svgo
Pull request #696 (2017-04-02 16:02:24)

remove useless switch groups
I don't use arrow functions, because ugligyJS doesn't like them sadly


Patch:
diff --git a/plugins/collapseGroups.js b/plugins/collapseGroups.js
index adc9763..8d98403 100644
--- a/plugins/collapseGroups.js
+++ b/plugins/collapseGroups.js
@@ -42,7 +42,7 @@ function hasAnimatedAttr(item) {
 exports.fn = function(item) {
 
     // non-empty elements
-    if (item.isElem() && !item.isElem('switch') && !item.isEmpty()) {
+    if (item.isElem() && !item.isEmpty()) {
 
         item.content.forEach(function(g, i) {
 
@@ -82,6 +82,17 @@ exports.fn = function(item) {
                 if (!g.hasAttr() && !g.content.some(function(item) { return item.isElem(animationElems) })) {
                     item.spliceContent(i, 1, g.content);
                 }
+                
+
+            } else if (g.isElem('switch') && !g.isEmpty()) {
+                // check if one of the direct children has requiredFeatures, requiredExtensions or systemLanguage attributes else remove
+
+                if (!g.content || !g.content.some(function(child) {
+                    return child.hasAttr('systemLanguage') || child.hasAttr('requiredFeatures') || child.hasAttr('requiredExtensions');
+                    })
+                ) {
+                    item.spliceContent(i, 1, g.content);
+                }
             }
 
         });


Test patch:
diff --git a/test/plugins/_index.js b/test/plugins/_index.js
index 7ce3d52..e8b298f 100644
--- a/test/plugins/_index.js
+++ b/test/plugins/_index.js
@@ -24,10 +24,10 @@ describe('plugins tests', function() {
 
             file = PATH.resolve(__dirname, file);
 
-            it(name + '.' + index, function(done) {
-
-                FS.readFile(file, 'utf8', function(err, data) {
+            it(name + '.' + index, function() {
 
+                return readFile(file)
+                .then(function(data) {
                     var splitted = normalize(data).split(/\s*@@@\s*/),
                         orig     = splitted[0],
                         should   = splitted[1],
@@ -44,12 +44,10 @@ describe('plugins tests', function() {
                         js2svg  : { pretty: true }
                     });
 
-                    svgo.optimize(orig).then(function(result) {
+                    return svgo.optimize(orig).then(function(result) {
                         //FIXME: results.data has a '\n' at the end while it should not
                         normalize(result.data).should.be.equal(should);
-                        done();
                     });
-
                 });
 
             });
@@ -63,3 +61,12 @@ describe('plugins tests', function() {
 function normalize(file) {
     return file.trim().replace(regEOL, '\n');
 }
+
+function readFile(file) {
+    return new Promise(function(resolve, reject) {
+        FS.readFile(file, 'utf8', function(err, data) {
+            if (err) return reject(err);
+            resolve(data);
+        });
+    });
+}
\ No newline at end of file
diff --git a/test/plugins/collapseGroups.14.svg b/test/plugins/collapseGroups.14.svg
new file mode 100644
index 0000000..d59c4a8
--- /dev/null
+++ b/test/plugins/collapseGroups.14.svg
@@ -0,0 +1,25 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <switch>
+        <g id="a">
+            <g class="i"/>
+        </g>
+        <g id="b" class="n">
+            <g class="i"/>
+        </g>
+        <g>
+            <g/>
+        </g>
+    </switch>
+    
+</svg>
+
+@@@
+
+
+<svg xmlns="http://www.w3.org/2000/svg">
+    <g class="i" id="a"/>
+    <g id="b" class="n">
+        <g class="i"/>
+    </g>
+    <g/>
+</svg>
\ No newline at end of file