registering textNode/elementNode transformations
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 6 Dec 2013 09:29:19 +0000 (10:29 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:48 +0000 (22:32 +0100)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index ac59532..dbe0290 100644 (file)
@@ -496,6 +496,8 @@ var Document = function(xml) {
     this._textNodeMethods = {};
     this._elementNodeMethods = {};
     this._nodeTransformations = {};
+    this._textNodeTransformations = {};
+    this._elementNodeTransformations = {};
 };
 
 $.extend(Document.prototype, Backbone.Events, {
@@ -517,18 +519,21 @@ $.extend(Document.prototype, Backbone.Events, {
                 from = node[0];
             }
         }
-        var Factory, typeMethods;
+        var Factory, typeMethods, typeTransformations;
         if(from.nodeType === Node.TEXT_NODE) {
             Factory = this.TextNodeFactory;
             typeMethods = this._textNodeMethods;
+            typeTransformations = this._textNodeTransformations;
         } else if(from.nodeType === Node.ELEMENT_NODE) {
             Factory = this.ElementNodeFactory;
             typeMethods = this._elementNodeMethods;
+            typeTransformations = this._elementNodeTransformations;
         }
         var toret = new Factory(from, this);
         _.extend(toret, this._nodeMethods);
         _.extend(toret, typeMethods);
         _.extend(toret, this._nodeTransformations);
+        _.extend(toret, typeTransformations);
         return toret;
     },
 
@@ -689,12 +694,15 @@ $.extend(Document.prototype, Backbone.Events, {
         registerMethod(methodName, method, destination);
     },
 
-    registerDocumentTransformation: function(desc, name) {
-        registerTransformation(desc, name, this);
-    },
-
-    registerNodeTransformation: function(desc, name) {
-        registerTransformation(desc, name, this._nodeTransformations);
+    registerTransformation: function(desc, name, dstName) {
+        var doc = this;
+        var destination = {
+            document: doc,
+            documentNode: doc._nodeTransformations,
+            textNode: doc._textNodeTransformations,
+            elementNode: doc._elementNodeTransformations
+        }[dstName];
+        registerTransformation(desc, name, destination);
     },
 
     registerExtension: function(extension) {
@@ -718,10 +726,8 @@ $.extend(Document.prototype, Backbone.Events, {
                 if(dstExtension.transformations) {
                     _.pairs(dstExtension.transformations).forEach(function(pair) {
                         var name = pair[0],
-                            desc = pair[1],
-                            operation;
-                        operation = {document: 'registerDocumentTransformation', documentNode: 'registerNodeTransformation'}[dstName];
-                        doc[operation](desc, name);
+                            desc = pair[1];
+                        doc.registerTransformation(desc, name, dstName);
                     });
                 }
             }
index 41742d9..3175ae9 100644 (file)
@@ -903,10 +903,24 @@ describe('smartxml', function() {
         });
 
         it('allows adding transformation to a DocumentNode', function() {
-            extension = {documentNode: {transformations: {
-                testTransformation: function() { return this; },
-                testTransformation2: {impl: function() { return this;}}
-            }}};
+            extension = {
+                documentNode: {
+                    transformations: {
+                        testTransformation: function() { return this; },
+                        testTransformation2: {impl: function() { return this;}}
+                    }
+                },
+                textNode: {
+                    transformations: {
+                        textTestTransformation: function() { return this; }
+                    }
+                },
+                elementNode: {
+                    transformations: {
+                        elementTestTransformation: function() { return this; }
+                    }
+                }
+            };
             
             doc.registerExtension(extension);
 
@@ -918,6 +932,12 @@ describe('smartxml', function() {
             expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
             expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
             expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
+
+            expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
+            expect(elementNode.textTestTransformation).to.be.undefined;
+        
+            expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
+            expect(textNode.elementTestTransfomation).to.be.undefined; 
         });
     });