From 729243168cee79db77626f00e5b0073f692aba2e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 14 Feb 2014 14:59:54 +0100 Subject: [PATCH] smartxml: fixing detachment of the document root Document instance's reference to the root wasn't being cleared --- src/smartxml/core.js | 4 ++++ src/smartxml/smartxml.js | 6 ++++++ src/smartxml/smartxml.test.js | 9 +++++++++ 3 files changed, 19 insertions(+) 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('
'), -- 2.20.1