CB-9264 - Duplicate entries in config.xml
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/cc916564 Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/cc916564 Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/cc916564 Branch: refs/heads/master Commit: cc9165647197e63de43bab0335d4f8fcd07f9858 Parents: 186eea0 Author: Shazron Abdullah <shaz...@apache.org> Authored: Wed Mar 16 14:02:46 2016 -0700 Committer: Shazron Abdullah <shaz...@apache.org> Committed: Wed Mar 16 14:02:46 2016 -0700 ---------------------------------------------------------------------- cordova-common/spec/util/xml-helpers.spec.js | 22 ++++++++++++++++++++++ cordova-common/src/util/xml-helpers.js | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/cc916564/cordova-common/spec/util/xml-helpers.spec.js ---------------------------------------------------------------------- diff --git a/cordova-common/spec/util/xml-helpers.spec.js b/cordova-common/spec/util/xml-helpers.spec.js index 8cc46ca..9d714f0 100644 --- a/cordova-common/spec/util/xml-helpers.spec.js +++ b/cordova-common/spec/util/xml-helpers.spec.js @@ -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); + }); + }); }); http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/cc916564/cordova-common/src/util/xml-helpers.js ---------------------------------------------------------------------- diff --git a/cordova-common/src/util/xml-helpers.js b/cordova-common/src/util/xml-helpers.js index 8b02989..85d9799 100644 --- a/cordova-common/src/util/xml-helpers.js +++ b/cordova-common/src/util/xml-helpers.js @@ -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. --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org