smartxml: Fixing changing node's tag
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Oct 2013 11:09:19 +0000 (13:09 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Oct 2013 11:09:19 +0000 (13:09 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index aa04fd7..b90a042 100644 (file)
@@ -100,7 +100,8 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, {
 
     setTag: function(tagName) {
         var node = this.document.createElementNode({tagName: tagName}),
 
     setTag: function(tagName) {
         var node = this.document.createElementNode({tagName: tagName}),
-            oldTagName = this.getTagName();
+            oldTagName = this.getTagName(),
+            myContents = this._$.contents();
 
         this.getAttrs().forEach(function(attribute) {
             node.setAttr(attribute.name, attribute.value, true);
 
         this.getAttrs().forEach(function(attribute) {
             node.setAttr(attribute.name, attribute.value, true);
@@ -112,6 +113,7 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, {
         }
         this._$.replaceWith(node._$);
         this._setNativeNode(node._$[0]);
         }
         this._$.replaceWith(node._$);
         this._setNativeNode(node._$[0]);
+        this._$.append(myContents);
         this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
     },
 
         this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
     },
 
index e58e754..597c107 100644 (file)
@@ -111,6 +111,12 @@ describe('smartxml', function() {
                     doc.root.setTag('span');
                     expect(doc.root.getTagName()).to.equal('span');
                 });
                     doc.root.setTag('span');
                     expect(doc.root.getTagName()).to.equal('span');
                 });
+
+                it('keeps contents', function() {
+                    var node = elementNodeFromXML('<div><div></div></div>');
+                    node.setTag('header');
+                    expect(node.contents()).to.have.length(1);
+                });
             });
 
 
             });