From ea6df9066799de1d2bfda2132ad06c2d8f57582e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 25 Oct 2013 15:12:16 +0200 Subject: [PATCH] smartxm: emitting nodeMoved when inserting existing node into different place --- src/smartxml/smartxml.js | 16 ++++---------- src/smartxml/smartxml.test.js | 41 +++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 6b02779..7e0d9b3 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -42,18 +42,14 @@ $.extend(DocumentNode.prototype, { after: function(node) { var insertion = this.getNodeInsertion(node); this._$.after(insertion.ofNode.nativeNode); - if(insertion.insertsNew) { - this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode}); - } + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); return insertion.ofNode; }, before: function(node) { var insertion = this.getNodeInsertion(node); this._$.before(insertion.ofNode.nativeNode); - if(insertion.insertsNew) { - this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode}); - } + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); return insertion.ofNode; }, @@ -173,18 +169,14 @@ $.extend(ElementNode.prototype, { append: function(node) { var insertion = this.getNodeInsertion(node); this._$.append(insertion.ofNode.nativeNode); - if(insertion.insertsNew) { - this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode}); - } + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); return insertion.ofNode; }, prepend: function(node) { var insertion = this.getNodeInsertion(node); this._$.prepend(insertion.ofNode.nativeNode); - if(insertion.insertsNew) { - this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode}); - } + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); return insertion.ofNode; }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 4f9a72a..4c9b860 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -380,14 +380,19 @@ describe('smartxml', function() { expect(event.meta.node.sameNode(appended)).to.be.true; }); - it('doesn\'t emit nodeAdded when appending aready existing node', function() { + it('emits nodeMoved when appending aready existing node', function() { var node = elementNodeFromXML('
'), a = node.contents()[0], b = node.contents()[1], spy = sinon.spy(); node.document.on('change', spy); - a.append(b); - expect(spy.callCount).to.equal(0); + + var appended = a.append(b), + event = spy.args[0][0]; + + expect(spy.callCount).to.equal(1); + expect(event.type).to.equal('nodeMoved'); + expect(event.meta.node.sameNode(appended)).to.be.true; }); it('emits nodeAdded event when prepending new node', function() { @@ -401,14 +406,18 @@ describe('smartxml', function() { expect(event.meta.node.sameNode(prepended)).to.be.true; }); - it('doesn\'t emit nodeAdded when prepending aready existing node', function() { + it('emits nodeMoved when prepending aready existing node', function() { var node = elementNodeFromXML('
'), a = node.contents()[0], b = node.contents()[1], spy = sinon.spy(); node.document.on('change', spy); - a.prepend(b); - expect(spy.callCount).to.equal(0); + + var prepended = a.prepend(b), + event = spy.args[0][0]; + expect(spy.callCount).to.equal(1); + expect(event.type).to.equal('nodeMoved'); + expect(event.meta.node.sameNode(prepended)).to.be.true; }); it('emits nodeAdded event when inserting node after another', function() { @@ -422,14 +431,18 @@ describe('smartxml', function() { expect(event.meta.node.sameNode(inserted)).to.be.true; }); - it('doesn\'t emit nodeAdded when inserting aready existing node after another', function() { + it('emits nodeMoved when inserting aready existing node after another', function() { var node = elementNodeFromXML('
'), a = node.contents()[0], b = node.contents()[1], spy = sinon.spy(); node.document.on('change', spy); - b.after(a); - expect(spy.callCount).to.equal(0); + var inserted = b.after(a), + event = spy.args[0][0]; + + expect(spy.callCount).to.equal(1); + expect(event.type).to.equal('nodeMoved'); + expect(event.meta.node.sameNode(inserted)).to.be.true; }); it('emits nodeAdded event when inserting node before another', function() { @@ -443,14 +456,18 @@ describe('smartxml', function() { expect(event.meta.node.sameNode(inserted)).to.be.true; }); - it('doesn\'t emit nodeAdded when inserting aready existing node before another', function() { + it('emits nodeAdded when inserting aready existing node before another', function() { var node = elementNodeFromXML('
'), a = node.contents()[0], b = node.contents()[1], spy = sinon.spy(); node.document.on('change', spy); - a.before(b); - expect(spy.callCount).to.equal(0); + var inserted = a.before(b), + event = spy.args[0][0]; + + expect(spy.callCount).to.equal(1); + expect(event.type).to.equal('nodeMoved'); + expect(event.meta.node.sameNode(inserted)).to.be.true; }); }); -- 2.20.1