You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by dp...@apache.org on 2019/04/14 06:05:04 UTC

[cordova-common] branch master updated: implement setPreference and setPlatformPreference

This is an automated email from the ASF dual-hosted git repository.

dpogue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-common.git


The following commit(s) were added to refs/heads/master by this push:
     new a5cd232  implement setPreference and setPlatformPreference
a5cd232 is described below

commit a5cd2321c52604e0bca6f429cc9c6741e2aa0346
Author: Luo Gang <cr...@gmail.com>
AuthorDate: Tue Jan 22 17:12:43 2019 +0800

    implement setPreference and setPlatformPreference
---
 spec/ConfigParser/ConfigParser.spec.js | 15 +++++++++++++++
 src/ConfigParser/ConfigParser.js       | 29 +++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/spec/ConfigParser/ConfigParser.spec.js b/spec/ConfigParser/ConfigParser.spec.js
index e78f39b..a2f13b8 100644
--- a/spec/ConfigParser/ConfigParser.spec.js
+++ b/spec/ConfigParser/ConfigParser.spec.js
@@ -108,6 +108,14 @@ describe('config.xml parser', function () {
             it('Test 012 : should return an empty string for a non-existing preference', function () {
                 expect(cfg.getPreference('zimzooo!')).toEqual('');
             });
+            it('should allow setting the preference', function () {
+                cfg.setPreference('orientation', 'landscape');
+                expect(cfg.getPreference('orientation')).toEqual('landscape');
+            });
+            it('should allow setting the platform specific preference', function () {
+                cfg.setPreference('android-minSdkVersion', 'android', '11');
+                expect(cfg.getPreference('android-minSdkVersion', 'android')).toEqual('11');
+            });
         });
         describe('global preference', function () {
             it('Test 013 : should return the value of a global preference', function () {
@@ -127,6 +135,13 @@ describe('config.xml parser', function () {
             it('Test 017 : should return an empty string when querying with unsupported platform', function () {
                 expect(cfg.getPlatformPreference('orientation', 'foobar')).toEqual('');
             });
+            it('should allow setting the platform specific preference', function () {
+                cfg.setPlatformPreference('orientation', 'android', 'foobar');
+                expect(cfg.getPlatformPreference('orientation', 'android')).toEqual('foobar');
+            });
+            it('should throw when setting a preference for unsupported platform', function () {
+                expect(function () { cfg.setPlatformPreference('orientation', 'foobar', 'landscape'); }).toThrow();
+            });
         });
         describe('plugin', function () {
             it('Test 018 : should read plugin id list', function () {
diff --git a/src/ConfigParser/ConfigParser.js b/src/ConfigParser/ConfigParser.js
index 7f3f7fe..cfc40cd 100644
--- a/src/ConfigParser/ConfigParser.js
+++ b/src/ConfigParser/ConfigParser.js
@@ -168,6 +168,23 @@ ConfigParser.prototype = {
     getPlatformPreference: function (name, platform) {
         return findElementAttributeValue(name, this.doc.findall('./platform[@name="' + platform + '"]/preference'));
     },
+    setPlatformPreference: function (name, platform, value) {
+        const platformEl = this.doc.find('./platform[@name="' + platform + '"]');
+        if (!platformEl) {
+            throw new CordovaError('platform does not exist (received platform: ' + platform + ')');
+        }
+        const elems = this.doc.findall('./platform[@name="' + platform + '"]/preference');
+        let pref = elems.filter(function (elem) {
+            return elem.attrib.name.toLowerCase() === name.toLowerCase();
+        }).pop();
+
+        if (!pref) {
+            pref = new et.Element('preference');
+            pref.attrib.name = name;
+            platformEl.append(pref);
+        }
+        pref.attrib.value = value;
+    },
     getPreference: function (name, platform) {
 
         var platformPreference = '';
@@ -179,6 +196,18 @@ ConfigParser.prototype = {
         return platformPreference || this.getGlobalPreference(name);
 
     },
+    setPreference: function (name, platform, value) {
+        if (!value) {
+            value = platform;
+            platform = undefined;
+        }
+
+        if (platform) {
+            this.setPlatformPreference(name, platform, value);
+        } else {
+            this.setGlobalPreference(name, value);
+        }
+    },
     /**
      * Returns all resources for the platform specified.
      * @param  {String} platform     The platform.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org