smartxm: emitting nodeMoved when inserting existing node into different place
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 25 Oct 2013 13:12:16 +0000 (15:12 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 25 Oct 2013 13:12:16 +0000 (15:12 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 6b02779..7e0d9b3 100644 (file)
@@ -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;
     },
 
index 4f9a72a..4c9b860 100644 (file)
@@ -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('<div><a></a><b></b></div>'),
                 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('<div><a></a><b></b></div>'),
                 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('<div><a></a><b></b></div>'),
                 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('<div><a></a><b></b></div>'),
                 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;
         });
     });