editor: canvas cursor fix - handle node with Zero Width Space only correctly
[fnpeditor.git] / src / smartxml / smartxml.js
index d9e44be..8bf531b 100644 (file)
@@ -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, {
@@ -276,6 +298,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) {
@@ -416,6 +441,27 @@ $.extend(Document.prototype, Backbone.Events, {
         });
     },
 
+    ifChanged: function(context, action, documentChangedHandler, documentUnchangedHandler) {
+        var hasChanged = false,
+            changeMonitor = function() {
+                hasChanged = true;
+            };
+
+        this.on('change', changeMonitor);
+        action.call(context);
+        this.off('change', changeMonitor);
+        
+        if(hasChanged) {
+            if(documentChangedHandler) {
+                documentChangedHandler.call(context);
+            }
+        } else {
+            if(documentUnchangedHandler) {
+                documentUnchangedHandler.call(context);
+            }
+        }
+    },
+
     transform: function(Transformation, args) {
         var toret, transformation;
 
@@ -426,17 +472,26 @@ $.extend(Document.prototype, Backbone.Events, {
         }
         if(transformation) {
             this._transformationLevel++;
-            toret = transformation.run({beUndoable:this._transformationLevel === 1});
-            if(this._transformationLevel === 1 && !this._undoInProgress) {
-                if(this._transactionInProgress) {
-                    this._transactionStack.push(transformation);
-                } else {
-                    this.undoStack.push(transformation);
+            
+            this.ifChanged(
+                this,
+                function() {
+                    toret = transformation.run({beUndoable:this._transformationLevel === 1});
+                },
+                function() {
+                    if(this._transformationLevel === 1 && !this._undoInProgress) {
+                        if(this._transactionInProgress) {
+                            this._transactionStack.push(transformation);
+                        } else {
+                            this.undoStack.push(transformation);
+                        }
+                    }
+                    if(!this._undoInProgress && this._transformationLevel === 1) {
+                        this.redoStack = [];
+                    }
                 }
-            }
-            if(!this._undoInProgress && this._transformationLevel === 1) {
-                this.redoStack = [];
-            }
+            );
+
             this._transformationLevel--;
             return toret;
         } else {
@@ -509,8 +564,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) {