From: Aleksander Ɓukasz Date: Mon, 9 Dec 2013 11:31:48 +0000 (+0100) Subject: canvas: Fix getting invalidated references to wlxmlNodes after node tag change X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/097ad6254d6b36c226072b0c7bdcaa5f945dc483?ds=inline canvas: Fix getting invalidated references to wlxmlNodes after node tag change --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.test.js b/src/editor/modules/documentCanvas/canvas/canvas.test.js index 3b33751..bb795ec 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.test.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.test.js @@ -88,6 +88,23 @@ describe('Listening to document changes', function() { expect(sectionChildren[0].getText()).to.equal('Alice'); expect(sectionChildren[1].getWlxmlTag()).to.equal('a'); }); + + it('Handles nodeTagChange event', function() { + + var doc = wlxml.WLXMLDocumentFromXML('
Alice
'), + c = canvas.fromXMLDocument(doc); + + doc.root.contents()[0].setTag('header'); + + var headerNode = doc.root.contents()[0], + headerElement = c.doc().children()[0]; + + expect(headerElement.getWlxmlTag()).to.equal('header', 'element ok'); + + /* Make sure we handle invalidation of reference to wlxmlNode after changing its tag */ + expect(headerNode.getData('canvasElement').sameNode(headerElement)).to.equal(true, 'node->element'); + expect(headerElement.data('wlxmlNode').sameNode(headerNode)).to.equal(true, 'element->node'); + }); }); describe('Cursor', function() { diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js index a98397f..ae1cee1 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js @@ -39,6 +39,7 @@ var handlers = { nodeTagChange: function(event) { var canvasNode = utils.findCanvasElement(event.meta.node); canvasNode.setWlxmlTag(event.meta.newTagName); + canvasNode.data('wlxmlNode', event.meta.node); }, nodeAdded: function(event, checkForExistence) { if(event.meta.node.isRoot()) { diff --git a/src/smartxml/core.js b/src/smartxml/core.js index 10d058f..867353d 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -99,6 +99,8 @@ var elementNodeTransformations = { if(this.sameNode(this.document.root)) { this.document._defineDocumentProperties(node._$); } + + /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */ this._$.replaceWith(node._$); this._setNativeNode(node._$[0]); this._$.append(myContents);