this._nodeTransformations.register(Transformation);
},
+ registerExtension: function(extension) {
+ //debugger;
+ var doc = this,
+ existingPropertyNames = _.values(this);
+
+ 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;
+ };
+
+ [
+ {source: extension.document, target: doc},
+ {source: extension.documentNode, target: [doc.ElementNodeFactory.prototype, doc.TextNodeFactory.prototype]},
+
+ ].forEach(function(mapping) {
+ if(mapping.source) {
+ if(mapping.source.methods) {
+ existingPropertyNames = _.values(mapping.target)
+ _.pairs(mapping.source.methods).forEach(function(pair) {
+ var methodName = pair[0],
+ method = pair[1],
+ targets = _.isArray(mapping.target) ? mapping.target : [mapping.target];
+ if(_.contains(existingPropertyNames, methodName)) {
+ throw new Error('Cannot extend {target} with method name {methodName}. Name already exists.'
+ .replace('{target}', mapping.target)
+ .replace('{methodName}', methodName)
+ );
+ }
+ targets.forEach(function(target) {
+ if(target === doc) {
+ target.registerMethod(methodName, method);
+ } else {
+ doc.registerNodeMethod(methodName, method);
+ }
+
+ });
+ });
+ }
+
+ if(mapping.source.transformations) {
+ _.pairs(mapping.source.transformations).forEach(function(pair) {
+ var transformation = getTrans(pair[1], pair[0]),
+ targets = _.isArray(mapping.target) ? mapping.target : [mapping.target];
+ targets.forEach(function(target) {
+ if(target === doc) {
+ target.registerTransformation(transformations.createContextTransformation(transformation));
+ } else {
+ doc.registerNodeTransformation(transformations.createContextTransformation(transformation));
+ }
+
+
+ });
+ });
+ }
+ }
+ });
+ },
transform: function(transformation, args) {
//console.log('transform');
});
});
+ describe('Extension API', 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 = {};
+
+ expect(function() {
+ elementNode.transform('testTransformation');
+ }).to.throw(Error);
+ expect(function() {
+ textNode.transform('testTransformation');
+ }).to.throw(Error);
+ 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);
+
+ /* refresh */
+ elementNode = doc.root;
+ textNode = doc.root.contents()[0];
+
+ 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');
+ });
+ });
+
// describe('Undo/redo', function() {
// it('does work', function() {
registerExtension: function(extension) {
//debugger;
+ smartxml.Document.prototype.registerExtension.call(this, extension);
var doc = this,
existingPropertyNames = _.values(this);
return desc;
};
- [
- {source: extension.document, target: doc},
- {source: extension.documentNode, target: [doc.ElementNodeFactory.prototype, doc.TextNodeFactory.prototype]},
-
- ].forEach(function(mapping) {
- if(mapping.source) {
- if(mapping.source.methods) {
- existingPropertyNames = _.values(mapping.target)
- _.pairs(mapping.source.methods).forEach(function(pair) {
- var methodName = pair[0],
- method = pair[1],
- targets = _.isArray(mapping.target) ? mapping.target : [mapping.target];
- if(_.contains(existingPropertyNames, methodName)) {
- throw new Error('Cannot extend {target} with method name {methodName}. Name already exists.'
- .replace('{target}', mapping.target)
- .replace('{methodName}', methodName)
- );
- }
- targets.forEach(function(target) {
- if(target === doc) {
- target.registerMethod(methodName, method);
- } else {
- doc.registerNodeMethod(methodName, method);
- }
-
- });
- });
- }
-
- if(mapping.source.transformations) {
- _.pairs(mapping.source.transformations).forEach(function(pair) {
- var transformation = getTrans(pair[1], pair[0]),
- targets = _.isArray(mapping.target) ? mapping.target : [mapping.target];
- targets.forEach(function(target) {
- if(target === doc) {
- target.registerTransformation(transformations.createContextTransformation(transformation));
- } else {
- doc.registerNodeTransformation(transformations.createContextTransformation(transformation));
- }
-
-
- });
- });
- }
- }
- });
-
_.pairs(extension.wlxmlClass).forEach(function(pair) {
var className = pair[0],
classExtension = pair[1];
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;
+
+ // spr+ a expect dotyczacy object api?
});
it('allows adding method to a document', function() {