comparing nodes
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
1 define([
2 'libs/chai',
3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
6     
7 'use strict';
8
9 var expect = chai.expect;
10
11 describe('Canvas', function() {
12     describe('basic properties', function() {
13         it('renders empty document when canvas created from empty XML', function() {
14             var c = canvas.fromXML('');
15             expect(c.doc()).to.equal(null);
16         });
17
18         it('gives access to its document root node', function() {
19             var c = canvas.fromXML('<section></section>');
20             expect(c.doc().wlxmlTag).to.equal('section');
21         });
22     });
23
24     describe('document representation api', function() {
25         describe('document root element', function() {
26             var c = canvas.fromXML('<section></section>');
27             it('exists', function() {
28                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
29             });
30             it('is of type DocumentNodeElement', function() {
31                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
32             });
33         });
34
35         describe('DocumentElements comparison', function() {
36             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
37                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
38                     first_div1 = c.doc().children()[0],
39                     first_div2 = c.doc().children()[0],
40                     second_div = c.doc().children()[1];
41                 expect(first_div1.sameNode(first_div1)).to.be.true;
42                 expect(first_div1.sameNode(first_div2)).to.be.true;
43                 expect(first_div1.sameNode(second_div)).to.be.false;
44             });
45         });
46
47         describe('traversing', function() {
48             it('reports element nodes', function() {
49                 var c = canvas.fromXML('<section><div></div></section>'),
50                     children = c.doc().children();
51                 expect(children.length).to.equal(1);
52                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
53
54                 c = canvas.fromXML('<section><div></div><div></div></section>'),
55                     children = c.doc().children();
56                 expect(children.length).to.equal(2);
57                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
58                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
59             });
60             it('reports text nodes', function() {
61                 var c = canvas.fromXML('<section>Alice</section>'),
62                     children = c.doc().children();
63                 expect(children.length).to.equal(1);
64                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
65             });
66             describe('free text handling', function() {
67                     it('sees free text', function() {
68                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
69                             children = c.doc().children();
70                         expect(children.length).to.equal(3);
71                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
72                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
73                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
74                     });
75             });
76             describe('white characters handling', function() {
77                 it('says empty element node has no children', function() {
78                     var c = canvas.fromXML('<section></section>');
79                     expect(c.doc().children().length).to.equal(0);
80                 });
81                 it('says element node with one space has one DocumentTextElement', function() {
82                     var c = canvas.fromXML('<section> </section>');
83                     expect(c.doc().children().length).to.equal(1);
84                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
85                 });
86                 it('ignores white space surrounding block elements', function() {
87                     var c = canvas.fromXML('<section> <div></div> </section>');
88                         var children = c.doc().children();
89                         expect(children.length).to.equal(1);
90                         expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
91                 });
92                 it('ignores white space between block elements', function() {
93                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
94                         var children = c.doc().children();
95                         expect(children.length === 2);
96                         [0,1].forEach(function(idx) {
97                             expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
98                         });
99                 });
100             });
101         })
102
103     });
104 });
105
106 /*describe('Canvas', function() {
107     it('can wrap selected document nodes in a list', function() {
108         var c = canvas.fromXML('\
109             <section>\
110                 <div>Alice</div>\
111                 <div>has</div>\
112                 <div>a cat</div>\
113             </section>
114         ');
115         var div_alice   = c.doc().children({tag: 'div'})[0];
116         var div_cat     = c.doc().children({tag: 'div'})[2];
117         c.doc.wrapInList({start: div_alice, end: div_cat});
118
119         expect(c.doc().children().length === 3)
120
121
122     })
123 });*/
124
125
126
127
128 });