From 4ebb1730539288b8198f6db38b0ac9d56b0e521d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 6 Dec 2013 14:35:20 +0100 Subject: [PATCH 1/1] allow text/element node methods and transformations to access node and transormations on document node --- src/smartxml/smartxml.js | 7 +++++ src/smartxml/smartxml.test.js | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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() { -- 2.20.1