smartxml: registering textNode/elementNode methods
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 6 Dec 2013 09:09:20 +0000 (10:09 +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 831a96a..ac59532 100644 (file)
@@ -493,6 +493,8 @@ var Document = function(xml) {
     this._transformationLevel = 0;
     
     this._nodeMethods = {};
+    this._textNodeMethods = {};
+    this._elementNodeMethods = {};
     this._nodeTransformations = {};
 };
 
@@ -515,14 +517,17 @@ $.extend(Document.prototype, Backbone.Events, {
                 from = node[0];
             }
         }
-        var Factory;
+        var Factory, typeMethods;
         if(from.nodeType === Node.TEXT_NODE) {
             Factory = this.TextNodeFactory;
+            typeMethods = this._textNodeMethods;
         } else if(from.nodeType === Node.ELEMENT_NODE) {
             Factory = this.ElementNodeFactory;
+            typeMethods = this._elementNodeMethods;
         }
         var toret = new Factory(from, this);
         _.extend(toret, this._nodeMethods);
+        _.extend(toret, typeMethods);
         _.extend(toret, this._nodeTransformations);
         return toret;
     },
@@ -673,12 +678,15 @@ $.extend(Document.prototype, Backbone.Events, {
         return insertion.ofNode;
     },
 
-    registerMethod: function(methodName, method) {
-        registerMethod(methodName, method, this);
-    },
-
-    registerNodeMethod: function(methodName, method) {
-        registerMethod(methodName, method, this._nodeMethods);
+    registerMethod: function(methodName, method, dstName) {
+        var doc = this;
+        var destination = {
+            document: doc,
+            documentNode: doc._nodeMethods,
+            textNode: doc._textNodeMethods,
+            elementNode: doc._elementNodeMethods
+        }[dstName];
+        registerMethod(methodName, method, destination);
     },
 
     registerDocumentTransformation: function(desc, name) {
@@ -694,16 +702,15 @@ $.extend(Document.prototype, Backbone.Events, {
         var doc = this,
             existingPropertyNames = _.values(this);
 
-        ['document', 'documentNode'].forEach(function(dstName) {
+        ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) {
             var dstExtension = extension[dstName];
             if(dstExtension) {
                 if(dstExtension.methods) {
                     _.pairs(dstExtension.methods).forEach(function(pair) {
                         var methodName = pair[0],
-                            method = pair[1],
-                            operation;
-                        operation = {document: 'registerMethod', documentNode: 'registerNodeMethod'}[dstName];
-                        doc[operation](methodName, method);
+                            method = pair[1];
+
+                        doc.registerMethod(methodName, method, dstName);
 
                     });
                 }
index 8041013..bfe06ad 100644 (file)
@@ -845,9 +845,12 @@ describe('smartxml', function() {
             expect(function() {
                 doc.testTransformation();
             }).to.throw(Error);
+            
             expect(doc.testMethod).to.be.undefined;
             expect(elementNode.testMethod).to.be.undefined;
             expect(textNode.testMethod).to.be.undefined;
+            expect(elementNode.elementTestMethod).to.be.undefined;
+            expect(textNode.textTestMethod).to.be.undefined;
         });
 
         it('allows adding method to a document', function() {
@@ -871,9 +874,23 @@ describe('smartxml', function() {
         });
 
         it('allows adding method to a DocumentNode instance', function() {
-            extension = {documentNode: {methods: {
-                testMethod: function() { return this; }    
-            }}};
+            extension = {
+                documentNode: {
+                    methods: {
+                        testMethod: function() { return this; }
+                    }
+                },
+                textNode: {
+                    methods: {
+                        textTestMethod: function() { return this; }
+                    }
+                },
+                elementNode: {
+                    methods: {
+                        elementTestMethod: function() { return this; }
+                    }
+                }
+            };
 
             doc.registerExtension(extension);
 
@@ -883,6 +900,12 @@ describe('smartxml', function() {
 
             expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
             expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
+
+            expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
+            expect(elementNode.textTestMethod).to.be.undefined;
+        
+            expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
+            expect(textNode.elementTestMethod).to.be.undefined;
         });
 
         it('allows adding transformation to a DocumentNode', function() {