cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [4/4] cordova-lib git commit: ConfigParser: refactor getPreference()
Date Sun, 04 Jan 2015 01:53:32 GMT
ConfigParser: refactor getPreference()


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/b2e33f55
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/b2e33f55
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/b2e33f55

Branch: refs/heads/master
Commit: b2e33f55e98f23116066f47f9b6ea5b49474d95c
Parents: 1c1a2cc
Author: Marko Raatikka <marko.raatikka@gmail.com>
Authored: Thu Jan 1 21:29:14 2015 +0200
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Sat Jan 3 20:52:04 2015 -0500

----------------------------------------------------------------------
 cordova-lib/spec-cordova/ConfigParser.spec.js | 29 +++++++++++---
 cordova-lib/spec-cordova/test-config.xml      |  2 +
 cordova-lib/src/configparser/ConfigParser.js  | 45 ++++++++++++++++------
 3 files changed, 58 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/b2e33f55/cordova-lib/spec-cordova/ConfigParser.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/ConfigParser.spec.js b/cordova-lib/spec-cordova/ConfigParser.spec.js
index 483ce6c..e3eed8e 100644
--- a/cordova-lib/spec-cordova/ConfigParser.spec.js
+++ b/cordova-lib/spec-cordova/ConfigParser.spec.js
@@ -85,16 +85,33 @@ describe('config.xml parser', function () {
             });
         });
         describe('preference', function() {
-            it('should get value of existing preference', function() {
+            it('should return the value of a global preference', function() {
                 expect(cfg.getPreference('fullscreen')).toEqual('true');
             });
-            it('should get undefined as non existing preference', function() {
-                expect(cfg.getPreference('zimzooo!')).toEqual(undefined);
+            it('should return the value of a platform-specific preference', function() {
+                expect(cfg.getPreference('android-minSdkVersion', 'android')).toEqual('10');
+            });
+            it('should return an empty string for a non-existing preference', function()
{
+                expect(cfg.getPreference('zimzooo!')).toEqual('');
             });
         });
-        describe('platform specific preference', function() {
-            it('should get value of existing platform specific preference', function() {
-                expect(cfg.getPreference('android-minSdkVersion', 'android')).toEqual('10');
+        describe('global preference', function() {
+            it('should return the value of a global preference', function() {
+                expect(cfg.getGlobalPreference('orientation')).toEqual('portrait');
+            });
+            it('should return an empty string for a non-existing preference', function()
{
+                expect(cfg.getGlobalPreference('foobar')).toEqual('');
+            });
+        });
+        describe('platform-specific preference', function() {
+            it('should return the value of a platform specific preference', function() {
+                expect(cfg.getPlatformPreference('orientation', 'android')).toEqual('landscape');
+            });
+            it('should return an empty string when querying for a non-existing preference',
function() {
+                expect(cfg.getPlatformPreference('foobar', 'android')).toEqual('');
+            });
+            it('should return an empty string when querying with unsupported platform', function()
{
+                expect(cfg.getPlatformPreference('orientation', 'foobar')).toEqual('');
             });
         });
         describe('feature',function(){

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/b2e33f55/cordova-lib/spec-cordova/test-config.xml
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/test-config.xml b/cordova-lib/spec-cordova/test-config.xml
index 1852fb5..9f2490d 100644
--- a/cordova-lib/spec-cordova/test-config.xml
+++ b/cordova-lib/spec-cordova/test-config.xml
@@ -20,11 +20,13 @@
     <access origin="*" />
     <preference name="fullscreen" value="true" />
     <preference name="webviewbounce" value="true" />
+    <preference name="orientation" value="portrait" />
     <icon id="icon" src="icon.png" />
     <icon id="logo" src="logo.png" width="255" height="255" />
     <platform name="android">
         <icon src="logo-android.png" width="255" height="255" density="mdpi" />
         <preference name="android-minSdkVersion" value="10" />
+        <preference name="orientation" value="landscape" />
     </platform>
 
     <!-- Features -->

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/b2e33f55/cordova-lib/src/configparser/ConfigParser.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js
index 4a58f42..8b5736b 100644
--- a/cordova-lib/src/configparser/ConfigParser.js
+++ b/cordova-lib/src/configparser/ConfigParser.js
@@ -82,6 +82,25 @@ function getCordovaNamespacePrefix(doc){
     return prefix;
 }
 
+/**
+ * Finds the value of an element's attribute
+ * @param  {String} attributeName Name of the attribute to search for
+ * @param  {Array}  elems         An array of ElementTree nodes
+ * @return {String}
+ */
+function findElementAttributeValue(attributeName, elems) {
+
+    elems = Array.isArray(elems) ? elems : [ elems ];
+
+    var value = elems.filter(function (elem) {
+        return elem.attrib.name.toLowerCase() === attributeName.toLowerCase();
+    }).map(function (filteredElems) {
+        return filteredElems.attrib.value;
+    }).pop();
+
+    return value ? value : '';
+}
+
 ConfigParser.prototype = {
     packageName: function(id) {
         return this.doc.getroot().attrib['id'];
@@ -124,20 +143,22 @@ ConfigParser.prototype = {
     author: function() {
         return getNodeTextSafe(this.doc.find('author'));
     },
+    getGlobalPreference: function (name) {
+        return findElementAttributeValue(name, this.doc.findall('preference'));
+    },
+    getPlatformPreference: function (name, platform) {
+        return findElementAttributeValue(name, this.doc.findall('platform[@name=\'' + platform
+ '\']/preference'));
+    },
     getPreference: function(name, platform) {
-        var preferences = this.doc.findall('preference');
-        if (platform) { // include platform specific preferences
-            preferences = preferences.concat(
-                this.doc.findall('platform[@name=\'' + platform + '\']/preference'));
+
+        var platformPreference = '';
+
+        if (platform) {
+            platformPreference = this.getPlatformPreference(name, platform);
         }
-        var ret = null;
-        preferences.forEach(function (preference) {
-            // Take the last one that matches.
-            if (preference.attrib.name.toLowerCase() === name.toLowerCase()) {
-                ret = preference.attrib.value;
-            }
-        });
-        return ret;
+
+        return platformPreference ? platformPreference : this.getGlobalPreference(name);
+
     },
     /**
      * Returns all resources for the platform specified.


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


Mime
View raw message