getElementForNode handles both text and element nodes
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.test.js
index 902cb34..f64754f 100644 (file)
@@ -4,16 +4,17 @@ define([
 'libs/sinon',
 'modules/documentCanvas/canvas/canvas',
 'modules/documentCanvas/canvas/utils',
-'wlxml/wlxml'
-], function($, chai, sinon, canvas, utils, wlxml) {
+'modules/documentCanvas/canvas/documentElement',
+'wlxml/wlxml',
+], function($, chai, sinon, canvas, utils, documentElement, wlxml) {
     
 'use strict';
 /* global describe, it, beforeEach, afterEach */
 
 var expect = chai.expect;
 
-var getCanvasFromXML = function(xml) {
-    return canvas.fromXMLDocument(getDocumentFromXML(xml));
+var getCanvasFromXML = function(xml, elements) {
+    return canvas.fromXMLDocument(getDocumentFromXML(xml), elements);
 };
 
 var getDocumentFromXML = function(xml) {
@@ -26,6 +27,17 @@ var wait = function(callback, timeout) {
 };
 
 
+describe('wtf', function() {
+    it('wtf!', function() {
+        var c = getCanvasFromXML('<section>Alice</section>'),
+            doc = c.wlxmlDocument;
+
+        var txtNode = doc.root.contents()[0];
+        txtNode.wrapWith({tagName: 'header', start: 1, end: 2});
+        expect(c.doc().children().length).to.equal(3);
+    });
+});
+
 describe('new Canvas', function() {
     it('abc', function() {
         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
@@ -33,7 +45,7 @@ describe('new Canvas', function() {
 
         expect(c.doc().children()).to.have.length(3);
         expect(c.doc().children()[0].canvas).to.equal(c);
-        expect(c.doc().children()[0].data('wlxmlNode').sameNode(doc.root));
+        expect(c.doc().children()[0].wlxmlNode.sameNode(doc.root));
     });
 });
 
@@ -58,7 +70,7 @@ describe('Handling changes to the document', function() {
             c = canvas.fromXMLDocument(doc);
 
         var header = doc.root.replaceWith({tagName: 'header'});
-        expect(c.doc().data('wlxmlNode').sameNode(header)).to.equal(true);
+        expect(c.doc().wlxmlNode.sameNode(header)).to.equal(true);
     });
 });
 
@@ -73,8 +85,8 @@ describe('Listening to document changes', function() {
         a.before(b);
         var sectionChildren = c.doc().children();
         expect(sectionChildren.length).to.equal(2);
-        expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
-        expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
+        expect(sectionChildren[0].wlxmlNode.getTagName()).to.equal('b');
+        expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
     });
 
     it('Handling text node moved', function() {
@@ -87,7 +99,7 @@ describe('Listening to document changes', function() {
         var sectionChildren = c.doc().children();
         expect(sectionChildren.length).to.equal(2);
         expect(sectionChildren[0].getText()).to.equal('Alice');
-        expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
+        expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
     });
 
     it('Handles nodeTagChange event', function() {
@@ -100,11 +112,11 @@ describe('Listening to document changes', function() {
         var headerNode = doc.root.contents()[0],
             headerElement = c.doc().children()[0];
 
-        expect(headerElement.getWlxmlTag()).to.equal('header', 'element ok');
+        expect(headerElement.wlxmlNode.getTagName()).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');
+        expect(headerElement.wlxmlNode.sameNode(headerNode)).to.equal(true, 'element->node');
     });
 
     it('Handles nodeDetached event for an empty text node', function(done) {
@@ -113,21 +125,179 @@ describe('Listening to document changes', function() {
             aTextElement;
 
         canvas.fromXMLDocument(doc);
-        aTextElement = utils.findCanvasElementInParent(aTextNode, aTextNode.parent()); // TODO: This really should be easier...
+        aTextElement = utils.getElementForNode(aTextNode);
 
         aTextElement.setText('');
 
         wait(function() {
             var parent = aTextElement.parent();
             expect(aTextElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'canvas represents this as empty node');
-            aTextElement.data('wlxmlNode').detach();
+            aTextElement.wlxmlNode.detach();
             expect(parent.children().length).to.equal(1);
-            expect(parent.children()[0].getWlxmlTag()).to.equal('span');
+            expect(parent.children()[0].wlxmlNode.getTagName()).to.equal('span');
             done();
         });
     });
 });
 
+describe('Displaying span nodes', function() {
+    it('inlines a span element with a text', function() {
+        var c = getCanvasFromXML('<section><span>Alice</span></section>'),
+            spanElement = c.doc().children()[0];
+        expect(spanElement.isBlock()).to.equal(false);
+    });
+    it('renders non-span element as a block', function() {
+        var c = getCanvasFromXML('<section><span></span></section>'),
+            element = c.doc().children()[0],
+            node = element.wlxmlNode;
+
+        expect(element.isBlock()).to.equal(false, 'initially inline');
+        node = node.setTag('div');
+        expect(node.getData('canvasElement').isBlock()).to.equal(true, 'block');
+    });
+
+    it('inlines a span element if its block content gets removed', function() {
+        var c = getCanvasFromXML('<section><span>Alice <div>has</div> a cat!</span></section>'),
+            spanElement = c.doc().children()[0],
+            divNode = spanElement.wlxmlNode.contents()[1];
+
+        expect(spanElement.isBlock()).to.equal(true, 'initially a block');
+        divNode.detach();
+        expect(spanElement.isBlock()).to.equal(false, 'inlined after removing inner block');
+        
+        spanElement.wlxmlNode.append({tagName: 'div'});
+
+        expect(spanElement.isBlock()).to.equal(true, 'block again after bringing back inner block');
+    });
+
+    it('keeps showing element as a block after changing its node tag to span if it contains elements of non-span nodes', function() {
+        var c = getCanvasFromXML('<section><div><div></div></div></section>'),
+            outerDivElement = c.doc().children()[0],
+            outerDivNode = outerDivElement.wlxmlNode;
+        outerDivNode = outerDivNode.setTag('span');
+        expect(c.doc().children()[0].isBlock()).to.equal(true);
+    });
+});
+
+
+describe('Default document changes handling', function() {
+    it('handles added node', function() {
+        var c = getCanvasFromXML('<section></section>');
+        c.wlxmlDocument.root.append({tagName:'div'});
+        expect(c.doc().children().length).to.equal(1);
+        c.wlxmlDocument.root.prepend({tagName:'div'});
+        expect(c.doc().children().length).to.equal(2);
+
+        var node = c.wlxmlDocument.root.contents()[1];
+        node.before({tagName: 'div'});
+        expect(c.doc().children().length).to.equal(3);
+        node.after({tagName: 'div'});
+        expect(c.doc().children().length).to.equal(4);
+    });
+
+    it('handles attribute value change for a class attribute', function() {
+        var c = getCanvasFromXML('<section></section>');
+        c.wlxmlDocument.root.setAttr('class', 'test');
+        expect(c.doc().wlxmlNode.getClass()).to.equal('test');
+    });
+
+    it('handles detached node', function() {
+        var c = getCanvasFromXML('<section><div></div></section>');
+        c.wlxmlDocument.root.contents()[0].detach();
+        expect(c.doc().children().length).to.equal(0);
+    });
+
+    it('handles moved node', function() {
+        var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
+            a = doc.root.contents()[0],
+            b = doc.root.contents()[1],
+            c = canvas.fromXMLDocument(doc);
+
+        a.before(b);
+        var sectionChildren = c.doc().children();
+        expect(sectionChildren.length).to.equal(2);
+        expect(sectionChildren[0].wlxmlNode.getTagName()).to.equal('b');
+        expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
+    });
+
+    it('handles moving text node to another parent', function() {
+        var c = getCanvasFromXML('<section>Alice<div><span>has</span></div>a cat.</section>'),
+            doc = c.wlxmlDocument,
+            text = doc.root.contents()[0],
+            div = doc.root.contents()[1];
+        
+        div.append(text);
+        
+        var sectionChildren = c.doc().children();
+        expect(sectionChildren.length).to.equal(2);
+        expect(sectionChildren[0].wlxmlNode.sameNode(div)).to.equal(true);
+        expect(sectionChildren[1].getText()).to.equal('a cat.');
+
+        expect(div.contents().length).to.equal(2);
+        expect(div.contents()[0].getTagName()).to.equal('span');
+        expect(div.contents()[1].getText()).to.equal('Alice');
+    });
+
+    it('handles change in a text node', function() {
+        var c = getCanvasFromXML('<section>Alice</section>');
+        c.wlxmlDocument.root.contents()[0].setText('cat');
+        expect(c.doc().children()[0].getText()).to.equal('cat');
+    });
+});
+    
+describe('Custom elements based on wlxml class attribute', function() {
+    it('allows custom rendering', function() {
+        var prototype = $.extend({}, documentElement.DocumentNodeElement.prototype, {
+                init: function() {
+                    this._container().append('<test></test>');
+                }
+            }),
+            c = getCanvasFromXML('<section><div class="testClass"></div></section>', [
+            {tag: 'div', klass: 'testClass', prototype: prototype}
+        ]);
+
+        expect(c.doc().children()[0]._container().children('test').length).to.equal(1); // @!
+    });
+
+    it('allows handling changes to internal structure of rendered node', function() {
+        var prototype = $.extend({}, documentElement.DocumentNodeElement.prototype, {
+                init: function() {
+                    this.header = $('<h1>');
+                    this._container().append(this.header);
+                    this.refresh2();
+                },
+                refresh2: function() {
+                    this.header.text(this.wlxmlNode.contents().length);
+                },
+                onNodeAdded: function(event) {
+                    void(event);
+                    this.refresh2();
+                }
+        });
+
+        var c = getCanvasFromXML('<section><div class="testClass"><a></a></div></section>', [
+            {tag: 'div', klass: 'testClass', prototype: prototype}
+        ]);
+
+        var node = c.wlxmlDocument.root.contents()[0],
+            element = node.getData('canvasElement');
+
+        var header = element.dom.find('h1');
+        expect(header.text()).to.equal('1', 'just <a>');
+
+        node.append({tagName: 'div'});
+
+        expect(header.text()).to.equal('2', 'added div');
+    });
+
+    describe('Handling unknown class', function() {
+        it('Inherits default behavior', function() {
+            var c = getCanvasFromXML('<section><div class="unknown">Hi!</div></section>');
+            expect(c.doc().children()[0].children()[0].getText()).to.equal('Hi!');
+        });
+    });
+});
+
 describe('Cursor', function() {
     /* globals Node */
     var getSelection;
@@ -153,7 +323,7 @@ describe('Cursor', function() {
 
     it('returns position when browser selection collapsed', function() {
         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
-            dom = c.doc().dom(),
+            dom = c.doc().dom,
             text = findTextNode(dom, 'Alice has a cat');
 
         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
@@ -187,7 +357,7 @@ describe('Cursor', function() {
 
     it('recognizes selection start and end on document order', function() {
         var c = getCanvasFromXML('<section><span>Alice</span><span>has a cat</span><div>abc<span>...</span>cde</div></section>'),
-            dom = c.doc().dom(),
+            dom = c.doc().dom,
             textFirst = findTextNode(dom, 'Alice'),
             textSecond = findTextNode(dom, 'has a cat'),
             textAbc = findTextNode(dom, 'abc'),
@@ -288,7 +458,7 @@ describe('Cursor', function() {
 
     it('returns boundries of selection when browser selection not collapsed', function() {
         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
-            dom = c.doc().dom(),
+            dom = c.doc().dom,
             text = {
                 alice: findTextNode(dom, 'Alice '),
                 has: findTextNode(dom, 'has'),
@@ -320,7 +490,7 @@ describe('Cursor', function() {
 
     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
-            dom = c.doc().dom(),
+            dom = c.doc().dom,
             text = {
                 alice: findTextNode(dom, 'Alice '),
                 has: findTextNode(dom, 'has'),