You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/01/18 01:03:53 UTC

[1/2] git commit: Added support for elements in config.xml (specs included).

Added support for <preference> elements in config.xml (specs included).


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

Branch: refs/heads/master
Commit: af5d44f1182330d030d7061e66a1718867c1ebaa
Parents: 161446a
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Jan 17 16:00:19 2013 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Jan 17 16:00:19 2013 -0800

----------------------------------------------------------------------
 spec/config_parser.spec.js |   41 +++++++++++++++++++++++++++++++++++++++
 src/config_parser.js       |   33 +++++++++++++++++++++++++++++++
 src/metadata/ios_parser.js |   10 ++------
 3 files changed, 77 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/af5d44f1/spec/config_parser.spec.js
----------------------------------------------------------------------
diff --git a/spec/config_parser.spec.js b/spec/config_parser.spec.js
index b2727ff..947ce20 100644
--- a/spec/config_parser.spec.js
+++ b/spec/config_parser.spec.js
@@ -105,4 +105,45 @@ describe('config.xml parser', function () {
             });
         });
     });
+
+    describe('preference elements', function() {
+        var cfg;
+
+        beforeEach(function() {
+            cfg = new config_parser(xml);
+        });
+
+        describe('getter', function() {
+            it('should get all preference elements', function() {
+                expect(cfg.preference.get()[0].name).toEqual('phonegap-version');
+                expect(cfg.preference.get()[0].value).toEqual('1.9.0');
+            });
+            it('should return an array of all preference name/value pairs', function() {
+                expect(cfg.preference.get() instanceof Array).toBe(true);
+            });
+        });
+        describe('setters', function() {
+            it('should allow removing a preference by name', function() {
+                cfg.preference.remove('phonegap-version');
+                expect(cfg.preference.get().length).toEqual(3);
+            });
+            it('should write to disk after removing a preference', function() {
+                cfg.preference.remove('phonegap-version');
+                expect(fs.readFileSync(xml, 'utf-8')).not.toMatch(/<preference\sname="phonegap-version"/);
+            });
+            it('should allow adding a new preference', function() {
+                cfg.preference.add({name:'UIWebViewBounce',value:'false'});
+                expect(cfg.preference.get().length).toEqual(5);
+                expect(cfg.preference.get()[4].value).toEqual('false');
+            });
+            it('should write to disk after adding a preference', function() {
+                cfg.preference.add({name:'UIWebViewBounce',value:'false'});
+                expect(fs.readFileSync(xml, 'utf-8')).toMatch(/<preference name="UIWebViewBounce" value="false"/);
+            });
+            it('should allow removing all preference elements when no parameter is specified', function() {
+                cfg.preference.remove();
+                expect(fs.readFileSync(xml, 'utf-8')).not.toMatch(/<preference.*\/>/);
+            });
+        });
+    });
 });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/af5d44f1/src/config_parser.js
----------------------------------------------------------------------
diff --git a/src/config_parser.js b/src/config_parser.js
index f68a0e9..6192ee5 100644
--- a/src/config_parser.js
+++ b/src/config_parser.js
@@ -5,6 +5,7 @@ function config_parser(path) {
     this.path = path;
     this.doc = new et.ElementTree(et.XML(fs.readFileSync(path, 'utf-8')));
     this.access = new access(this);
+    this.preference = new preference(this);
 }
 
 config_parser.prototype = {
@@ -51,4 +52,36 @@ access.prototype = {
     }
 };
 
+function preference(cfg) {
+    this.config = cfg;
+};
+
+preference.prototype = {
+    add:function(pref) {
+        var el = new et.Element('preference');
+        el.attrib.name = pref.name;
+        el.attrib.value = pref.value;
+        this.config.doc.getroot().append(el);
+        this.config.update();
+    },
+    remove:function(name) {
+        var self = this;
+        var els = [];
+        if (name) els = this.config.doc.findall('preference[@name="' + name + '"]');
+        else els = this.config.doc.findall('preference');
+        els.forEach(function(a) {
+            self.config.doc.getroot().remove(0, a);
+        });
+        this.config.update();
+    },
+    get:function() {
+        return this.config.doc.findall('preference').map(function(a) {
+            return {
+                name:a.attrib.name,
+                value:a.attrib.value
+            };
+        });
+    }
+};
+
 module.exports = config_parser;

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/af5d44f1/src/metadata/ios_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/ios_parser.js b/src/metadata/ios_parser.js
index bda23c1..422690d 100644
--- a/src/metadata/ios_parser.js
+++ b/src/metadata/ios_parser.js
@@ -38,15 +38,11 @@ module.exports.prototype = {
 
         // Update whitelist
         var self = this;
-        this.config.doc.findall('access').forEach(function(a) {
-            self.config.doc.getroot().remove(0, a);
-        });
+        // Remove old access elements
+        this.config.access.remove();
         config.access.get().forEach(function(uri) {
-            var el = new et.Element('access');
-            el.attrib.origin = uri;
-            self.config.doc.getroot().append(el);
+            self.config.access.add(uri);
         });
-        this.config.update();
         
         // Update product name
         var proj = new xcode.project(this.pbxproj);