From: Aleksander Ɓukasz Date: Fri, 14 Feb 2014 13:59:54 +0000 (+0100) Subject: smartxml: fixing detachment of the document root X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/729243168cee79db77626f00e5b0073f692aba2e smartxml: fixing detachment of the document root Document instance's reference to the root wasn't being cleared --- diff --git a/src/smartxml/core.js b/src/smartxml/core.js index ec2c643..ae445e0 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -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; }, diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 7eab11b..203bdb2 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -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}); } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 3410407..b152d26 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -385,6 +385,15 @@ describe('smartxml', function() { describe('Manipulations', function() { + describe('detaching nodes', function() { + it('can detach document root node', function() { + var doc = getDocumentFromXML('
'); + + 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('
'),