X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6651adb0f9bd9b0c5c83047a3b66f66e1cc7fc57..1dcfdcafd9fd0ff3af5dc39619a3a953f4377a11:/modules/documentCanvas/canvas/canvas.test3.js?ds=inline diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 135b0b4..2244653 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -426,31 +426,86 @@ describe('Canvas', function() { expect(wrapperChildren[2].getText()).to.equal(' cat'); }); }); + + describe('unwrapping', function() { + it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() { + var c = canvas.fromXML('
Alice has a cat
'), + section = c.doc(), + text = section.children()[0].children()[0]; + + text.unwrap(); + + expect(section.children().length).to.equal(1); + expect(section.children()[0].getText()).to.equal('Alice has a cat'); + }) + }); }); describe('Lists api', function() { - it('allows creation of a list from existing sibling DocumentElements', function() { - var c = canvas.fromXML('\ -
\ - Alice\ -
has
\ - a\ -
cat
\ -
'), - section = c.doc(), - textAlice = section.children()[0], - divCat = section.children()[3] + describe('creating lists', function() { + it('allows creation of a list from existing sibling DocumentElements', function() { + var c = canvas.fromXML('\ +
\ + Alice\ +
has
\ + a\ +
cat
\ +
'), + section = c.doc(), + textHas = section.children()[1], + divA = section.children()[2] + + c.list.create({element1: textHas, element2: divA}); + + expect(section.children().length).to.equal(3, 'section has three child elements'); + + var child1 = section.children()[0], + list = section.children()[1], + child3 = section.children()[2]; + + expect(child1.getText()).to.equal('Alice'); + expect(list.is('list')).to.equal(true, 'second child is a list'); + expect(list.children().length).to.equal(2, 'list contains two elements'); + list.children().forEach(function(child) { + expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item'); + }); + expect(child3.children()[0].getText()).to.equal('cat'); + }); - c.list.create({element1: textAlice, element2: divCat}); + it('allows creating nested list from existing sibling list items', function() { + var c = canvas.fromXML('\ +
\ +
\ +
A
\ +
B
\ +
C
\ +
D
\ +
\ +
'), + outerList = c.doc().children()[0], + itemB = outerList.children()[1], + itemC = outerList.children()[2]; + + + c.list.create({element1: itemB, element2: itemC}); + + var outerListItems = outerList.children(), + innerList = outerListItems[1].children()[0], + innerListItems = innerList.children(); - expect(section.children().length).to.equal(1, 'section has one child element'); + expect(outerListItems.length).to.equal(3, 'outer list has three items'); + expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok'); + expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element'); + + expect(innerList.is('list')).to.equal(true, 'inner list created'); + expect(innerListItems.length).to.equal(2, 'inner list has two items'); + expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok'); + expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok'); + + expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok'); - var list = section.children()[0]; - expect(list.is('list')).to.equal(true, 'section\'s only child is a list'); - expect(list.children().length).to.equal(4, 'list contains four elements'); - list.children().forEach(function(child) { - expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item'); }); + }); describe('extracting list items', function() { @@ -499,20 +554,24 @@ describe('Canvas', function() {
\
0
\
1
\ +
2
\
\ '), list = c.doc().children()[0], item1 = list.children()[0], - item2 = list.children()[1]; + item2 = list.children()[1], + item3 = list.children()[2]; - c.list.extractItems({element1: item1, element2: item1}); + c.list.extractItems({element1: item1, element2: item2}); var section = c.doc(), - oldItem = section.children()[0], - newList = section.children()[1]; + oldItem1 = section.children()[0], + oldItem2 = section.children()[1], + newList = section.children()[2]; - expect(section.children().length).to.equal(2, 'section has two children'); - expect(oldItem.children()[0].getText()).to.equal('0', 'first item extracted'); + expect(section.children().length).to.equal(3, 'section has three children'); + expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted'); + expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted'); expect(newList.is('list')).to.equal(true, 'list lies below extracted item'); expect(newList.children().length).to.equal(1, 'list has now one child'); }); @@ -523,20 +582,24 @@ describe('Canvas', function() {
\
0
\
1
\ +
2
\
\ '), list = c.doc().children()[0], item1 = list.children()[0], - item2 = list.children()[1]; + item2 = list.children()[1], + item3 = list.children()[2]; - c.list.extractItems({element1: item2, element2: item2}); + c.list.extractItems({element1: item2, element2: item3}); var section = c.doc(), - oldItem = section.children()[1], + oldItem1 = section.children()[1], + oldItem2 = section.children()[2], newList = section.children()[0]; - expect(section.children().length).to.equal(2, 'section has two children'); - expect(oldItem.children()[0].getText()).to.equal('1', 'first item extracted'); + expect(section.children().length).to.equal(3, 'section has three children'); + expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted'); + expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted'); expect(newList.is('list')).to.equal(true, 'list lies above extracted item'); expect(newList.children().length).to.equal(1, 'list has now one child'); }); @@ -546,20 +609,23 @@ describe('Canvas', function() {
\
\
some item
\ +
some item 2
\
\
'), list = c.doc().children()[0], - item = list.children()[0]; + item1 = list.children()[0], + item2 = list.children()[1]; - c.list.extractItems({element1: item, element2: item}); + c.list.extractItems({element1: item1, element2: item2}); var section = c.doc(), list1 = section.children()[0], - oldItem1 = section.children()[1], - oldItem2 = section.children()[2], - list2 = section.children()[3]; + oldItem1 = section.children()[0], + oldItem2 = section.children()[1]; - expect(section.children().length).to.equal(1, 'section contains one child'); + expect(section.children().length).to.equal(2, 'section contains two children'); + expect(oldItem1.children()[0].getText()).to.equal('some item'); + expect(oldItem2.children()[0].getText()).to.equal('some item 2'); }); it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() { @@ -619,6 +685,7 @@ describe('Canvas', function() {
\
1.1
\
1.2
\ +
1.3
\
\ \
2
\ @@ -626,25 +693,28 @@ describe('Canvas', function() { '), list = c.doc().children()[0], nestedList = list.children()[1].children()[0], - nestedListItem = nestedList.children()[1]; + nestedListItem1 = nestedList.children()[1], + nestedListItem2 = nestedList.children()[2]; - c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2, merge: true}); var section = c.doc(), list = section.children()[0], item1 = list.children()[0], item2 = list.children()[1], item3 = list.children()[2], - item4 = list.children()[3]; + item4 = list.children()[3], + item5 = list.children()[4]; nestedList = item2.children()[0]; - expect(list.children().length).to.equal(4, 'top list has four items'); + expect(list.children().length).to.equal(5, 'top list has five items'); expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element'); expect(nestedList.children().length).to.equal(1, 'nested list is left with one child'); expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone'); - expect(item3.children()[0].getText()).to.equal('1.2', 'second item ok'); - expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok'); + expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok'); + expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok'); + expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok'); }); it('puts extracted items above the list if starting item is the first one - nested case' , function() { @@ -656,6 +726,7 @@ describe('Canvas', function() {
\
1.1
\
1.2
\ +
1.3
\
\ \
2
\ @@ -663,25 +734,29 @@ describe('Canvas', function() { '), list = c.doc().children()[0], nestedList = list.children()[1].children()[0], - nestedListItem = nestedList.children()[0]; + nestedListItem1 = nestedList.children()[0], + nestedListItem2 = nestedList.children()[1]; - c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2, merge: true}); var section = c.doc(), list = section.children()[0], item1 = list.children()[0], item2 = list.children()[1], item3 = list.children()[2], - item4 = list.children()[3]; - nestedList = item3.children()[0]; + item4 = list.children()[3], + item5 = list.children()[4]; + nestedList = item4.children()[0]; - expect(list.children().length).to.equal(4, 'top list has four items'); + expect(list.children().length).to.equal(5, 'top list has five items'); expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok'); - expect(item3.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element'); + expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok'); + + expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element'); expect(nestedList.children().length).to.equal(1, 'nested list is left with one child'); - expect(nestedList.children()[0].children()[0].getText()).to.equal('1.2', 'nested list item left alone'); - expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok'); + expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone'); + expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok'); }); it('removes list if all its items are extracted - nested case', function() { @@ -692,6 +767,7 @@ describe('Canvas', function() {
\
\
1.1
\ +
1.2
\
\
\
2
\ @@ -699,20 +775,23 @@ describe('Canvas', function() { '), list = c.doc().children()[0], nestedList = list.children()[1].children()[0], - nestedListItem = nestedList.children()[0]; + nestedListItem1 = nestedList.children()[0], + nestedListItem2 = nestedList.children()[1]; - c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: true}); + c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2, merge: true}); var section = c.doc(), list = section.children()[0], item1 = list.children()[0], item2 = list.children()[1], - item3 = list.children()[2]; + item3 = list.children()[2], + item4 = list.children()[3]; - expect(list.children().length).to.equal(3, 'top list has three items'); + expect(list.children().length).to.equal(4, 'top list has four items'); expect(item1.children()[0].getText()).to.equal('0', 'first item ok'); expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok'); - expect(item3.children()[0].getText()).to.equal('2', 'third item ok'); + expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok'); + expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok'); }); }); });