From: Aleksander Ɓukasz Date: Fri, 6 Dec 2013 09:29:19 +0000 (+0100) Subject: registering textNode/elementNode transformations X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/be657bf05cf1a986983b7e5583f32db116f9ebe9?hp=f4828b331965f0eae8508750aea836d8aa81a8cd registering textNode/elementNode transformations --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index ac59532..dbe0290 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -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); }); } } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 41742d9..3175ae9 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -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; }); });