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('Handles text nodes on the boundries', function() {
64 var doc = getDocumentFromXML('<section>Alice<span>has</span>a cat</section>'),
65 textNode1 = doc.root.contents()[0],
66 textNode2 = doc.root.contents()[2];
67 doc.createList({node1: textNode1, node2: textNode2});
68 expect(doc.root.contents().length).to.equal(1);
69 expect(doc.root.contents()[0].is('list')).to.equal(true);
72 it('allows creating nested list from existing sibling list items', function() {
73 var doc = getDocumentFromXML('\
76 <div class="item">A</div>\
77 <div class="item">B</div>\
78 <div class="item">C</div>\
79 <div class="item">D</div>\
83 var outerList = doc.root.contents('.list')[0],
84 itemB = outerList.contents('.item')[1],
85 itemC = outerList.contents('.item')[2];
88 doc.createList({node1: itemB, node2: itemC});
90 var outerListItems = outerList.contents('.item'),
91 innerList = outerListItems[1].contents()[0];
93 var innerListItems = innerList.contents('.item');
95 expect(outerListItems.length).to.equal(3, 'outer list has three items');
96 expect(outerListItems[0].contents()[0].getText()).to.equal('A', 'first outer item ok');
97 expect(outerListItems[1].getClass()).to.equal('item', 'inner list is wrapped by item element');
99 expect(innerList.is('list')).to.equal(true, 'inner list created');
100 expect(innerListItems.length).to.equal(2, 'inner list has two items');
101 expect(innerListItems[0].contents()[0].getText()).to.equal('B', 'first inner item ok');
102 expect(innerListItems[1].contents()[0].getText()).to.equal('C', 'second inner item ok');
104 expect(outerListItems[2].contents()[0].getText()).to.equal('D', 'last outer item ok');
109 describe('extracting list items', function() {
110 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
111 var doc = getDocumentFromXML(removeEmptyTextNodes('\
114 <div class="item">0</div>\
115 <div class="item">1</div>\
116 <div class="item">2</div>\
117 <div class="item">3</div>\
120 list = doc.root.contents()[0],
121 item1 = list.contents()[1],
122 item2 = list.contents()[2];
124 doc.extractItems({item1: item1, item2: item2});
126 var section = doc.root,
127 list1 = section.contents()[0],
128 oldItem1 = section.contents()[1],
129 oldItem2 = section.contents()[2],
130 list2 = section.contents()[3];
132 expect(section.contents().length).to.equal(4, 'section contains two old items and two lists');
134 expect(list1.is('list')).to.equal(true, 'first section child is a list');
135 expect(list1.contents().length).to.equal(1, 'first list has one child');
136 expect(list1.contents()[0].contents()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
138 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item got extracted');
139 expect(oldItem1.getClass() === 'p').to.equal(true, 'first extracted element became a paragraph');
141 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item got extracted');
142 expect(oldItem2.getClass() === 'p').to.equal(true, 'second extracted element became a paragraph');
144 expect(list2.is('list')).to.equal(true, 'last section child is a list');
145 expect(list2.contents().length).to.equal(1, 'second list has one child');
146 expect(list2.contents()[0].contents()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
149 it('puts extracted items above the list if starting item is the first one', function() {
150 var doc = getDocumentFromXML(removeEmptyTextNodes('\
153 <div class="item">0</div>\
154 <div class="item">1</div>\
155 <div class="item">2</div>\
158 list = doc.root.contents()[0],
159 item1 = list.contents()[0],
160 item2 = list.contents()[1];
162 doc.extractItems({item1: item1, item2: item2});
164 var section = doc.root,
165 oldItem1 = section.contents()[0],
166 oldItem2 = section.contents()[1],
167 newList = section.contents()[2];
169 expect(section.contents().length).to.equal(3, 'section has three children');
170 expect(oldItem1.contents()[0].getText()).to.equal('0', 'first item extracted');
171 expect(oldItem2.contents()[0].getText()).to.equal('1', 'second item extracted');
172 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
173 expect(newList.contents().length).to.equal(1, 'list has now one child');
176 it('puts extracted items below the list if ending item is the last one', function() {
177 var doc = getDocumentFromXML(removeEmptyTextNodes('\
180 <div class="item">0</div>\
181 <div class="item">1</div>\
182 <div class="item">2</div>\
185 list = doc.root.contents()[0],
186 item2 = list.contents()[1],
187 item3 = list.contents()[2];
189 doc.extractItems({item1: item2, item2: item3});
191 var section = doc.root,
192 oldItem1 = section.contents()[1],
193 oldItem2 = section.contents()[2],
194 newList = section.contents()[0];
196 expect(section.contents().length).to.equal(3, 'section has three children');
197 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item extracted');
198 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item extracted');
199 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
200 expect(newList.contents().length).to.equal(1, 'list has now one child');
203 it('removes list if all its items are extracted', function() {
204 var doc = getDocumentFromXML(removeEmptyTextNodes('\
207 <div class="item">some item</div>\
208 <div class="item">some item 2</div>\
211 list = doc.root.contents()[0],
212 item1 = list.contents()[0],
213 item2 = list.contents()[1];
215 doc.extractItems({item1: item1, item2: item2});
217 var section = doc.root,
218 oldItem1 = section.contents()[0],
219 oldItem2 = section.contents()[1];
221 expect(section.contents().length).to.equal(2, 'section contains two children');
222 expect(oldItem1.contents()[0].getText()).to.equal('some item');
223 expect(oldItem2.contents()[0].getText()).to.equal('some item 2');
226 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
227 var doc = getDocumentFromXML(removeEmptyTextNodes('\
230 <div class="item">0</div>\
232 <div class="list.items">\
233 <div class="item">1.1</div>\
234 <div class="item">1.2</div>\
235 <div class="item">1.3</div>\
238 <div class="item">2</div>\
241 list = doc.root.contents()[0],
242 nestedList = list.contents()[1].contents()[0],
243 nestedListItem = nestedList.contents()[1];
245 doc.extractItems({item1: nestedListItem, item2: nestedListItem}); //@@ name!
247 list = doc.root.contents()[0];
248 var item1 = list.contents()[0],
249 item2 = list.contents()[1], //
250 item3 = list.contents()[2],
251 item4 = list.contents()[3], //
252 item5 = list.contents()[4],
253 nestedList1 = item2.contents()[0],
254 nestedList2 = item4.contents()[0];
256 expect(list.contents().length).to.equal(5, 'top list has five items');
258 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
260 expect(item2.getClass()).to.equal('item', 'first nested list is still wrapped in item element');
261 expect(nestedList1.contents().length).to.equal(1, 'first nested list is left with one child');
262 expect(nestedList1.contents()[0].contents()[0].getText()).to.equal('1.1', 'first nested list item left alone');
264 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
266 expect(item4.getClass()).to.equal('item', 'second nested list is still wrapped in item element');
267 expect(nestedList2.contents().length).to.equal(1, 'second nested list is left with one child');
268 expect(nestedList2.contents()[0].contents()[0].getText()).to.equal('1.3', 'second nested list item left alone');
270 expect(item5.contents()[0].getText()).to.equal('2', 'last item ok');
273 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
274 var doc = getDocumentFromXML(removeEmptyTextNodes('\
276 <div class="list.items">\
277 <div class="item">0</div>\
279 <div class="list.items">\
280 <div class="item">1.1</div>\
281 <div class="item">1.2</div>\
282 <div class="item">1.3</div>\
285 <div class="item">2</div>\
288 list = doc.root.contents()[0],
289 nestedList = list.contents()[1].contents()[0],
290 nestedListItem1 = nestedList.contents()[1],
291 nestedListItem2 = nestedList.contents()[2];
293 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
294 list = doc.root.contents()[0];
295 var item1 = list.contents()[0],
296 item2 = list.contents()[1],
297 item3 = list.contents()[2],
298 item4 = list.contents()[3],
299 item5 = list.contents()[4];
300 nestedList = item2.contents()[0];
302 expect(list.contents().length).to.equal(5, 'top list has five items');
303 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
304 expect(item2.getClass()).to.equal('item', 'nested list is still wrapped in item element');
305 expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
306 expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.1', 'nested list item left alone');
307 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
308 expect(item4.contents()[0].getText()).to.equal('1.3', 'fourth item ok');
309 expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
312 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
313 var doc = getDocumentFromXML(removeEmptyTextNodes('\
315 <div class="list.items">\
316 <div class="item">0</div>\
318 <div class="list.items">\
319 <div class="item">1.1</div>\
320 <div class="item">1.2</div>\
321 <div class="item">1.3</div>\
324 <div class="item">2</div>\
327 list = doc.root.contents()[0],
328 nestedList = list.contents()[1].contents()[0],
329 nestedListItem1 = nestedList.contents()[0],
330 nestedListItem2 = nestedList.contents()[1];
332 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
334 list = doc.root.contents()[0];
335 var item1 = list.contents()[0],
336 item2 = list.contents()[1],
337 item3 = list.contents()[2],
338 item4 = list.contents()[3],
339 item5 = list.contents()[4];
340 nestedList = item4.contents()[0];
342 expect(list.contents().length).to.equal(5, 'top list has five items');
343 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
344 expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
345 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
347 expect(item4.getClass()).to.equal('item', 'nested list is still wrapped in item element');
348 expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
349 expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.3', 'nested list item left alone');
350 expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
353 it('removes list if all its items are extracted - nested case', function() {
354 var doc = getDocumentFromXML(removeEmptyTextNodes('\
356 <div class="list.items">\
357 <div class="item">0</div>\
359 <div class="list.items">\
360 <div class="item">1.1</div>\
361 <div class="item">1.2</div>\
364 <div class="item">2</div>\
367 list = doc.root.contents()[0],
368 nestedList = list.contents()[1].contents()[0],
369 nestedListItem1 = nestedList.contents()[0],
370 nestedListItem2 = nestedList.contents()[1];
372 doc.extractItems({item1: nestedListItem1, item2: nestedListItem2});
374 list = doc.root.contents()[0];
375 var item1 = list.contents()[0],
376 item2 = list.contents()[1],
377 item3 = list.contents()[2],
378 item4 = list.contents()[3];
380 expect(list.contents().length).to.equal(4, 'top list has four items');
381 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
382 expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
383 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
384 expect(item4.contents()[0].getText()).to.equal('2', 'fourth item ok');
387 it('extracts items out of outer most list when merge flag is set to false', function() {
388 var doc = getDocumentFromXML(removeEmptyTextNodes('\
390 <div class="list.items">\
391 <div class="item">0</div>\
393 <div class="list.items">\
394 <div class="item">1.1</div>\
395 <div class="item">1.2</div>\
398 <div class="item">2</div>\
402 list = section.contents()[0],
403 nestedList = list.contents()[1].contents()[0],
404 nestedListItem = nestedList.contents()[0];
406 var test = doc.extractItems({item1: nestedListItem, item2: nestedListItem, merge: false});
408 expect(test.sameNode(nestedListItem)).to.equal(true, 'first item returned');
410 var sectionContents = section.contents(),
411 extractedItem = sectionContents[1];
413 expect(sectionContents.length).to.equal(3, 'section has three children');
414 expect(sectionContents[0].is('list')).to.equal(true, 'first child is a list');
416 expect(extractedItem.getTagName()).to.equal('div', 'extracted item is a wlxml div');
417 expect(extractedItem.getClass()).to.equal('p', 'extracted item became a paragraph');
418 expect(extractedItem.contents()[0].getText()).to.equal('1.1', 'extracted item ok');
419 expect(sectionContents[2].is('list')).to.equal(true, 'second child is a list');