smartxml: fix - do not send nodeDetached event for out of document node
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 3 Dec 2013 22:41:47 +0000 (23:41 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:47 +0000 (22:32 +0100)
This fixes the case where new node, which wasn't inserted into document
tree yet was moved (via append/prepend/before/after) into another
out of document node which was triggering nodeDetached event.

src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 4a0cd7a..8110a97 100644 (file)
@@ -15,12 +15,13 @@ var TEXT_NODE = Node.TEXT_NODE;
 var INSERTION = function(implementation) {
     var toret = function(node) {
         var insertion = this.getNodeInsertion(node),
 var INSERTION = function(implementation) {
     var toret = function(node) {
         var insertion = this.getNodeInsertion(node),
+            nodeWasContained = this.document.containsNode(insertion.ofNode),
             nodeParent;
         if(!(this.document.containsNode(this))) {
             nodeParent = insertion.ofNode.parent();
         }
         implementation.call(this, insertion.ofNode.nativeNode);
             nodeParent;
         if(!(this.document.containsNode(this))) {
             nodeParent = insertion.ofNode.parent();
         }
         implementation.call(this, insertion.ofNode.nativeNode);
-        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
         return insertion.ofNode;
     };
     return toret;
         return insertion.ofNode;
     };
     return toret;
@@ -180,13 +181,13 @@ $.extend(DocumentNode.prototype, {
         }
     },
 
         }
     },
 
-    triggerChangeEvent: function(type, metaData, origParent) {
+    triggerChangeEvent: function(type, metaData, origParent, nodeWasContained) {
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
             this.document.trigger('change', event);
         }
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
             this.document.trigger('change', event);
         }
-        if((type === 'nodeAdded' || type === 'nodeMoved') && !(this.document.containsNode(this))) {
+        if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) {
              event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent});
              this.document.trigger('change', event);
         }
              event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent});
              this.document.trigger('change', event);
         }
index 8d0ca4b..c7cb6ad 100644 (file)
@@ -734,6 +734,19 @@ describe('smartxml', function() {
                 expect(event.type).to.equal('nodeDetached');
                 expect(event.meta.node.sameNode(a));
             });
                 expect(event.type).to.equal('nodeDetached');
                 expect(event.meta.node.sameNode(a));
             });
+
+            it('doesn\'t emit nodeDetached event for already out of document moved to out of document node: ' + insertionMethod, function() {
+                var doc = getDocumentFromXML('<div><a></a></div>'),
+                    a = doc.root.contents()[0],
+                    spy = sinon.spy();
+
+                doc.on('change', spy);
+
+                var newNode = doc.createDocumentNode({tagName: 'b'});
+                    var newNodeInner = newNode.append({tagName:'c'});
+
+                expect(spy.callCount).to.equal(0);
+            });
         });
 
 
         });