f15d75b5b0d36452b690d465a97929eda97277fc
[fnpeditor.git] / src / wlxml / extensions / list / list.test.js
1 define(function(require) {
2     
3 'use strict';
4
5 var chai = require('libs/chai'),
6     wlxml = require('wlxml/wlxml'),
7     expect = chai.expect,
8     $ = require('libs/jquery'),
9     lists = require('wlxml/extensions/list/list');
10
11
12 var getDocumentFromXML = function(xml, options) {
13     return wlxml.WLXMLDocumentFromXML(xml, options || {});
14 };
15
16 var removeEmptyTextNodes = function(xml) {
17     xml = $($.trim(xml));
18     xml.find(':not(iframe)')
19         .addBack()
20         .contents()
21         .filter(function() {return this.nodeType === Node.TEXT_NODE;})
22         .each(function() {
23             if(!this.data.length) {
24                 $(this).remove();
25             }
26         });
27     return $('<w>').append(xml).html();
28 };
29
30
31 describe.only('Lists extension', function() {
32
33     describe('creating lists', function() {
34         it('allows creation of a list from existing sibling DocumentElements', function() {
35             var doc = getDocumentFromXML('<section>Alice<div>has</div>a<div>cat</div></section>'),
36                 section = doc.root,
37                 div1 = section.contents()[1],
38                 textA = section.contents()[2];
39             
40             doc.transform('createList', {node1: div1, node2: textA});
41
42             expect(section.contents().length).to.equal(3, 'section has three child nodes');
43
44             var child1 = section.contents()[0],
45                 list = section.contents()[1],
46                 child3 = section.contents()[2];
47
48             expect(child1.getText()).to.equal('Alice');
49             expect(list.is('list')).to.equal(true, 'second child is a list');
50             expect(list.contents().length).to.equal(2, 'list contains two elements');
51             list.contents().forEach(function(child) {
52                  expect(child.getClass()).to.equal('item', 'list childs have wlxml class of item');
53             });
54             expect(child3.contents()[0].getText()).to.equal('cat');
55         });
56
57         it('allows creating nested list from existing sibling list items', function() {
58             var doc = getDocumentFromXML('\
59                 <section>\
60                     <div class="list">\
61                         <div class="item">A</div>\
62                         <div class="item">B</div>\
63                         <div class="item">C</div>\
64                         <div class="item">D</div>\
65                     </div>\
66                 </section>');
67                 
68                 var outerList = doc.root.contents('.list')[0],
69                 itemB = outerList.contents('.item')[1],
70                 itemC = outerList.contents('.item')[2];
71
72             
73             doc.transform('createList', {node1: itemB, node2: itemC});
74
75             var outerListItems = outerList.contents('.item'),
76                 innerList = outerListItems[1].contents()[0];
77             
78             var innerListItems = innerList.contents('.item');
79
80             expect(outerListItems.length).to.equal(3, 'outer list has three items');
81             expect(outerListItems[0].contents()[0].getText()).to.equal('A', 'first outer item ok');
82             expect(outerListItems[1].getClass()).to.equal('item', 'inner list is wrapped by item element');
83
84             expect(innerList.is('list')).to.equal(true, 'inner list created');
85             expect(innerListItems.length).to.equal(2, 'inner list has two items');
86             expect(innerListItems[0].contents()[0].getText()).to.equal('B', 'first inner item ok');
87             expect(innerListItems[1].contents()[0].getText()).to.equal('C', 'second inner item ok');
88
89             expect(outerListItems[2].contents()[0].getText()).to.equal('D', 'last outer item ok');
90
91         });
92     });
93
94     describe('extracting list items', function() {
95         it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
96             var doc = getDocumentFromXML(removeEmptyTextNodes('\
97                 <section>\
98                     <div class="list">\
99                         <div class="item">0</div>\
100                         <div class="item">1</div>\
101                         <div class="item">2</div>\
102                         <div class="item">3</div>\
103                     </div>\
104                 </section>')),
105                 list = doc.root.contents()[0],
106                 item1 = list.contents()[1],
107                 item2 = list.contents()[2];
108
109             doc.transform('extractItems', {item1: item1, item2: item2});
110
111             var section = doc.root,
112                 list1 = section.contents()[0],
113                 oldItem1 = section.contents()[1],
114                 oldItem2 = section.contents()[2],
115                 list2 = section.contents()[3];
116
117             expect(section.contents().length).to.equal(4, 'section contains two old items and two lists');
118             
119             expect(list1.is('list')).to.equal(true, 'first section child is a list');
120             expect(list1.contents().length).to.equal(1, 'first list has one child');
121             expect(list1.contents()[0].contents()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
122
123             expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item got extracted');
124             expect(oldItem1.getClass() === '').to.equal(true, 'first extracted element has no wlxml class');
125
126             expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item got extracted');
127             expect(oldItem2.getClass() === '').to.equal(true, 'second extracted element has no wlxml class');
128
129             expect(list2.is('list')).to.equal(true, 'last section child is a list');
130             expect(list2.contents().length).to.equal(1, 'second list has one child');
131             expect(list2.contents()[0].contents()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
132         });
133
134         it('puts extracted items above the list if starting item is the first one', function() {
135             var doc = getDocumentFromXML(removeEmptyTextNodes('\
136                 <section>\
137                     <div class="list">\
138                         <div class="item">0</div>\
139                         <div class="item">1</div>\
140                         <div class="item">2</div>\
141                     </div>\
142                 </section>')),
143                 list = doc.root.contents()[0],
144                 item1 = list.contents()[0],
145                 item2 = list.contents()[1],
146                 item3 = list.contents()[2];
147
148             doc.transform('extractItems', {item1: item1, item2: item2});
149
150             var section = doc.root,
151                 oldItem1 = section.contents()[0],
152                 oldItem2 = section.contents()[1],
153                 newList = section.contents()[2];
154
155             expect(section.contents().length).to.equal(3, 'section has three children');
156             expect(oldItem1.contents()[0].getText()).to.equal('0', 'first item extracted');
157             expect(oldItem2.contents()[0].getText()).to.equal('1', 'second item extracted');
158             expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
159             expect(newList.contents().length).to.equal(1, 'list has now one child');
160         });
161
162         it('puts extracted items below the list if ending item is the last one', function() {
163             var doc = getDocumentFromXML(removeEmptyTextNodes('\
164                 <section>\
165                     <div class="list">\
166                         <div class="item">0</div>\
167                         <div class="item">1</div>\
168                         <div class="item">2</div>\
169                     </div>\
170                 </section>')),
171                 list = doc.root.contents()[0],
172                 item1 = list.contents()[0],
173                 item2 = list.contents()[1],
174                 item3 = list.contents()[2];
175
176             doc.transform('extractItems', {item1: item2, item2: item3});
177
178             var section = doc.root,
179                 oldItem1 = section.contents()[1],
180                 oldItem2 = section.contents()[2],
181                 newList = section.contents()[0];
182
183             expect(section.contents().length).to.equal(3, 'section has three children');
184             expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item extracted');
185             expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item extracted');
186             expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
187             expect(newList.contents().length).to.equal(1, 'list has now one child');
188         });
189
190         it('removes list if all its items are extracted', function() {
191             var doc = getDocumentFromXML(removeEmptyTextNodes('\
192                 <section>\
193                     <div class="list">\
194                         <div class="item">some item</div>\
195                         <div class="item">some item 2</div>\
196                     </div>\
197                 </section>')),
198                 list = doc.root.contents()[0],
199                 item1 = list.contents()[0],
200                 item2 = list.contents()[1];
201
202             doc.transform('extractItems', {item1: item1, item2: item2});
203
204             var section = doc.root,
205                 oldItem1 = section.contents()[0],
206                 oldItem2 = section.contents()[1];
207
208             expect(section.contents().length).to.equal(2, 'section contains two children');
209             expect(oldItem1.contents()[0].getText()).to.equal('some item');
210             expect(oldItem2.contents()[0].getText()).to.equal('some item 2');
211         });
212
213         it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
214             var doc = getDocumentFromXML(removeEmptyTextNodes('\
215                 <section>\
216                     <div class="list">\
217                         <div class="item">0</div>\
218                         <div class="item">\
219                             <div class="list.items">\
220                                 <div class="item">1.1</div>\
221                                 <div class="item">1.2</div>\
222                                 <div class="item">1.3</div>\
223                             </div>\
224                         </div>\
225                         <div class="item">2</div>\
226                     </div>\
227                 </section>')),
228                 list = doc.root.contents()[0],
229                 nestedList = list.contents()[1].contents()[0],
230                 nestedListItem = nestedList.contents()[1];
231
232             doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem});
233
234             var section = doc.root,
235                 list = section.contents()[0],
236                 item1 = list.contents()[0],
237                 item2 = list.contents()[1], //
238                 item3 = list.contents()[2],
239                 item4 = list.contents()[3], //
240                 item5 = list.contents()[4],
241                 nestedList1 = item2.contents()[0],
242                 nestedList2 = item4.contents()[0];
243
244             expect(list.contents().length).to.equal(5, 'top list has five items');
245             
246             expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
247
248             expect(item2.getClass()).to.equal('item', 'first nested list is still wrapped in item element');
249             expect(nestedList1.contents().length).to.equal(1, 'first nested list is left with one child');
250             expect(nestedList1.contents()[0].contents()[0].getText()).to.equal('1.1', 'first nested list item left alone');
251             
252             expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
253
254             expect(item4.getClass()).to.equal('item', 'second nested list is still wrapped in item element');
255             expect(nestedList2.contents().length).to.equal(1, 'second nested list is left with one child');
256             expect(nestedList2.contents()[0].contents()[0].getText()).to.equal('1.3', 'second nested list item left alone');
257
258             expect(item5.contents()[0].getText()).to.equal('2', 'last item ok');
259         });
260
261         it('puts extracted items below the list if ending item is the last one - nested case' , function() {
262             var doc = getDocumentFromXML(removeEmptyTextNodes('\
263                 <section>\
264                     <div class="list.items">\
265                         <div class="item">0</div>\
266                         <div class="item">\
267                             <div class="list.items">\
268                                 <div class="item">1.1</div>\
269                                 <div class="item">1.2</div>\
270                                 <div class="item">1.3</div>\
271                             </div>\
272                         </div>\
273                         <div class="item">2</div>\
274                     </div>\
275                 </section>')),
276                 list = doc.root.contents()[0],
277                 nestedList = list.contents()[1].contents()[0],
278                 nestedListItem1 = nestedList.contents()[1],
279                 nestedListItem2 = nestedList.contents()[2];
280
281             doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
282
283             var section = doc.root,
284                 list = section.contents()[0],
285                 item1 = list.contents()[0],
286                 item2 = list.contents()[1],
287                 item3 = list.contents()[2],
288                 item4 = list.contents()[3],
289                 item5 = list.contents()[4];
290             nestedList = item2.contents()[0];
291
292             expect(list.contents().length).to.equal(5, 'top list has five items');
293             expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
294             expect(item2.getClass()).to.equal('item', 'nested list is still wrapped in item element');
295             expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
296             expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.1', 'nested list item left alone');
297             expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
298             expect(item4.contents()[0].getText()).to.equal('1.3', 'fourth item ok');
299             expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
300         });
301
302         it('puts extracted items above the list if starting item is the first one - nested case' , function() {
303             var doc = getDocumentFromXML(removeEmptyTextNodes('\
304                 <section>\
305                     <div class="list.items">\
306                         <div class="item">0</div>\
307                         <div class="item">\
308                             <div class="list.items">\
309                                 <div class="item">1.1</div>\
310                                 <div class="item">1.2</div>\
311                                 <div class="item">1.3</div>\
312                             </div>\
313                         </div>\
314                         <div class="item">2</div>\
315                     </div>\
316                 </section>')),
317                 list = doc.root.contents()[0],
318                 nestedList = list.contents()[1].contents()[0],
319                 nestedListItem1 = nestedList.contents()[0],
320                 nestedListItem2 = nestedList.contents()[1];
321
322             doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
323
324             var section = doc.root,
325                 list = section.contents()[0],
326                 item1 = list.contents()[0],
327                 item2 = list.contents()[1],
328                 item3 = list.contents()[2],
329                 item4 = list.contents()[3],
330                 item5 = list.contents()[4];
331             nestedList = item4.contents()[0];
332
333             expect(list.contents().length).to.equal(5, 'top list has five items');
334             expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
335             expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
336             expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
337             
338             expect(item4.getClass()).to.equal('item', 'nested list is still wrapped in item element');
339             expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
340             expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.3', 'nested list item left alone');
341             expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
342         });
343
344         it('removes list if all its items are extracted - nested case', function() {
345             var doc = getDocumentFromXML(removeEmptyTextNodes('\
346                 <section>\
347                     <div class="list.items">\
348                         <div class="item">0</div>\
349                         <div class="item">\
350                             <div class="list.items">\
351                                 <div class="item">1.1</div>\
352                                 <div class="item">1.2</div>\
353                             </div>\
354                         </div>\
355                         <div class="item">2</div>\
356                     </div>\
357                 </section>')),
358                 list = doc.root.contents()[0],
359                 nestedList = list.contents()[1].contents()[0],
360                 nestedListItem1 = nestedList.contents()[0],
361                 nestedListItem2 = nestedList.contents()[1];
362
363             doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
364
365             var section = doc.root,
366                 list = section.contents()[0],
367                 item1 = list.contents()[0],
368                 item2 = list.contents()[1],
369                 item3 = list.contents()[2],
370                 item4 = list.contents()[3];
371
372             expect(list.contents().length).to.equal(4, 'top list has four items');
373             expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
374             expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
375             expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
376             expect(item4.contents()[0].getText()).to.equal('2', 'fourth item ok');
377         });
378
379         it('extracts items out of outer most list when merge flag is set to false', function() {
380             var doc = getDocumentFromXML(removeEmptyTextNodes('\
381                 <section>\
382                     <div class="list.items">\
383                         <div class="item">0</div>\
384                         <div class="item">\
385                             <div class="list.items">\
386                                 <div class="item">1.1</div>\
387                                 <div class="item">1.2</div>\
388                             </div>\
389                         </div>\
390                         <div class="item">2</div>\
391                     </div>\
392                 </section>')),
393                 section = doc.root,
394                 list = section.contents()[0],
395                 nestedList = list.contents()[1].contents()[0],
396                 nestedListItem = nestedList.contents()[0];
397
398             var test = doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem, merge: false});
399
400             expect(test).to.equal(true, 'extraction status ok');
401
402             var sectionContents = section.contents(),
403                 extractedItem = sectionContents[1];
404
405             expect(sectionContents.length).to.equal(3, 'section has three children');
406             expect(sectionContents[0].is('list')).to.equal(true, 'first child is a list');
407
408             expect(extractedItem.getTagName()).to.equal('div', 'extracted item is a wlxml div');
409             expect(extractedItem.getClass()).to.equal('', 'extracted item has no wlxml class');
410             expect(extractedItem.contents()[0].getText()).to.equal('1.1', 'extracted item ok');
411             expect(sectionContents[2].is('list')).to.equal(true, 'second child is a list');
412         });
413     });
414
415 });
416
417 });