'wlxml/wlxml',
     'wlxml/extensions/list/list'
 
-], function($, saveDialog, wlxml) {
+], function($, saveDialog, wlxml, listExtension) {
 
 'use strict';
 
     var history = sandbox.getBootstrappedData().history;
 
     var wlxmlDocument = wlxml.WLXMLDocumentFromXML(sandbox.getBootstrappedData().document);
+
+    wlxmlDocument.registerExtension(listExtension);
      
     
     function readCookie(name) {
 
     this.undoStack = [];
     this.redoStack = [];
     this._transformationLevel = 0;
+    this.transformations = new transformations.TransformationStorage();
+
 };
 
 $.extend(Document.prototype, Backbone.Events, {
 
     Document: Document,
     DocumentNode: DocumentNode,
-    ElementNode: ElementNode
+    ElementNode: ElementNode,
+    TextNode: TextNode
 };
 
 });
\ No newline at end of file
 
 
 
 
-toret.TransformationStorage = function() {};
+toret.TransformationStorage = function() {
+    this._transformations = {};
+};
 
 _.extend(toret.TransformationStorage.prototype, {
-    _transformations: {},
     
     register: function(Transformation) {
         var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []);
 
-define(function(require) {
+define(function() {
 
 'use strict';
 
+var extension = {document: {transformations: {}}, wlxmlClass: {list: {methods: {}}}};
 
-var wlxml = require('wlxml/wlxml'),
-    extension = {documentTransformations: [], classMethods: {}};
 
-
-extension.classMethods['list'] = {
+extension.wlxmlClass.list.methods = {
     itemIndex: function(listItem) {
         var toret = -1;
         this.contents('.item').some(function(item, index) {
     }
 }
 
-extension.documentTransformations.push({
-    name: 'createList',
+
+
+extension.document.transformations.createList = {
     impl: function(params) {          
         var parent = params.node1.parent(),
             parentContents = parent.contents(),
     isAllowed: function() {
         return this.args.node1.parent().sameNode(this.args.node2.parent());
     }
-});
+};
 
-extension.documentTransformations.push({
-    name: 'extractItems',
+extension.document.transformations.extractItems = {
     impl: function(params) {
         params = _.extend({}, {merge: true}, params);
         var list = params.item1.parent(),
         var parent = this.args.nodel1.parent();
         return parent.is('list') && parent.sameNode(this.args.node2.parent());
     }
-});
+};
+
+return extension;
 
-wlxml.registerExtension(extension);
 
 });
\ No newline at end of file
 
     wlxml = require('wlxml/wlxml'),
     expect = chai.expect,
     $ = require('libs/jquery'),
-    lists = require('wlxml/extensions/list/list');
+    listsExtension = require('wlxml/extensions/list/list');
 
 
 var getDocumentFromXML = function(xml, options) {
-    return wlxml.WLXMLDocumentFromXML(xml, options || {});
+    var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
+    doc.registerExtension(listsExtension);
+    return doc;
 };
 
 var removeEmptyTextNodes = function(xml) {
 
     
 'use strict';
 
-return {
-    className: 'uri',
-    attributes: {uri: {type: 'string'}}
-};
+var extenstion = {wlxmlClass: {uri: attributes: {
+    {
+        uri: {type: 'string'}
+    }
+}}}
+
+return extension;
 
 });
\ No newline at end of file
 
 };
 
 var installObject = function(instance, klass) {
-    var methods = classMethods[klass];
+    var methods = instance.document.classMethods[klass];
     if(methods) {
-        instance.object = Object.create(methods);
+        instance.object = Object.create(_.extend({
+            transform: function(name, args) {
+                // TODO: refactor with DocumentElement.transform
+                var Transformation = instance.document.classTransformations[klass].get(name),
+                    transformation;
+                if(Transformation) {
+                    transformation = new Transformation(instance.document, instance, args);
+                }
+                return instance.document.transform(transformation);
+            }
+        }, methods));
         _.keys(methods).forEach(function(key) {
             instance.object[key] = _.bind(instance.object[key], instance);
         });
 
 
 
+var WLXMLDocumentNode = function() {
+    smartxml.DocumentNode.apply(this, arguments);
+}
+WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
+
 var WLXMLDocument = function(xml, options) {
     smartxml.Document.call(this, xml);
     this.options = options;
+
+    // this.DocumentNodeFactory = function() {
+    //     WLXMLDocumentNode.apply(this, arguments);
+    // };
+
+    // this.DocumentNodeFactory.prototype = Object.create(WLXMLDocumentNode.prototype);    
+    
+    this.ElementNodeFactory = function() {
+        WLXMLElementNode.apply(this, arguments);
+    }
+    this.ElementNodeFactory.prototype = Object.create(WLXMLElementNode.prototype);
+    this.ElementNodeFactory.prototype.transformations = new transformations.TransformationStorage();
+
+    this.TextNodeFactory = function() {
+        smartxml.TextNode.apply(this, arguments);
+    }
+    this.TextNodeFactory.prototype = Object.create(smartxml.TextNode.prototype);
+    this.TextNodeFactory.prototype.transformations = new transformations.TransformationStorage();
+
+    this.classMethods = {};
+    this.classTransformations = {};
 };
 
 var formatter_prefix = '_wlxml_formatter_';
 
+
 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
 $.extend(WLXMLDocument.prototype, {
     ElementNodeFactory: WLXMLElementNode,
-
     loadXML: function(xml) {
         smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
         $(this.dom).find(':not(iframe)').addBack().contents()
                 el.replaceWith(document.createTextNode(text.transformed));
             });
         this.trigger('contentSet');
+    },
+
+    registerExtension: function(extension) {
+        //debugger;
+        var doc = this,
+            existingPropertyName = _.values(this);
+
+        [
+            {source: extension.document, target: doc},
+            {source: extension.documentNode, target: [doc.ElementNodeFactory.prototype, doc.TextNodeFactory.prototype]},
+
+        ].forEach(function(x) {
+            if(x.source && x.source.methods) {
+                existingPropertyName = _.values(x.target)
+                _.pairs(x.source.methods).forEach(function(pair) {
+                    var methodName = pair[0],
+                        method = pair[1],
+                        targets = _.isArray(x.target) ? x.target : [x.target];
+                    if(_.contains(existingPropertyName, methodName)) {
+                        throw new Error('Cannot extend XXX with method name {methodName}. Name already exists.'.replace('{methodName}', methodName));
+                    }
+                    targets.forEach(function(target) {
+                        target[methodName] = method;
+                    });
+                    
+                });   
+            }
+        });
+
+
+        var getTrans = function(desc, methodName) {
+            if(typeof desc === 'function') {
+                desc = {impl: desc};
+            }
+            if(!desc.impl) {
+                throw new Error('Got transformation description without implementation.')
+            }
+            desc.name = desc.name || methodName;
+            return desc;
+        };
+
+        if(extension.document && extension.document.transformations) {
+            _.pairs(extension.document.transformations).forEach(function(pair) {
+                var transformation = getTrans(pair[1], pair[0]);
+                doc.transformations.register(transformations.createContextTransformation(transformation));
+            });   
+        }
+
+        if(extension.documentNode && extension.documentNode.transformations) {
+            _.pairs(extension.documentNode.transformations).forEach(function(pair) {
+                var transformation = getTrans(pair[1], pair[0]);
+                
+                doc.ElementNodeFactory.prototype.transformations.register(transformations.createContextTransformation(transformation));
+                doc.TextNodeFactory.prototype.transformations.register(transformations.createContextTransformation(transformation));
+            });   
+        }
+
+        _.pairs(extension.wlxmlClass).forEach(function(pair) {
+            var className = pair[0],
+                classExtension = pair[1],
+                thisClassMethods = (doc.classMethods[className] = doc.classMethods[className] || {}),
+                thisClassTransformations = (doc.classTransformations[className] = doc.classTransformations[className] || new transformations.TransformationStorage());
+    
+            _.extend(thisClassMethods, classExtension.methods || {}); //@ warning/throw on override?
+            
+
+            _.pairs(classExtension.transformations || {}).forEach(function(pair) {
+                var transformation = getTrans(pair[1], pair[0]);
+                thisClassTransformations.register(transformations.createContextTransformation(transformation));
+            }); 
+        });
+
     }
 
 });
     },
 
     registerExtension: function(extension) {
-        extension.documentTransformations.forEach(function(method) {
-            WLXMLDocument.prototype.transformations.register(transformations.createContextTransformation(method));
-        });
+        // @@ depracated
+        if(extension.documentTransformations) {
+            extension.documentTransformations.forEach(function(method) {
+                WLXMLDocument.prototype.transformations.register(transformations.createContextTransformation(method));
+            });
+        }
 
         _.pairs(extension.classMethods).forEach(function(pair) {
             var className = pair[0],
 
 
     });
 
+    describe('Extension', function() {
+        var doc, extension, elementNode, textNode, testClassNode;
+
+        beforeEach(function() {
+            doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
+            elementNode = doc.root;
+            textNode = doc.root.contents()[0];
+            extension = {};
+            
+            console.log('A');
+            expect(function() {
+                elementNode.transform('testTransformation');
+            }).to.throw(Error);
+            console.log('B');
+            expect(function() {
+                textNode.transform('testTransformation');
+            }).to.throw(Error);
+            console.log('C');
+            expect(function() {
+                doc.transform('testTransformation');
+            }).to.throw(Error);
+            expect(doc.testMethod).to.be.undefined;
+            expect(elementNode.testMethod).to.be.undefined;
+            expect(textNode.testMethod).to.be.undefined;
+        });
+
+        it('allows adding method to a document', function() {
+            extension = {document: {methods: {
+                testMethod: function() { return this; }
+            }}};
+
+            doc.registerExtension(extension);
+            expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
+        });
+
+        it('allows adding transformation to a document', function() {
+            extension = {document: {transformations: {
+                testTransformation: function() { return this; },
+                testTransformation2: {impl: function() { return this;}}
+            }}};
+
+            doc.registerExtension(extension);
+            expect(doc.transform('testTransformation')).to.equal(doc, 'context is set to a document instance');
+            expect(doc.transform('testTransformation2')).to.equal(doc, 'context is set to a document instance');
+        });
+
+        it('allows adding method to a DocumentNode instance', function() {
+            extension = {documentNode: {methods: {
+                testMethod: function() { return this; }    
+            }}};
+
+            doc.registerExtension(extension);
+            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');
+        });
+
+        it('allows adding transformation to a DocumentNode', function() {
+            extension = {documentNode: {transformations: {
+                testTransformation: function() { return this; },
+                testTransformation2: {impl: function() { return this;}}
+            }}};
+            
+            doc.registerExtension(extension);
+
+            expect(elementNode.transform('testTransformation').sameNode(elementNode)).to.equal(true, '1');
+            expect(elementNode.transform('testTransformation2').sameNode(elementNode)).to.equal(true, '2');
+            expect(textNode.transform('testTransformation').sameNode(textNode)).to.equal(true, '3');
+            expect(textNode.transform('testTransformation2').sameNode(textNode)).to.equal(true, '4');
+        });
+
+        it('allows adding method to an ElementNode of specific class', function() {
+            extension = {wlxmlClass: {test_class: {methods: {
+                testMethod: function() { return this; }
+            }}}};
+            doc.registerExtension(extension);
+            testClassNode = doc.root.contents()[1];
+            expect(testClassNode.object.testMethod().sameNode(testClassNode)).to.equal(true, '1');
+        });
+
+        it('allows adding transformation to an ElementNode of specific class', function() {
+            extension = {wlxmlClass: {test_class: {transformations: {
+                testTransformation: function() { return this; },
+                testTransformation2: {impl: function() { return this; }}
+            }}}};
+            doc.registerExtension(extension);
+            testClassNode = doc.root.contents()[1];
+            expect(testClassNode.object.transform('testTransformation').sameNode(testClassNode)).to.equal(true, '1');
+            expect(testClassNode.object.transform('testTransformation2').sameNode(testClassNode)).to.equal(true, '1');
+        });
+
+
+    });
+
 });
 
 });
\ No newline at end of file