From: Aleksander Ɓukasz Date: Mon, 2 Dec 2013 21:55:50 +0000 (+0100) Subject: smartxml: sending nodeDetached events when existing node added to out of document... X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/7f617eae0ff5fae863e1fbc3b4ff527b916b98ca smartxml: sending nodeDetached events when existing node added to out of document node --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index e3c831d..021a116 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -13,9 +13,13 @@ var TEXT_NODE = Node.TEXT_NODE; var INSERTION = function(implementation) { var toret = function(node) { - var insertion = this.getNodeInsertion(node); + var insertion = this.getNodeInsertion(node), + 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}); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent); return insertion.ofNode; }; return toret; @@ -155,11 +159,16 @@ $.extend(DocumentNode.prototype, { } }, - triggerChangeEvent: function(type, metaData) { - var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); + triggerChangeEvent: function(type, metaData, origParent) { + 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))) { + event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent}); + this.document.trigger('change', event); + } }, getNodeInsertion: function(node) { diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index e045788..0b85383 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -715,6 +715,28 @@ describe('smartxml', function() { expect(event2.type).to.equal('nodeAdded'); expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta'); }); + + + ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) { + it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() { + var doc = getDocumentFromXML('
'), + a = doc.root.contents()[0], + spy = sinon.spy(); + + doc.on('change', spy); + + var newNode = doc.createDocumentNode({tagName: 'b'}), + newNodeInner = newNode.append({tagName:'c'}); + + newNodeInner[insertionMethod](a); + + var event = spy.args[0][0]; + expect(event.type).to.equal('nodeDetached'); + expect(event.meta.node.sameNode(a)); + }); + }); + + }); describe('Traversing', function() {