From e1fef8a5849274a64f2c75ea4c7a261d67cea1a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 23 Oct 2013 13:05:05 +0200 Subject: [PATCH] smartxml: fixing changing tag of a root node --- src/smartxml/smartxml.js | 21 ++++++++++++--------- src/smartxml/smartxml.test.js | 6 ++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 9a3ba26..aa04fd7 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -107,6 +107,9 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { }); 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()}); @@ -234,15 +237,7 @@ $.extend(Document.prototype, Backbone.Events, { }, 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'); }, @@ -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) { diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 938e5f7..e58e754 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -105,6 +105,12 @@ describe('smartxml', function() { expect(node.getTagName()).to.equal('header'); expect(node.getData()).to.eql({key: 'value'}); }); + + it('can change document root tag name', function() { + var doc = getDocumentFromXML('
'); + doc.root.setTag('span'); + expect(doc.root.getTagName()).to.equal('span'); + }); }); -- 2.20.1