CB-9264 - Duplicate entries in config.xml
authorShazron Abdullah <shazron@apache.org>
Wed, 16 Mar 2016 21:02:46 +0000 (14:02 -0700)
committerShazron Abdullah <shazron@apache.org>
Wed, 16 Mar 2016 21:02:46 +0000 (14:02 -0700)
spec/util/xml-helpers.spec.js
src/util/xml-helpers.js

index 8cc46ca..9d714f0 100644 (file)
@@ -267,5 +267,27 @@ describe('xml-helpers', function(){
             expect(testElements.length).toEqual(2);
 
         });
+        
+        it('should remove duplicate preferences (by name attribute value)', function () {
+            var testXml = et.XML(
+                '<?xml version="1.0" encoding="UTF-8"?>\n' +
+                '<widget xmlns     = "http://www.w3.org/ns/widgets"\n' +
+                '        xmlns:cdv = "http://cordova.apache.org/ns/1.0"\n' +
+                '        id        = "io.cordova.hellocordova"\n' +
+                '        version   = "0.0.1">\n' +
+                '    <preference name="Orientation" value="default" />\n' +
+                '    <preference name="Orientation" value="portrait" />\n' +
+                '    <preference name="Orientation" value="landscape" />\n' +
+                '    <platform name="ios">\n' +
+                '        <preference name="Orientation" value="all" />\n' +
+                '        <preference name="Orientation" value="portrait" />\n' +
+                '    </platform>\n' +
+                '</widget>\n'
+            );
+            xml_helpers.mergeXml(testXml, dstXml, 'ios');
+            var testElements = dstXml.findall('preference[@name="Orientation"]');
+            expect(testElements.length).toEqual(1);
+        });
+                
     });
 });
index 8b02989..85d9799 100644 (file)
@@ -218,6 +218,9 @@ function mergeXml(src, dest, platform, clobber) {
 
     //Handle children
     src.getchildren().forEach(mergeChild);
+    
+    //Handle duplicate preference tags (by name attribute)
+    removeDuplicatePreferences(dest);
 
     function mergeChild (srcChild) {
         var srcTag = srcChild.tag,
@@ -254,6 +257,26 @@ function mergeXml(src, dest, platform, clobber) {
             dest.append(destChild);
         }
     }
+    
+    function removeDuplicatePreferences(xml) {
+        // reduce preference tags to a hashtable to remove dupes
+        var prefHash = xml.findall('preference[@name][@value]').reduce(function(previousValue, currentValue) {
+            previousValue[ currentValue.attrib.name ] = currentValue.attrib.value;
+            return previousValue;
+        }, {});
+        
+        // remove all preferences
+        xml.findall('preference[@name][@value]').forEach(function(pref) {
+            xml.remove(pref);
+        });
+        
+        // write new preferences
+        Object.keys(prefHash).forEach(function(key, index) {
+            var element = et.SubElement(xml, 'preference');
+            element.set('name', key);
+            element.set('value', this[key]);
+        }, prefHash);
+    }
 }
 
 // Expose for testing.