From: Aleksander Ɓukasz Date: Fri, 6 Dec 2013 13:35:20 +0000 (+0100) Subject: allow text/element node methods and transformations to access node and transormations... X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/4ebb1730539288b8198f6db38b0ac9d56b0e521d?ds=inline allow text/element node methods and transformations to access node and transormations on document node --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index b28f8d1..60ed6a0 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -299,8 +299,15 @@ $.extend(Document.prototype, Backbone.Events, { var toret = new Factory(from, this); _.extend(toret, this._nodeMethods); _.extend(toret, typeMethods); + _.extend(toret, this._nodeTransformations); _.extend(toret, typeTransformations); + + toret.__super__ = _.extend({}, this._nodeMethods, this._nodeTransformations); + _.keys(toret.__super__).forEach(function(key) { + toret.__super__[key] = _.bind(toret.__super__[key], toret); + }); + return toret; }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 3175ae9..683e9b6 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -939,6 +939,57 @@ describe('smartxml', function() { expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true; expect(textNode.elementTestTransfomation).to.be.undefined; }); + + it('allows text/element node methods and transformations to access node and transormations on document node', function() { + + var doc = getDocumentFromXML('
text
'); + + doc.registerExtension({ + documentNode: { + methods: { + test: function() { + return 'super'; + } + }, + transformations: { + testT: function() { + return 'super_trans'; + } + } + }, + elementNode: { + methods: { + test: function() { + return 'element_sub_' + this.__super__.test(); + } + }, + transformations: { + testT: function() { + return 'element_trans_sub_' + this.__super__.testT(); + } + } + }, + textNode: { + methods: { + test: function() { + return 'text_sub_' + this.__super__.test(); + } + }, + transformations: { + testT: function() { + return 'text_trans_sub_' + this.__super__.testT(); + } + } + } + }); + + var textNode = doc.root.contents()[0]; + + expect(doc.root.test()).to.equal('element_sub_super'); + expect(textNode.test()).to.equal('text_sub_super'); + expect(doc.root.testT()).to.equal('element_trans_sub_super_trans'); + expect(textNode.testT()).to.equal('text_trans_sub_super_trans'); + }); }); // describe('Undo/redo', function() {