From cee08f0b14be951a31544ae708b2a92f6255e61a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 5 Dec 2013 16:12:23 +0100 Subject: [PATCH] moving smartxml transformations into object properties name space --- src/smartxml/smartxml.js | 36 ++++++++++++-------------- src/smartxml/smartxml.test.js | 18 ++++++++----- src/smartxml/transformations.js | 5 ++-- src/wlxml/extensions/list/list.js | 2 +- src/wlxml/extensions/list/list.test.js | 22 ++++++++-------- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d912904..831a96a 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -38,12 +38,8 @@ var DocumentNode = function(nativeNode, document) { $.extend(DocumentNode.prototype, { - transform: function(name, args) { - var Transformation = this.transformations.get(name), - transformation; - if(Transformation) { - transformation = new Transformation(this.document, this, args); - } + transform: function(Transformation, args) { + var transformation = new Transformation(this.document, this, args); return this.document.transform(transformation); }, @@ -473,7 +469,10 @@ var parseXML = function(xml) { var registerTransformation = function(desc, name, target) { var Transformation = transformations.createContextTransformation(desc, name); - target.register(Transformation); + target[name] = function(args) { + var instance = this; + return instance.transform(Transformation, args); + } }; var registerMethod = function(methodName, method, target) { @@ -492,10 +491,9 @@ var Document = function(xml) { this.undoStack = []; this.redoStack = []; this._transformationLevel = 0; - this.transformations = new transformations.TransformationStorage(); this._nodeMethods = {}; - this._nodeTransformations = new transformations.TransformationStorage(); + this._nodeTransformations = {}; }; $.extend(Document.prototype, Backbone.Events, { @@ -525,7 +523,7 @@ $.extend(Document.prototype, Backbone.Events, { } var toret = new Factory(from, this); _.extend(toret, this._nodeMethods); - toret.transformations = this._nodeTransformations; + _.extend(toret, this._nodeTransformations); return toret; }, @@ -684,7 +682,7 @@ $.extend(Document.prototype, Backbone.Events, { }, registerDocumentTransformation: function(desc, name) { - registerTransformation(desc, name, this.transformations); + registerTransformation(desc, name, this); }, registerNodeTransformation: function(desc, name) { @@ -723,15 +721,15 @@ $.extend(Document.prototype, Backbone.Events, { }); }, - transform: function(transformation, args) { + transform: function(Transformation, args) { //console.log('transform'); - var Transformation, toret; - if(typeof transformation === 'string') { - Transformation = this.transformations.get(transformation); - if(Transformation) { - transformation = new Transformation(this, this, args); - } - } + var toret, transformation; + + if(typeof Transformation === 'function') { + transformation = new Transformation(this, this, args); + } else { + transformation = Transformation; + } if(transformation) { this._transformationLevel++; toret = transformation.run(); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index a15f833..11118f4 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -843,7 +843,7 @@ describe('smartxml', function() { textNode.transform('testTransformation'); }).to.throw(Error); expect(function() { - doc.transform('testTransformation'); + doc.testTransformation(); }).to.throw(Error); expect(doc.testMethod).to.be.undefined; expect(elementNode.testMethod).to.be.undefined; @@ -866,8 +866,8 @@ describe('smartxml', function() { }}}; 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'); + expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance'); + expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance'); }); it('allows adding method to a DocumentNode instance', function() { @@ -893,10 +893,14 @@ describe('smartxml', function() { 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'); + /* refresh */ + elementNode = doc.root; + textNode = doc.root.contents()[0]; + + expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1'); + 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'); }); }); diff --git a/src/smartxml/transformations.js b/src/smartxml/transformations.js index 772cf75..eaa292c 100644 --- a/src/smartxml/transformations.js +++ b/src/smartxml/transformations.js @@ -12,12 +12,11 @@ var getTransDesc = function(desc, name) { if(!desc.impl) { throw new Error('Got transformation description without implementation.') } - desc.name = desc.name || name; return desc; }; toret.createGenericTransformation = function(desc, name) { - desc = getTransDesc(desc, name); + desc = getTransDesc(desc); var GenericTransformation = function(document, args) { this.args = args || {}; @@ -48,7 +47,7 @@ toret.createGenericTransformation = function(desc, name) { } }; _.extend(GenericTransformation.prototype, { - name: desc.name, + name: name, run: function() { var changeRoot; if(!desc.undo) { diff --git a/src/wlxml/extensions/list/list.js b/src/wlxml/extensions/list/list.js index 75b5336..6cac91b 100644 --- a/src/wlxml/extensions/list/list.js +++ b/src/wlxml/extensions/list/list.js @@ -135,7 +135,7 @@ extension.document.transformations.extractItems = { reference.after(toAdd); } if(!params.merge && listIsNested) { - return this.transform('extractItems', {item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]}); + return this.extractItems({item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]}); } return true; }, diff --git a/src/wlxml/extensions/list/list.test.js b/src/wlxml/extensions/list/list.test.js index 10a6e91..faa85da 100644 --- a/src/wlxml/extensions/list/list.test.js +++ b/src/wlxml/extensions/list/list.test.js @@ -39,7 +39,7 @@ describe('Lists extension', function() { div1 = section.contents()[1], textA = section.contents()[2]; - doc.transform('createList', {node1: div1, node2: textA}); + doc.createList({node1: div1, node2: textA}); expect(section.contents().length).to.equal(3, 'section has three child nodes'); @@ -72,7 +72,7 @@ describe('Lists extension', function() { itemC = outerList.contents('.item')[2]; - doc.transform('createList', {node1: itemB, node2: itemC}); + doc.createList({node1: itemB, node2: itemC}); var outerListItems = outerList.contents('.item'), innerList = outerListItems[1].contents()[0]; @@ -108,7 +108,7 @@ describe('Lists extension', function() { item1 = list.contents()[1], item2 = list.contents()[2]; - doc.transform('extractItems', {item1: item1, item2: item2}); + doc.extractItems({item1: item1, item2: item2}); var section = doc.root, list1 = section.contents()[0], @@ -147,7 +147,7 @@ describe('Lists extension', function() { item2 = list.contents()[1], item3 = list.contents()[2]; - doc.transform('extractItems', {item1: item1, item2: item2}); + doc.extractItems({item1: item1, item2: item2}); var section = doc.root, oldItem1 = section.contents()[0], @@ -175,7 +175,7 @@ describe('Lists extension', function() { item2 = list.contents()[1], item3 = list.contents()[2]; - doc.transform('extractItems', {item1: item2, item2: item3}); + doc.extractItems({item1: item2, item2: item3}); var section = doc.root, oldItem1 = section.contents()[1], @@ -201,7 +201,7 @@ describe('Lists extension', function() { item1 = list.contents()[0], item2 = list.contents()[1]; - doc.transform('extractItems', {item1: item1, item2: item2}); + doc.extractItems({item1: item1, item2: item2}); var section = doc.root, oldItem1 = section.contents()[0], @@ -231,7 +231,7 @@ describe('Lists extension', function() { nestedList = list.contents()[1].contents()[0], nestedListItem = nestedList.contents()[1]; - doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem}); + doc.extractItems({item1: nestedListItem, item2: nestedListItem}); //@@ name! var section = doc.root, list = section.contents()[0], @@ -280,7 +280,7 @@ describe('Lists extension', function() { nestedListItem1 = nestedList.contents()[1], nestedListItem2 = nestedList.contents()[2]; - doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2}); + doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); var section = doc.root, list = section.contents()[0], @@ -321,7 +321,7 @@ describe('Lists extension', function() { nestedListItem1 = nestedList.contents()[0], nestedListItem2 = nestedList.contents()[1]; - doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2}); + doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); var section = doc.root, list = section.contents()[0], @@ -362,7 +362,7 @@ describe('Lists extension', function() { nestedListItem1 = nestedList.contents()[0], nestedListItem2 = nestedList.contents()[1]; - doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2}); + doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); var section = doc.root, list = section.contents()[0], @@ -397,7 +397,7 @@ describe('Lists extension', function() { nestedList = list.contents()[1].contents()[0], nestedListItem = nestedList.contents()[0]; - var test = doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem, merge: false}); + var test = doc.extractItems({item1: nestedListItem, item2: nestedListItem, merge: false}); expect(test).to.equal(true, 'extraction status ok'); -- 2.20.1