X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/b31ffcb46eae55b9c66457e2330b16e8db84f1d6..85331f59b2aa174291878f90633f7189fd221628:/src/smartxml/smartxml.js?ds=inline diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d9e44be..e525c14 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -34,6 +34,16 @@ $.extend(DocumentNode.prototype, { clone: function() { var clone = this._$.clone(true, true); + clone.find('*').addBack().each(function() { + var clonedData = $(this).data(); + _.pairs(clonedData).forEach(function(pair) { + var key = pair[0], + value = pair[1]; + if(_.isFunction(value.clone)) { + clonedData[key] = value.clone(); + } + }); + }); return this.document.createDocumentNode(clone[0]); }, @@ -222,7 +232,12 @@ $.extend(TextNode.prototype, { var parseXML = function(xml) { - return $($.trim(xml))[0]; + var toret = $($.trim(xml)); + if(!toret.length) { + throw new Error('Unable to parse XML: ' + xml); + } + return toret[0]; + }; var registerTransformation = function(desc, name, target) { @@ -245,8 +260,7 @@ var registerMethod = function(methodName, method, target) { }; -var Document = function(xml) { - this.loadXML(xml); +var Document = function(xml, extensions) { this.undoStack = []; this.redoStack = []; this._transactionStack = []; @@ -260,6 +274,11 @@ var Document = function(xml) { this._elementNodeTransformations = {}; this.registerExtension(coreTransformations); + + (extensions || []).forEach(function(extension) { + this.registerExtension(extension); + }.bind(this)); + this.loadXML(xml); }; $.extend(Document.prototype, Backbone.Events, { @@ -276,6 +295,9 @@ $.extend(Document.prototype, Backbone.Events, { /* globals document */ from = document.createTextNode(from.text); } else { + if(!from.tagName) { + throw new Error('tagName missing'); + } var node = $('<' + from.tagName + '>'); _.keys(from.attrs || {}).forEach(function(key) { @@ -509,8 +531,10 @@ $.extend(Document.prototype, Backbone.Events, { throw new Error('End of transaction requested, but there is no transaction in progress!'); } this._transactionInProgress = false; - this.undoStack.push(this._transactionStack); - this._transactionStack = []; + if(this._transactionStack.length) { + this.undoStack.push(this._transactionStack); + this._transactionStack = []; + } }, getNodeByPath: function(path) {