From: Aleksander Ɓukasz Date: Wed, 23 Oct 2013 11:05:05 +0000 (+0200) Subject: smartxml: fixing changing tag of a root node X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e1fef8a5849274a64f2c75ea4c7a261d67cea1a3?hp=36de4837650d53704c67a9f23a4d68b1287b03ec smartxml: fixing changing tag of a root node --- 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'); + }); });