wip: integrating lists, first extensions api approach
[fnpeditor.git] / src / smartxml / smartxml.js
index 712c0ad..6cc3e33 100644 (file)
@@ -242,12 +242,18 @@ $.extend(ElementNode.prototype, {
         return this.nativeNode.tagName.toLowerCase();
     },
 
-    contents: function() {
+    contents: function(selector) {
         var toret = [],
             document = this.document;
-        this._$.contents().each(function() {
-            toret.push(document.createDocumentNode(this));
-        });
+        if(selector) {
+            this._$.children(selector).each(function() {
+                toret.push(document.createDocumentNode(this));
+            });
+        } else {
+            this._$.contents().each(function() {
+                toret.push(document.createDocumentNode(this));
+            });
+        }
         return toret;
     },
 
@@ -389,6 +395,17 @@ ElementNode.prototype.transformations.register(transformations.createContextTran
     }
 }));
 
+ElementNode.prototype.transformations.register(transformations.createContextTransformation({
+    name: 'smartxml.setAttr2',
+    impl: function(args) {
+        this.prevAttr = this.getAttr(args.name);
+        this.setAttr(args.name, args.value);
+    },
+    undo: function(args) {
+        this.setAttr(args.name, this.prevAttr);
+    }
+}));
+
 DocumentNode.prototype.transformations.register(transformations.createContextTransformation({
     name: 'smartxml.wrapWith',
     getChangeRoot: function() {
@@ -536,6 +553,10 @@ TextNode.prototype.transformations.register(transformations.createContextTransfo
     },
     getChangeRoot: function() {
         return this.context.parent().parent();
+    },
+    isAllowed: function(args) {
+        var parent = this.parent();
+        return !!(parent && parent.parent());
     }
 }));
 
@@ -559,6 +580,7 @@ var Document = function(xml) {
     this.loadXML(xml);
     this.undoStack = [];
     this.redoStack = [];
+    this._transformationLevel = 0;
 };
 
 $.extend(Document.prototype, Backbone.Events, {
@@ -746,8 +768,12 @@ $.extend(Document.prototype, Backbone.Events, {
             }
         } 
         if(transformation) {
+            this._transformationLevel++;
             toret = transformation.run();
-            this.undoStack.push(transformation);
+            if(this._transformationLevel === 1) {
+                this.undoStack.push(transformation);
+            }
+            this._transformationLevel--;
             console.log('clearing redo stack');
             this.redoStack = [];
             return toret;