From: Aleksander Ɓukasz Date: Wed, 23 Oct 2013 10:13:13 +0000 (+0200) Subject: smartxml: loading new xml content to an existing document instance X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/5b4122c6f8f4fef93526a0029aa6a4ba5f329cfa?hp=512fcabb2a882e26649612efb2b91f25cfc02ec3 smartxml: loading new xml content to an existing document instance --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index cb90e74..80ee43c 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -214,16 +214,9 @@ var parseXML = function(xml) { }; var Document = function(xml) { - var $document = $(parseXML(xml)); - - var doc = this; - Object.defineProperty(this, 'root', {get: function() { - return doc.createElementNode($document[0]); - }}); - Object.defineProperty(this, 'dom', {get: function() { - return $document[0]; - }}); + this.loadXML(xml); }; + $.extend(Document.prototype, Backbone.Events, { ElementNodeFactory: ElementNode, TextNodeFactory: TextNode, @@ -239,6 +232,20 @@ $.extend(Document.prototype, Backbone.Events, { return new this.TextNodeFactory(nativeNode, this); }, + 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}); + + this.trigger('contentSet'); + }, + toXML: function() { return this.root.toXML(); } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index d997aa7..da1b335 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -30,6 +30,15 @@ describe('smartxml', function() { var doc = getDocumentFromXML('
'); expect(doc.root.getTagName()).to.equal('div'); }); + + it('can resets its content entirely', function() { + var doc = getDocumentFromXML('
'); + + expect(doc.root.getTagName()).to.equal('div'); + + doc.loadXML('
'); + expect(doc.root.getTagName()).to.equal('header'); + }); }); describe('Basic ElementNode properties', function() {