1 define(function(require) {
5 /* jshint multistr:true */
6 /* globals describe, it */
8 var chai = require('libs/chai'),
9 wlxml = require('wlxml/wlxml'),
11 $ = require('libs/jquery'),
12 listsExtension = require('wlxml/extensions/list/list');
15 var getDocumentFromXML = function(xml, options) {
16 var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
17 doc.registerExtension(listsExtension);
21 var removeEmptyTextNodes = function(xml) {
24 xml.find(':not(iframe)')
27 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
29 if(!this.data.length) {
33 return $('<w>').append(xml).html();
37 describe('Lists extension', function() {
39 describe('creating lists', function() {
40 it('allows creation of a list from existing sibling DocumentElements', function() {
41 var doc = getDocumentFromXML('<section>Alice<div>has</div>a<div>cat</div></section>'),
43 div1 = section.contents()[1],
44 textA = section.contents()[2];
46 doc.createList({node1: div1, node2: textA});
48 expect(section.contents().length).to.equal(3, 'section has three child nodes');
50 var child1 = section.contents()[0],
51 list = section.contents()[1],
52 child3 = section.contents()[2];
54 expect(child1.getText()).to.equal('Alice');
55 expect(list.is('list')).to.equal(true, 'second child is a list');
56 expect(list.contents().length).to.equal(2, 'list contains two elements');
57 list.contents().forEach(function(child) {
58 expect(child.getClass()).to.equal('item', 'list childs have wlxml class of item');
60 expect(child3.contents()[0].getText()).to.equal('cat');
63 it('allows creating nested list from existing sibling list items', function() {
64 var doc = getDocumentFromXML('\
67 <div class="item">A</div>\
68 <div class="item">B</div>\
69 <div class="item">C</div>\
70 <div class="item">D</div>\
74 var outerList = doc.root.contents('.list')[0],
75 itemB = outerList.contents('.item')[1],
76 itemC = outerList.contents('.item')[2];
79 doc.createList({node1: itemB, node2: itemC});
81 var outerListItems = outerList.contents('.item'),
82 innerList = outerListItems[1].contents()[0];
84 var innerListItems = innerList.contents('.item');
86 expect(outerListItems.length).to.equal(3, 'outer list has three items');
87 expect(outerListItems[0].contents()[0].getText()).to.equal('A', 'first outer item ok');
88 expect(outerListItems[1].getClass()).to.equal('item', 'inner list is wrapped by item element');
90 expect(innerList.is('list')).to.equal(true, 'inner list created');
91 expect(innerListItems.length).to.equal(2, 'inner list has two items');
92 expect(innerListItems[0].contents()[0].getText()).to.equal('B', 'first inner item ok');
93 expect(innerListItems[1].contents()[0].getText()).to.equal('C', 'second inner item ok');
95 expect(outerListItems[2].contents()[0].getText()).to.equal('D', 'last outer item ok');
100 describe('extracting list items', function() {
101 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
102 var doc = getDocumentFromXML(removeEmptyTextNodes('\
105 <div class="item">0</div>\
106 <div class="item">1</div>\
107 <div class="item">2</div>\
108 <div class="item">3</div>\
111 list = doc.root.contents()[0],
112 item1 = list.contents()[1],
113 item2 = list.contents()[2];
115 doc.extractItems({item1: item1, item2: item2});
117 var section = doc.root,
118 list1 = section.contents()[0],
119 oldItem1 = section.contents()[1],
120 oldItem2 = section.contents()[2],
121 list2 = section.contents()[3];
123 expect(section.contents().length).to.equal(4, 'section contains two old items and two lists');
125 expect(list1.is('list')).to.equal(true, 'first section child is a list');
126 expect(list1.contents().length).to.equal(1, 'first list has one child');
127 expect(list1.contents()[0].contents()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
129 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item got extracted');
130 expect(oldItem1.getClass() === '').to.equal(true, 'first extracted element has no wlxml class');
132 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item got extracted');
133 expect(oldItem2.getClass() === '').to.equal(true, 'second extracted element has no wlxml class');
135 expect(list2.is('list')).to.equal(true, 'last section child is a list');
136 expect(list2.contents().length).to.equal(1, 'second list has one child');
137 expect(list2.contents()[0].contents()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
140 it('puts extracted items above the list if starting item is the first one', function() {
141 var doc = getDocumentFromXML(removeEmptyTextNodes('\
144 <div class="item">0</div>\
145 <div class="item">1</div>\
146 <div class="item">2</div>\
149 list = doc.root.contents()[0],
150 item1 = list.contents()[0],
151 item2 = list.contents()[1],
152 item3 = list.contents()[2];
154 doc.extractItems({item1: item1, item2: item2});
156 var section = doc.root,
157 oldItem1 = section.contents()[0],
158 oldItem2 = section.contents()[1],
159 newList = section.contents()[2];
161 expect(section.contents().length).to.equal(3, 'section has three children');
162 expect(oldItem1.contents()[0].getText()).to.equal('0', 'first item extracted');
163 expect(oldItem2.contents()[0].getText()).to.equal('1', 'second item extracted');
164 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
165 expect(newList.contents().length).to.equal(1, 'list has now one child');
168 it('puts extracted items below the list if ending item is the last one', function() {
169 var doc = getDocumentFromXML(removeEmptyTextNodes('\
172 <div class="item">0</div>\
173 <div class="item">1</div>\
174 <div class="item">2</div>\
177 list = doc.root.contents()[0],
178 item1 = list.contents()[0],
179 item2 = list.contents()[1],
180 item3 = list.contents()[2];
182 doc.extractItems({item1: item2, item2: item3});
184 var section = doc.root,
185 oldItem1 = section.contents()[1],
186 oldItem2 = section.contents()[2],
187 newList = section.contents()[0];
189 expect(section.contents().length).to.equal(3, 'section has three children');
190 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item extracted');
191 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item extracted');
192 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
193 expect(newList.contents().length).to.equal(1, 'list has now one child');
196 it('removes list if all its items are extracted', function() {
197 var doc = getDocumentFromXML(removeEmptyTextNodes('\
200 <div class="item">some item</div>\
201 <div class="item">some item 2</div>\
204 list = doc.root.contents()[0],
205 item1 = list.contents()[0],
206 item2 = list.contents()[1];
208 doc.extractItems({item1: item1, item2: item2});
210 var section = doc.root,
211 oldItem1 = section.contents()[0],
212 oldItem2 = section.contents()[1];
214 expect(section.contents().length).to.equal(2, 'section contains two children');
215 expect(oldItem1.contents()[0].getText()).to.equal('some item');
216 expect(oldItem2.contents()[0].getText()).to.equal('some item 2');
219 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
220 var doc = getDocumentFromXML(removeEmptyTextNodes('\
223 <div class="item">0</div>\
225 <div class="list.items">\
226 <div class="item">1.1</div>\
227 <div class="item">1.2</div>\
228 <div class="item">1.3</div>\
231 <div class="item">2</div>\
234 list = doc.root.contents()[0],
235 nestedList = list.contents()[1].contents()[0],
236 nestedListItem = nestedList.contents()[1];
238 doc.extractItems({item1: nestedListItem, item2: nestedListItem}); //@@ name!
240 list = doc.root.contents()[0];
241 var item1 = list.contents()[0],
242 item2 = list.contents()[1], //
243 item3 = list.contents()[2],
244 item4 = list.contents()[3], //
245 item5 = list.contents()[4],
246 nestedList1 = item2.contents()[0],
247 nestedList2 = item4.contents()[0];
249 expect(list.contents().length).to.equal(5, 'top list has five items');
251 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
253 expect(item2.getClass()).to.equal('item', 'first nested list is still wrapped in item element');
254 expect(nestedList1.contents().length).to.equal(1, 'first nested list is left with one child');
255 expect(nestedList1.contents()[0].contents()[0].getText()).to.equal('1.1', 'first nested list item left alone');
257 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
259 expect(item4.getClass()).to.equal('item', 'second nested list is still wrapped in item element');
260 expect(nestedList2.contents().length).to.equal(1, 'second nested list is left with one child');
261 expect(nestedList2.contents()[0].contents()[0].getText()).to.equal('1.3', 'second nested list item left alone');
263 expect(item5.contents()[0].getText()).to.equal('2', 'last item ok');
266 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
267 var doc = getDocumentFromXML(removeEmptyTextNodes('\
269 <div class="list.items">\
270 <div class="item">0</div>\
272 <div class="list.items">\
273 <div class="item">1.1</div>\
274 <div class="item">1.2</div>\
275 <div class="item">1.3</div>\
278 <div class="item">2</div>\
281 list = doc.root.contents()[0],
282 nestedList = list.contents()[1].contents()[0],
283 nestedListItem1 = nestedList.contents()[1],
284 nestedListItem2 = nestedList.contents()[2];
286 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
287 list = doc.root.contents()[0];
288 var item1 = list.contents()[0],
289 item2 = list.contents()[1],
290 item3 = list.contents()[2],
291 item4 = list.contents()[3],
292 item5 = list.contents()[4];
293 nestedList = item2.contents()[0];
295 expect(list.contents().length).to.equal(5, 'top list has five items');
296 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
297 expect(item2.getClass()).to.equal('item', 'nested list is still wrapped in item element');
298 expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
299 expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.1', 'nested list item left alone');
300 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
301 expect(item4.contents()[0].getText()).to.equal('1.3', 'fourth item ok');
302 expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
305 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
306 var doc = getDocumentFromXML(removeEmptyTextNodes('\
308 <div class="list.items">\
309 <div class="item">0</div>\
311 <div class="list.items">\
312 <div class="item">1.1</div>\
313 <div class="item">1.2</div>\
314 <div class="item">1.3</div>\
317 <div class="item">2</div>\
320 list = doc.root.contents()[0],
321 nestedList = list.contents()[1].contents()[0],
322 nestedListItem1 = nestedList.contents()[0],
323 nestedListItem2 = nestedList.contents()[1];
325 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
327 list = doc.root.contents()[0];
328 var item1 = list.contents()[0],
329 item2 = list.contents()[1],
330 item3 = list.contents()[2],
331 item4 = list.contents()[3],
332 item5 = list.contents()[4];
333 nestedList = item4.contents()[0];
335 expect(list.contents().length).to.equal(5, 'top list has five items');
336 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
337 expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
338 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
340 expect(item4.getClass()).to.equal('item', 'nested list is still wrapped in item element');
341 expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
342 expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.3', 'nested list item left alone');
343 expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
346 it('removes list if all its items are extracted - nested case', function() {
347 var doc = getDocumentFromXML(removeEmptyTextNodes('\
349 <div class="list.items">\
350 <div class="item">0</div>\
352 <div class="list.items">\
353 <div class="item">1.1</div>\
354 <div class="item">1.2</div>\
357 <div class="item">2</div>\
360 list = doc.root.contents()[0],
361 nestedList = list.contents()[1].contents()[0],
362 nestedListItem1 = nestedList.contents()[0],
363 nestedListItem2 = nestedList.contents()[1];
365 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
367 list = doc.root.contents()[0];
368 var item1 = list.contents()[0],
369 item2 = list.contents()[1],
370 item3 = list.contents()[2],
371 item4 = list.contents()[3];
373 expect(list.contents().length).to.equal(4, 'top list has four items');
374 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
375 expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
376 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
377 expect(item4.contents()[0].getText()).to.equal('2', 'fourth item ok');
380 it('extracts items out of outer most list when merge flag is set to false', function() {
381 var doc = getDocumentFromXML(removeEmptyTextNodes('\
383 <div class="list.items">\
384 <div class="item">0</div>\
386 <div class="list.items">\
387 <div class="item">1.1</div>\
388 <div class="item">1.2</div>\
391 <div class="item">2</div>\
395 list = section.contents()[0],
396 nestedList = list.contents()[1].contents()[0],
397 nestedListItem = nestedList.contents()[0];
399 var test = doc.extractItems({item1: nestedListItem, item2: nestedListItem, merge: false});
401 expect(test).to.equal(true, 'extraction status ok');
403 var sectionContents = section.contents(),
404 extractedItem = sectionContents[1];
406 expect(sectionContents.length).to.equal(3, 'section has three children');
407 expect(sectionContents[0].is('list')).to.equal(true, 'first child is a list');
409 expect(extractedItem.getTagName()).to.equal('div', 'extracted item is a wlxml div');
410 expect(extractedItem.getClass()).to.equal('', 'extracted item has no wlxml class');
411 expect(extractedItem.contents()[0].getText()).to.equal('1.1', 'extracted item ok');
412 expect(sectionContents[2].is('list')).to.equal(true, 'second child is a list');