canvas: Fix getting invalidated references to wlxmlNodes after node tag change
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 9 Dec 2013 11:31:48 +0000 (12:31 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:50 +0000 (22:32 +0100)
src/editor/modules/documentCanvas/canvas/canvas.test.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js
src/smartxml/core.js

index 3b33751..bb795ec 100644 (file)
@@ -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('<section><div>Alice</div></section>'),
+            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() {
index a98397f..ae1cee1 100644 (file)
@@ -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()) {
index 10d058f..867353d 100644 (file)
@@ -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);