editor: new rules for backspace/delete
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.test.js
index 5c36f90..346a82b 100644 (file)
@@ -1,22 +1,28 @@
 define([
+'libs/jquery',
 'libs/chai',
+'libs/sinon',
 'modules/documentCanvas/canvas/canvas',
-'modules/documentCanvas/canvas/documentElement',
 'modules/documentCanvas/canvas/utils',
 'wlxml/wlxml'
-], function(chai, canvas, documentElement, utils, wlxml) {
+], function($, chai, sinon, canvas, utils, wlxml) {
     
 'use strict';
+/* global describe, it, beforeEach, afterEach */
 
 var expect = chai.expect;
 
+var getCanvasFromXML = function(xml) {
+    return canvas.fromXMLDocument(getDocumentFromXML(xml));
+};
 
-var nodeFromXML = function(xml) {
-    return wlxml.WLXMLElementNodeFromXML(xml);
+var getDocumentFromXML = function(xml) {
+    return wlxml.WLXMLDocumentFromXML(xml);
 };
 
-var getCanvasFromXML = function(xml) {
-    return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
+var wait = function(callback, timeout) {
+    /* globals window */
+    return window.setTimeout(callback, timeout || 0.5);
 };
 
 
@@ -25,24 +31,119 @@ describe('new Canvas', function() {
         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
             c = canvas.fromXMLDocument(doc);
 
-        expect(c.doc().children()).to.have.length(3)
+        expect(c.doc().children()).to.have.length(3);
+        expect(c.doc().children()[0].canvas).to.equal(c);
+        expect(c.doc().children()[0].wlxmlNode.sameNode(doc.root));
     });
-})
+});
 
-describe('Cursor', function() {
+describe('Handling empty text nodes', function() {
+    it('puts zero width space into node with about to be remove text', function(done) {
+        var c = getCanvasFromXML('<section>Alice</section>'),
+            textElement = c.doc().children()[0];
+        textElement.setText('');
+
+        /* Wait for MutationObserver to kick in. */
+        wait(function() {
+            expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
+            expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
+            done();
+        });
+    });
+});
+
+describe('Handling changes to the document', function() {
+    it('replaces the whole canvas content when document root node replaced', function() {
+        var doc = getDocumentFromXML('<section></section>'),
+            c = canvas.fromXMLDocument(doc);
+
+        var header = doc.root.replaceWith({tagName: 'header'});
+        expect(c.doc().wlxmlNode.sameNode(header)).to.equal(true);
+    });
+});
+
+describe('Listening to document changes', function() {
 
+    it('Handling element node moved', 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].getWlxmlTag()).to.equal('b');
+        expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
+    });
+
+    it('Handling text node moved', function() {
+        var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
+            a = doc.root.contents()[0],
+            textNode = doc.root.contents()[1],
+            c = canvas.fromXMLDocument(doc);
+
+        a.before(textNode);
+        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');
+    });
+
+    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.wlxmlNode.sameNode(headerNode)).to.equal(true, 'element->node');
+    });
+
+    it('Handles nodeDetached event for an empty text node', function(done) {
+        var doc = wlxml.WLXMLDocumentFromXML('<section><div>A<span>b</span></div></section>'),
+            aTextNode = doc.root.contents()[0].contents()[0],
+            aTextElement;
+
+        canvas.fromXMLDocument(doc);
+        aTextElement = utils.findCanvasElementInParent(aTextNode, aTextNode.parent()); // TODO: This really should be easier...
+
+        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.wlxmlNode.detach();
+            expect(parent.children().length).to.equal(1);
+            expect(parent.children()[0].getWlxmlTag()).to.equal('span');
+            done();
+        });
+    });
+});
+
+describe('Cursor', function() {
+    /* globals Node */
     var getSelection;
 
     var findTextNode = function(inside, text) {
         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
             return this.nodeType === Node.TEXT_NODE && this.data === text;
         });
-        if(nodes.length)
+        if(nodes.length) {
             return nodes[0];
+        }
         return null;
-    }
+    };
 
     beforeEach(function() {
+        /* globals window */
         getSelection = sinon.stub(window, 'getSelection');
     });
 
@@ -84,6 +185,107 @@ describe('Cursor', function() {
         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
     });
 
+    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(),
+            textFirst = findTextNode(dom, 'Alice'),
+            textSecond = findTextNode(dom, 'has a cat'),
+            textAbc = findTextNode(dom, 'abc'),
+            textCde = findTextNode(dom, 'cde');
+
+        var check = function(label, expected) {
+            var cursor = c.getCursor();
+            label = label + ': ';
+            expect(cursor.getSelectionStart().element.getText()).to.equal(expected.start.text, label + 'start element ok');
+            expect(cursor.getSelectionStart().offset).to.equal(expected.start.offset, label + 'start offset ok');
+            expect(cursor.getSelectionEnd().element.getText()).to.equal(expected.end.text, label + 'end element ok');
+            expect(cursor.getSelectionEnd().offset).to.equal(expected.end.offset, label + 'end offset ok');
+        };
+
+        getSelection.returns({
+            anchorNode: textFirst,
+            focusNode: textFirst,
+            anchorOffset: 1,
+            focusOffset: 3,
+            isCollapsed: false
+        });
+
+        check('same element, anchor first', {
+            start: {text: 'Alice', offset: 1},
+            end: {text: 'Alice', offset:3}
+        });
+
+
+        getSelection.returns({
+            anchorNode: textFirst,
+            focusNode: textFirst,
+            anchorOffset: 3,
+            focusOffset: 1,
+            isCollapsed: false
+        });
+
+        check('same element, anchor second', {
+            start: {text: 'Alice', offset: 1},
+            end: {text: 'Alice', offset:3}
+        });
+
+
+        getSelection.returns({
+            anchorNode: textAbc,
+            focusNode: textCde,
+            anchorOffset: 3,
+            focusOffset: 1,
+            isCollapsed: false
+        });
+
+        check('same parent, anchor first', {
+            start: {text: 'abc', offset: 3},
+            end: {text: 'cde', offset:1}
+        });
+
+
+        getSelection.returns({
+            anchorNode: textCde,
+            focusNode: textAbc,
+            anchorOffset: 1,
+            focusOffset: 3,
+            isCollapsed: false
+        });
+
+        check('same parent, anchor second', {
+            start: {text: 'abc', offset: 3},
+            end: {text: 'cde', offset:1}
+        });
+
+
+        getSelection.returns({
+            anchorNode: textFirst,
+            focusNode: textSecond,
+            anchorOffset: 1,
+            focusOffset: 3,
+            isCollapsed: false
+        });
+
+        check('different parents, anchor first', {
+            start: {text: 'Alice', offset: 1},
+            end: {text: 'has a cat', offset:3}
+        });
+
+
+        getSelection.returns({
+            anchorNode: textSecond,
+            focusNode: textFirst,
+            anchorOffset: 3,
+            focusOffset: 1,
+            isCollapsed: false
+        });
+
+        check('different parents, anchor second', {
+            start: {text: 'Alice', offset: 1},
+            end: {text: 'has a cat', offset:3}
+        });
+    });
+
     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(),