smartxml: fixing changing tag of a root node
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Oct 2013 11:05:05 +0000 (13:05 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Oct 2013 11:05:05 +0000 (13:05 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 9a3ba26..aa04fd7 100644 (file)
@@ -107,6 +107,9 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, {
         });
         node.setData(this.getData());
 
         });
         node.setData(this.getData());
 
+        if(this.sameNode(this.document.root)) {
+            defineDocumentProperties(this.document, node._$);
+        }
         this._$.replaceWith(node._$);
         this._setNativeNode(node._$[0]);
         this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
         this._$.replaceWith(node._$);
         this._setNativeNode(node._$[0]);
         this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
@@ -234,15 +237,7 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     loadXML: function(xml) {
     },
 
     loadXML: function(xml) {
-        var $document = $(parseXML(xml));
-
-        var doc = this;
-        Object.defineProperty(this, 'root', {get: function() {
-            return doc.createElementNode($document[0]);
-        }, configurable: true});
-        Object.defineProperty(this, 'dom', {get: function() {
-            return $document[0];
-        }, configurable: true});
+        defineDocumentProperties(this, $(parseXML(xml)));
         
         this.trigger('contentSet');
     },
         
         this.trigger('contentSet');
     },
@@ -252,6 +247,14 @@ $.extend(Document.prototype, Backbone.Events, {
     }
 });
 
     }
 });
 
+var defineDocumentProperties = function(doc, $document) {
+    Object.defineProperty(doc, 'root', {get: function() {
+        return doc.createElementNode($document[0]);
+    }, configurable: true});
+    Object.defineProperty(doc, 'dom', {get: function() {
+        return $document[0];
+    }, configurable: true});
+};
 
 return {
     documentFromXML: function(xml) {
 
 return {
     documentFromXML: function(xml) {
index 938e5f7..e58e754 100644 (file)
@@ -105,6 +105,12 @@ describe('smartxml', function() {
                     expect(node.getTagName()).to.equal('header');
                     expect(node.getData()).to.eql({key: 'value'});
                 });
                     expect(node.getTagName()).to.equal('header');
                     expect(node.getData()).to.eql({key: 'value'});
                 });
+
+                it('can change document root tag name', function() {
+                    var doc = getDocumentFromXML('<div></div>');
+                    doc.root.setTag('span');
+                    expect(doc.root.getTagName()).to.equal('span');
+                });
             });
 
 
             });