this._textNodeMethods = {};
this._elementNodeMethods = {};
this._nodeTransformations = {};
+ this._textNodeTransformations = {};
+ this._elementNodeTransformations = {};
};
$.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;
},
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) {
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);
});
}
}
});
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);
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;
});
});