Adding new document element before another one
[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
12 describe('Canvas', function() {
13     describe('basic properties', function() {
14         it('renders empty document when canvas created from empty XML', function() {
15             var c = canvas.fromXML('');
16             expect(c.doc()).to.equal(null);
17         });
18
19         it('gives access to its document root node', function() {
20             var c = canvas.fromXML('<section></section>');
21             expect(c.doc().wlxmlTag).to.equal('section');
22         });
23
24         describe('DocumentElement', function() {
25             it('knows index of its child', function() {
26                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
27                     root = c.doc(),
28                     child = root.children()[1];
29                 expect(root.childIndex(child)).to.equal(1);
30             });
31         });
32     });
33
34     describe('document representation api', function() {
35         describe('document root element', function() {
36             var c = canvas.fromXML('<section></section>');
37             it('exists', function() {
38                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
39             });
40             it('is of type DocumentNodeElement', function() {
41                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
42             });
43         });
44
45         describe('DocumentElements comparison', function() {
46             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
47                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
48                     first_div1 = c.doc().children()[0],
49                     first_div2 = c.doc().children()[0],
50                     second_div = c.doc().children()[1];
51                 expect(first_div1.sameNode(first_div1)).to.be.true;
52                 expect(first_div1.sameNode(first_div2)).to.be.true;
53                 expect(first_div1.sameNode(second_div)).to.be.false;
54             });
55         });
56
57         describe('traversing', function() {
58             it('reports element nodes', function() {
59                 var c = canvas.fromXML('<section><div></div></section>'),
60                     children = c.doc().children();
61                 expect(children.length).to.equal(1);
62                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
63
64                 c = canvas.fromXML('<section><div></div><div></div></section>'),
65                     children = c.doc().children();
66                 expect(children.length).to.equal(2);
67                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
68                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
69             });
70             
71             it('reports text nodes', function() {
72                 var c = canvas.fromXML('<section>Alice</section>'),
73                     children = c.doc().children();
74                 expect(children.length).to.equal(1);
75                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
76             });
77
78             describe('accessing parents', function() {
79                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
80                     var c = canvas.fromXML('<section><div></div></section>'),
81                         div = c.doc().children()[0];
82                     expect(div.parent().sameNode(c.doc())).to.be.true;
83                 });
84                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
85                     var c = canvas.fromXML('<section>Alice</section>'),
86                         text = c.doc().children()[0];
87                     expect(text.parent().sameNode(c.doc())).to.be.true;
88                 });
89             });
90
91             describe('free text handling', function() {
92                     it('sees free text', function() {
93                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
94                             children = c.doc().children();
95                         expect(children.length).to.equal(3);
96                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
97                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
98                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
99                     });
100             });
101             
102             describe('white characters handling', function() {
103                 it('says empty element node has no children', function() {
104                     var c = canvas.fromXML('<section></section>');
105                     expect(c.doc().children().length).to.equal(0);
106                 });
107                 it('says element node with one space has one DocumentTextElement', function() {
108                     var c = canvas.fromXML('<section> </section>');
109                     expect(c.doc().children().length).to.equal(1);
110                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
111                 });
112                 it('ignores white space surrounding block elements', function() {
113                     var c = canvas.fromXML('<section> <div></div> </section>');
114                     var children = c.doc().children();
115                     expect(children.length).to.equal(1);
116                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
117                 });
118                 it('ignores white space between block elements', function() {
119                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
120                     var children = c.doc().children();
121                     expect(children.length === 2);
122                     [0,1].forEach(function(idx) {
123                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
124                     });
125                 });
126             });
127         });
128
129         describe('manipulation api', function() {
130
131             describe('Basic Element inserting', function() {
132                 it('can put new NodeElement at the end', function() {
133                     var c = canvas.fromXML('<section></section>'),
134                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
135                         children = c.doc().children();
136
137                     expect(children.length).to.equal(1);
138                     expect(children[0].sameNode(appended));
139                 });
140
141                 it('can put new NodeElement after another NodeElement', function() {
142                     var c = canvas.fromXML('<section><div></div></section>'),
143                         div = c.doc().children()[0],
144                         added = div.after({tag: 'header', klass: 'some.class'}),
145                         children = c.doc().children();
146                     expect(children.length).to.equal(2);
147                     expect(children[1].sameNode(added));
148                 });
149
150                 it('can put new Nodeelement before another element', function() {
151                     var c = canvas.fromXML('<section><div></div></section>'),
152                         div = c.doc().children()[0],
153                         added = div.before({tag: 'header', klass: 'some.class'}),
154                         children = c.doc().children();
155                     expect(children.length).to.equal(2);
156                     expect(children[0].sameNode(added));
157                 });
158             });
159
160             describe('wrapping', function() {
161                 it('wraps DocumentNodeElement', function() {
162                     var c = canvas.fromXML('<section><div></div></section>'),
163                         div = c.doc().children()[0];
164                     
165                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
166                         parent = div.parent(),
167                         parent2 = c.doc().children()[0];
168
169                     expect(returned.sameNode(parent)).to.be.true;
170                     expect(returned.sameNode(parent2)).to.be.true;
171                 });
172                 it('wraps DocumentTextElement', function() {
173                     var c = canvas.fromXML('<section>Alice</section>'),
174                         text = c.doc().children()[0];
175                     
176                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
177                         parent = text.parent(),
178                         parent2 = c.doc().children()[0];
179
180                     expect(returned.sameNode(parent)).to.be.true;
181                     expect(returned.sameNode(parent2)).to.be.true;
182                 });
183             });
184         });
185
186     });
187 });
188
189
190 });