smartxml: fixing detachment of the document root
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 14 Feb 2014 13:59:54 +0000 (14:59 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 14 Feb 2014 14:46:52 +0000 (15:46 +0100)
Document instance's reference to the root wasn't being cleared

src/smartxml/core.js
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index ec2c643..ae445e0 100644 (file)
@@ -32,6 +32,10 @@ var documentNodeTransformations = {
         this._$.detach();
         if(existed) {
             this.triggerChangeEvent('nodeDetached', {parent: parent});
+            if(!parent) {
+                // This was the root of the document
+                this.document._defineDocumentProperties(null);
+            }
         }
         return this;
     },
index 7eab11b..203bdb2 100644 (file)
@@ -598,9 +598,15 @@ $.extend(Document.prototype, Backbone.Events, {
     _defineDocumentProperties: function($document) {
         var doc = this;
         Object.defineProperty(doc, 'root', {get: function() {
+            if(!$document) {
+                return null;
+            }
             return doc.createDocumentNode($document[0]);
         }, configurable: true});
         Object.defineProperty(doc, 'dom', {get: function() {
+            if(!$document) {
+                return null;
+            }
             return $document[0];
         }, configurable: true});
     }
index 3410407..b152d26 100644 (file)
@@ -385,6 +385,15 @@ describe('smartxml', function() {
 
     describe('Manipulations', function() {
 
+        describe('detaching nodes', function() {
+            it('can detach document root node', function() {
+                var doc = getDocumentFromXML('<div></div>');
+
+                doc.root.detach();
+                expect(doc.root).to.equal(null);
+            });
+        });
+
         describe('replacing node with another one', function() {
             it('replaces node with another one', function() {
                 var doc = getDocumentFromXML('<div><a></a></div>'),