1 define(function(require) {
5 var chai = require('libs/chai'),
6 wlxml = require('wlxml/wlxml'),
8 $ = require('libs/jquery'),
9 listsExtension = require('wlxml/extensions/list/list');
12 var getDocumentFromXML = function(xml, options) {
13 var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
14 doc.registerExtension(listsExtension);
18 var removeEmptyTextNodes = function(xml) {
20 xml.find(':not(iframe)')
23 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
25 if(!this.data.length) {
29 return $('<w>').append(xml).html();
33 describe('Lists extension', function() {
35 describe('creating lists', function() {
36 it('allows creation of a list from existing sibling DocumentElements', function() {
37 var doc = getDocumentFromXML('<section>Alice<div>has</div>a<div>cat</div></section>'),
39 div1 = section.contents()[1],
40 textA = section.contents()[2];
42 doc.transform('createList', {node1: div1, node2: textA});
44 expect(section.contents().length).to.equal(3, 'section has three child nodes');
46 var child1 = section.contents()[0],
47 list = section.contents()[1],
48 child3 = section.contents()[2];
50 expect(child1.getText()).to.equal('Alice');
51 expect(list.is('list')).to.equal(true, 'second child is a list');
52 expect(list.contents().length).to.equal(2, 'list contains two elements');
53 list.contents().forEach(function(child) {
54 expect(child.getClass()).to.equal('item', 'list childs have wlxml class of item');
56 expect(child3.contents()[0].getText()).to.equal('cat');
59 it('allows creating nested list from existing sibling list items', function() {
60 var doc = getDocumentFromXML('\
63 <div class="item">A</div>\
64 <div class="item">B</div>\
65 <div class="item">C</div>\
66 <div class="item">D</div>\
70 var outerList = doc.root.contents('.list')[0],
71 itemB = outerList.contents('.item')[1],
72 itemC = outerList.contents('.item')[2];
75 doc.transform('createList', {node1: itemB, node2: itemC});
77 var outerListItems = outerList.contents('.item'),
78 innerList = outerListItems[1].contents()[0];
80 var innerListItems = innerList.contents('.item');
82 expect(outerListItems.length).to.equal(3, 'outer list has three items');
83 expect(outerListItems[0].contents()[0].getText()).to.equal('A', 'first outer item ok');
84 expect(outerListItems[1].getClass()).to.equal('item', 'inner list is wrapped by item element');
86 expect(innerList.is('list')).to.equal(true, 'inner list created');
87 expect(innerListItems.length).to.equal(2, 'inner list has two items');
88 expect(innerListItems[0].contents()[0].getText()).to.equal('B', 'first inner item ok');
89 expect(innerListItems[1].contents()[0].getText()).to.equal('C', 'second inner item ok');
91 expect(outerListItems[2].contents()[0].getText()).to.equal('D', 'last outer item ok');
96 describe('extracting list items', function() {
97 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
98 var doc = getDocumentFromXML(removeEmptyTextNodes('\
101 <div class="item">0</div>\
102 <div class="item">1</div>\
103 <div class="item">2</div>\
104 <div class="item">3</div>\
107 list = doc.root.contents()[0],
108 item1 = list.contents()[1],
109 item2 = list.contents()[2];
111 doc.transform('extractItems', {item1: item1, item2: item2});
113 var section = doc.root,
114 list1 = section.contents()[0],
115 oldItem1 = section.contents()[1],
116 oldItem2 = section.contents()[2],
117 list2 = section.contents()[3];
119 expect(section.contents().length).to.equal(4, 'section contains two old items and two lists');
121 expect(list1.is('list')).to.equal(true, 'first section child is a list');
122 expect(list1.contents().length).to.equal(1, 'first list has one child');
123 expect(list1.contents()[0].contents()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
125 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item got extracted');
126 expect(oldItem1.getClass() === '').to.equal(true, 'first extracted element has no wlxml class');
128 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item got extracted');
129 expect(oldItem2.getClass() === '').to.equal(true, 'second extracted element has no wlxml class');
131 expect(list2.is('list')).to.equal(true, 'last section child is a list');
132 expect(list2.contents().length).to.equal(1, 'second list has one child');
133 expect(list2.contents()[0].contents()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
136 it('puts extracted items above the list if starting item is the first one', function() {
137 var doc = getDocumentFromXML(removeEmptyTextNodes('\
140 <div class="item">0</div>\
141 <div class="item">1</div>\
142 <div class="item">2</div>\
145 list = doc.root.contents()[0],
146 item1 = list.contents()[0],
147 item2 = list.contents()[1],
148 item3 = list.contents()[2];
150 doc.transform('extractItems', {item1: item1, item2: item2});
152 var section = doc.root,
153 oldItem1 = section.contents()[0],
154 oldItem2 = section.contents()[1],
155 newList = section.contents()[2];
157 expect(section.contents().length).to.equal(3, 'section has three children');
158 expect(oldItem1.contents()[0].getText()).to.equal('0', 'first item extracted');
159 expect(oldItem2.contents()[0].getText()).to.equal('1', 'second item extracted');
160 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
161 expect(newList.contents().length).to.equal(1, 'list has now one child');
164 it('puts extracted items below the list if ending item is the last one', function() {
165 var doc = getDocumentFromXML(removeEmptyTextNodes('\
168 <div class="item">0</div>\
169 <div class="item">1</div>\
170 <div class="item">2</div>\
173 list = doc.root.contents()[0],
174 item1 = list.contents()[0],
175 item2 = list.contents()[1],
176 item3 = list.contents()[2];
178 doc.transform('extractItems', {item1: item2, item2: item3});
180 var section = doc.root,
181 oldItem1 = section.contents()[1],
182 oldItem2 = section.contents()[2],
183 newList = section.contents()[0];
185 expect(section.contents().length).to.equal(3, 'section has three children');
186 expect(oldItem1.contents()[0].getText()).to.equal('1', 'first item extracted');
187 expect(oldItem2.contents()[0].getText()).to.equal('2', 'second item extracted');
188 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
189 expect(newList.contents().length).to.equal(1, 'list has now one child');
192 it('removes list if all its items are extracted', function() {
193 var doc = getDocumentFromXML(removeEmptyTextNodes('\
196 <div class="item">some item</div>\
197 <div class="item">some item 2</div>\
200 list = doc.root.contents()[0],
201 item1 = list.contents()[0],
202 item2 = list.contents()[1];
204 doc.transform('extractItems', {item1: item1, item2: item2});
206 var section = doc.root,
207 oldItem1 = section.contents()[0],
208 oldItem2 = section.contents()[1];
210 expect(section.contents().length).to.equal(2, 'section contains two children');
211 expect(oldItem1.contents()[0].getText()).to.equal('some item');
212 expect(oldItem2.contents()[0].getText()).to.equal('some item 2');
215 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
216 var doc = getDocumentFromXML(removeEmptyTextNodes('\
219 <div class="item">0</div>\
221 <div class="list.items">\
222 <div class="item">1.1</div>\
223 <div class="item">1.2</div>\
224 <div class="item">1.3</div>\
227 <div class="item">2</div>\
230 list = doc.root.contents()[0],
231 nestedList = list.contents()[1].contents()[0],
232 nestedListItem = nestedList.contents()[1];
234 doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem});
236 var section = doc.root,
237 list = section.contents()[0],
238 item1 = list.contents()[0],
239 item2 = list.contents()[1], //
240 item3 = list.contents()[2],
241 item4 = list.contents()[3], //
242 item5 = list.contents()[4],
243 nestedList1 = item2.contents()[0],
244 nestedList2 = item4.contents()[0];
246 expect(list.contents().length).to.equal(5, 'top list has five items');
248 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
250 expect(item2.getClass()).to.equal('item', 'first nested list is still wrapped in item element');
251 expect(nestedList1.contents().length).to.equal(1, 'first nested list is left with one child');
252 expect(nestedList1.contents()[0].contents()[0].getText()).to.equal('1.1', 'first nested list item left alone');
254 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
256 expect(item4.getClass()).to.equal('item', 'second nested list is still wrapped in item element');
257 expect(nestedList2.contents().length).to.equal(1, 'second nested list is left with one child');
258 expect(nestedList2.contents()[0].contents()[0].getText()).to.equal('1.3', 'second nested list item left alone');
260 expect(item5.contents()[0].getText()).to.equal('2', 'last item ok');
263 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
264 var doc = getDocumentFromXML(removeEmptyTextNodes('\
266 <div class="list.items">\
267 <div class="item">0</div>\
269 <div class="list.items">\
270 <div class="item">1.1</div>\
271 <div class="item">1.2</div>\
272 <div class="item">1.3</div>\
275 <div class="item">2</div>\
278 list = doc.root.contents()[0],
279 nestedList = list.contents()[1].contents()[0],
280 nestedListItem1 = nestedList.contents()[1],
281 nestedListItem2 = nestedList.contents()[2];
283 doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
285 var section = doc.root,
286 list = section.contents()[0],
287 item1 = list.contents()[0],
288 item2 = list.contents()[1],
289 item3 = list.contents()[2],
290 item4 = list.contents()[3],
291 item5 = list.contents()[4];
292 nestedList = item2.contents()[0];
294 expect(list.contents().length).to.equal(5, 'top list has five items');
295 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
296 expect(item2.getClass()).to.equal('item', 'nested list is still wrapped in item element');
297 expect(nestedList.contents().length).to.equal(1, 'nested list is left with one child');
298 expect(nestedList.contents()[0].contents()[0].getText()).to.equal('1.1', 'nested list item left alone');
299 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
300 expect(item4.contents()[0].getText()).to.equal('1.3', 'fourth item ok');
301 expect(item5.contents()[0].getText()).to.equal('2', 'fifth item ok');
304 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
305 var doc = getDocumentFromXML(removeEmptyTextNodes('\
307 <div class="list.items">\
308 <div class="item">0</div>\
310 <div class="list.items">\
311 <div class="item">1.1</div>\
312 <div class="item">1.2</div>\
313 <div class="item">1.3</div>\
316 <div class="item">2</div>\
319 list = doc.root.contents()[0],
320 nestedList = list.contents()[1].contents()[0],
321 nestedListItem1 = nestedList.contents()[0],
322 nestedListItem2 = nestedList.contents()[1];
324 doc.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
326 var section = doc.root,
327 list = section.contents()[0],
328 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.transform('extractItems', {item1: nestedListItem1, item2: nestedListItem2});
367 var section = doc.root,
368 list = section.contents()[0],
369 item1 = list.contents()[0],
370 item2 = list.contents()[1],
371 item3 = list.contents()[2],
372 item4 = list.contents()[3];
374 expect(list.contents().length).to.equal(4, 'top list has four items');
375 expect(item1.contents()[0].getText()).to.equal('0', 'first item ok');
376 expect(item2.contents()[0].getText()).to.equal('1.1', 'second item ok');
377 expect(item3.contents()[0].getText()).to.equal('1.2', 'third item ok');
378 expect(item4.contents()[0].getText()).to.equal('2', 'fourth item ok');
381 it('extracts items out of outer most list when merge flag is set to false', function() {
382 var doc = getDocumentFromXML(removeEmptyTextNodes('\
384 <div class="list.items">\
385 <div class="item">0</div>\
387 <div class="list.items">\
388 <div class="item">1.1</div>\
389 <div class="item">1.2</div>\
392 <div class="item">2</div>\
396 list = section.contents()[0],
397 nestedList = list.contents()[1].contents()[0],
398 nestedListItem = nestedList.contents()[0];
400 var test = doc.transform('extractItems', {item1: nestedListItem, item2: nestedListItem, merge: false});
402 expect(test).to.equal(true, 'extraction status ok');
404 var sectionContents = section.contents(),
405 extractedItem = sectionContents[1];
407 expect(sectionContents.length).to.equal(3, 'section has three children');
408 expect(sectionContents[0].is('list')).to.equal(true, 'first child is a list');
410 expect(extractedItem.getTagName()).to.equal('div', 'extracted item is a wlxml div');
411 expect(extractedItem.getClass()).to.equal('', 'extracted item has no wlxml class');
412 expect(extractedItem.contents()[0].getText()).to.equal('1.1', 'extracted item ok');
413 expect(sectionContents[2].is('list')).to.equal(true, 'second child is a list');