1 define(function(require) {
5 var chai = require('libs/chai'),
6 wlxml = require('wlxml/wlxml'),
8 $ = require('libs/jquery'),
9 lists = require('wlxml/extensions/list/list');
12 var getDocumentFromXML = function(xml, options) {
13 return wlxml.WLXMLDocumentFromXML(xml, options || {});
16 var removeEmptyTextNodes = function(xml) {
18 xml.find(':not(iframe)')
21 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
23 if(!this.data.length) {
27 return $('<w>').append(xml).html();
31 describe.only('Lists extension', function() {
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>'),
37 div1 = section.contents()[1],
38 textA = section.contents()[2];
40 doc.transform('createList', {node1: div1, node2: textA});
42 expect(section.contents().length).to.equal(3, 'section has three child nodes');
44 var child1 = section.contents()[0],
45 list = section.contents()[1],
46 child3 = section.contents()[2];
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');
54 expect(child3.contents()[0].getText()).to.equal('cat');
57 it('allows creating nested list from existing sibling list items', function() {
58 var doc = getDocumentFromXML('\
61 <div class="item">A</div>\
62 <div class="item">B</div>\
63 <div class="item">C</div>\
64 <div class="item">D</div>\
68 var outerList = doc.root.contents('.list')[0],
69 itemB = outerList.contents('.item')[1],
70 itemC = outerList.contents('.item')[2];
73 doc.transform('createList', {node1: itemB, node2: itemC});
75 var outerListItems = outerList.contents('.item'),
76 innerList = outerListItems[1].contents()[0];
78 var innerListItems = innerList.contents('.item');
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');
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');
89 expect(outerListItems[2].contents()[0].getText()).to.equal('D', 'last outer item ok');
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('\
99 <div class="item">0</div>\
100 <div class="item">1</div>\
101 <div class="item">2</div>\
102 <div class="item">3</div>\
105 list = doc.root.contents()[0],
106 item1 = list.contents()[1],
107 item2 = list.contents()[2];
109 doc.transform('extractItems', {item1: item1, item2: item2});
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];
117 expect(section.contents().length).to.equal(4, 'section contains two old items and two lists');
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');
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');
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');
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');
134 it('puts extracted items above the list if starting item is the first one', function() {
135 var doc = getDocumentFromXML(removeEmptyTextNodes('\
138 <div class="item">0</div>\
139 <div class="item">1</div>\
140 <div class="item">2</div>\
143 list = doc.root.contents()[0],
144 item1 = list.contents()[0],
145 item2 = list.contents()[1],
146 item3 = list.contents()[2];
148 doc.transform('extractItems', {item1: item1, item2: item2});
150 var section = doc.root,
151 oldItem1 = section.contents()[0],
152 oldItem2 = section.contents()[1],
153 newList = section.contents()[2];
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');
162 it('puts extracted items below the list if ending item is the last one', function() {
163 var doc = getDocumentFromXML(removeEmptyTextNodes('\
166 <div class="item">0</div>\
167 <div class="item">1</div>\
168 <div class="item">2</div>\
171 list = doc.root.contents()[0],
172 item1 = list.contents()[0],
173 item2 = list.contents()[1],
174 item3 = list.contents()[2];
176 doc.transform('extractItems', {item1: item2, item2: item3});
178 var section = doc.root,
179 oldItem1 = section.contents()[1],
180 oldItem2 = section.contents()[2],
181 newList = section.contents()[0];
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');
190 it('removes list if all its items are extracted', function() {
191 var doc = getDocumentFromXML(removeEmptyTextNodes('\
194 <div class="item">some item</div>\
195 <div class="item">some item 2</div>\
198 list = doc.root.contents()[0],
199 item1 = list.contents()[0],
200 item2 = list.contents()[1];
202 doc.transform('extractItems', {item1: item1, item2: item2});
204 var section = doc.root,
205 oldItem1 = section.contents()[0],
206 oldItem2 = section.contents()[1];
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');
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('\
217 <div class="item">0</div>\
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>\
225 <div class="item">2</div>\
228 list = doc.root.contents()[0],
229 nestedList = list.contents()[1].contents()[0],
230 nestedListItem = nestedList.contents()[1];
232 doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem});
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];
244 expect(list.contents().length).to.equal(5, 'top list has five items');
246 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
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');
252 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
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');
258 expect(item5.contents()[0].getText()).to.equal('2', 'last item ok');
261 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
262 var doc = getDocumentFromXML(removeEmptyTextNodes('\
264 <div class="list.items">\
265 <div class="item">0</div>\
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>\
273 <div class="item">2</div>\
276 list = doc.root.contents()[0],
277 nestedList = list.contents()[1].contents()[0],
278 nestedListItem1 = nestedList.contents()[1],
279 nestedListItem2 = nestedList.contents()[2];
281 doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
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];
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');
302 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
303 var doc = getDocumentFromXML(removeEmptyTextNodes('\
305 <div class="list.items">\
306 <div class="item">0</div>\
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>\
314 <div class="item">2</div>\
317 list = doc.root.contents()[0],
318 nestedList = list.contents()[1].contents()[0],
319 nestedListItem1 = nestedList.contents()[0],
320 nestedListItem2 = nestedList.contents()[1];
322 doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
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];
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');
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');
344 it('removes list if all its items are extracted - nested case', function() {
345 var doc = getDocumentFromXML(removeEmptyTextNodes('\
347 <div class="list.items">\
348 <div class="item">0</div>\
350 <div class="list.items">\
351 <div class="item">1.1</div>\
352 <div class="item">1.2</div>\
355 <div class="item">2</div>\
358 list = doc.root.contents()[0],
359 nestedList = list.contents()[1].contents()[0],
360 nestedListItem1 = nestedList.contents()[0],
361 nestedListItem2 = nestedList.contents()[1];
363 doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
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];
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');
379 it('extracts items out of outer most list when merge flag is set to false', function() {
380 var doc = getDocumentFromXML(removeEmptyTextNodes('\
382 <div class="list.items">\
383 <div class="item">0</div>\
385 <div class="list.items">\
386 <div class="item">1.1</div>\
387 <div class="item">1.2</div>\
390 <div class="item">2</div>\
394 list = section.contents()[0],
395 nestedList = list.contents()[1].contents()[0],
396 nestedListItem = nestedList.contents()[0];
398 var test = doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem, merge: false});
400 expect(test).to.equal(true, 'extraction status ok');
402 var sectionContents = section.contents(),
403 extractedItem = sectionContents[1];
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');
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');