You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bh...@apache.org on 2013/08/09 16:10:31 UTC

git commit: [CB-4543] Changed implementation of graftXML to create parent if its not present

Updated Branches:
  refs/heads/master 2ff0e708b -> 55dda303b


[CB-4543] Changed implementation of graftXML to create parent if its not present


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

Branch: refs/heads/master
Commit: 55dda303bbd09a9063d96b57a8f608abe5c802ae
Parents: 2ff0e70
Author: Jeffrey Heifetz <jh...@blackberry.com>
Authored: Thu Aug 8 12:40:46 2013 -0400
Committer: Bryan Higgins <bh...@blackberry.com>
Committed: Fri Aug 9 10:11:29 2013 -0400

----------------------------------------------------------------------
 spec/util/xml-helpers.spec.js | 45 +++++++++++++++++++++++++-------------
 src/util/xml-helpers.js       | 16 ++++++++++++--
 2 files changed, 44 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/55dda303/spec/util/xml-helpers.spec.js
----------------------------------------------------------------------
diff --git a/spec/util/xml-helpers.spec.js b/spec/util/xml-helpers.spec.js
index 0bf2946..5def06e 100644
--- a/spec/util/xml-helpers.spec.js
+++ b/spec/util/xml-helpers.spec.js
@@ -40,37 +40,37 @@ describe('xml-helpers', function(){
 
         it('should return true for identical tags', function(){
             expect(xml_helpers.equalNodes(usesNetworkOne, usesNetworkTwo)).toBe(true);
-        });   
-        
+        });
+
         it('should return false for different attributes', function(){
             expect(xml_helpers.equalNodes(usesNetworkOne, usesReceive)).toBe(false);
-        });  
-        
+        });
+
         it('should distinguish between text', function(){
             expect(xml_helpers.equalNodes(helloTagOne, goodbyeTag)).toBe(false);
-        });  
-        
+        });
+
         it('should ignore whitespace in text', function(){
             expect(xml_helpers.equalNodes(helloTagOne, helloTagTwo)).toBe(true);
-        });    
-        
+        });
+
         describe('should compare children', function(){
             it('by child quantity', function(){
                 var one = et.XML('<i><b>o</b></i>'),
                     two = et.XML('<i><b>o</b><u></u></i>');
-        
-                expect(xml_helpers.equalNodes(one, two)).toBe(false);        
+
+                expect(xml_helpers.equalNodes(one, two)).toBe(false);
             });
-            
+
             it('by child equality', function(){
                 var one = et.XML('<i><b>o</b></i>'),
                     two = et.XML('<i><u></u></i>'),
                     uno = et.XML('<i>\n<b>o</b>\n</i>');
-        
-                expect(xml_helpers.equalNodes(one, uno)).toBe(true); 
-                expect(xml_helpers.equalNodes(one, two)).toBe(false);       
+
+                expect(xml_helpers.equalNodes(one, uno)).toBe(true);
+                expect(xml_helpers.equalNodes(one, two)).toBe(false);
             });
-        }); 
+        });
     });
     describe('pruneXML', function() {
         var config_xml;
@@ -124,5 +124,20 @@ describe('xml-helpers', function(){
             xml_helpers.graftXML(config_xml, children, '/*');
             expect(config_xml.findall('access').length).toEqual(3);
         });
+
+        it('for simple XPath paths, the parent should be created if not present', function () {
+            var doc = new et.ElementTree(et.XML('<widget>')),
+                children = [et.XML('<rim:permits> super_awesome_permission </rim:permits>')],
+                selector= "/widget/rim:permissions";
+            expect(xml_helpers.graftXML(doc, children, selector)).toBe(true);
+            expect(et.tostring(doc.getroot())).toContain("<rim:permissions><rim:permits> super_awesome_permission </rim:permits></rim:permissions>");
+        });
+
+        it('returns false for more complicated selectors', function () {
+            var doc = new et.ElementTree(et.XML('<widget>')),
+                children = [et.XML('<rim:permits> super_awesome_permission </rim:permits>')],
+                selector= "/bookstore/book[price>35]/title";
+            expect(xml_helpers.graftXML(doc, children, selector)).toBe(false);
+        });
     });
 });

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/55dda303/src/util/xml-helpers.js
----------------------------------------------------------------------
diff --git a/src/util/xml-helpers.js b/src/util/xml-helpers.js
index 667a307..cfb914c 100644
--- a/src/util/xml-helpers.js
+++ b/src/util/xml-helpers.js
@@ -71,10 +71,22 @@ module.exports = {
         return true;
     },
 
-    // adds node to doc at selector
+    // adds node to doc at selector, creating parent if it doesn't exist
     graftXML: function(doc, nodes, selector) {
         var parent = resolveParent(doc, selector);
-        if (!parent) return false;
+        if (!parent) {
+            //Try to create the parent recursively if necessary
+            try {
+                var parentToCreate = et.XML("<" + path.basename(selector) + ">"),
+                    parentSelector = path.dirname(selector);
+
+                this.graftXML(doc, [parentToCreate], parentSelector);
+            } catch (e) {
+                return false;
+            }
+            parent = resolveParent(doc, selector);
+            if (!parent) return false;
+        }
 
         nodes.forEach(function (node) {
             // check if child is unique first