X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/c36df0c30a4fb6398823ab4b903054675953aba2..6e641abeaa2fcd8d8f9885db37d127e7d1a34885:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 52236b6..d168e6d 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -33,7 +33,20 @@ $.extend(DocumentNode.prototype, { }, clone: function() { - var clone = this._$.clone(true, true); + var clone = this._$.clone(true, true), + node = this; + clone.find('*').addBack().each(function() { + var el = this, + clonedData = $(this).data(); + + _.pairs(clonedData).forEach(function(pair) { + var key = pair[0], + value = pair[1]; + if(_.isFunction(value.clone)) { + clonedData[key] = value.clone(node.document.createDocumentNode(el)); + } + }); + }); return this.document.createDocumentNode(clone[0]); }, @@ -222,7 +235,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 +263,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 +277,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, { @@ -268,17 +290,25 @@ $.extend(Document.prototype, Backbone.Events, { createDocumentNode: function(from) { if(!(from instanceof Node)) { - if(from.text !== undefined) { - /* globals document */ - from = document.createTextNode(from.text); + if(typeof from === 'string') { + from = parseXML(from); + this.normalizeXML(from); } else { - var node = $('<' + from.tagName + '>'); + if(from.text !== undefined) { + /* 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) { - node.attr(key, from.attrs[key]); - }); + _.keys(from.attrs || {}).forEach(function(key) { + node.attr(key, from.attrs[key]); + }); - from = node[0]; + from = node[0]; + } } } var Factory, typeMethods, typeTransformations; @@ -309,11 +339,16 @@ $.extend(Document.prototype, Backbone.Events, { loadXML: function(xml, options) { options = options || {}; this._defineDocumentProperties($(parseXML(xml))); + this.normalizeXML(this.dom); if(!options.silent) { this.trigger('contentSet'); } }, + normalizeXML: function(nativeNode) { + void(nativeNode); // noop + }, + toXML: function() { return this.root.toXML(); }, @@ -499,8 +534,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) {