3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
9 var expect = chai.expect;
12 describe('Canvas', function() {
13 describe('basic properties', function() {
14 it('renders empty document when canvas created from empty XML', function() {
15 var c = canvas.fromXML('');
16 expect(c.doc()).to.equal(null);
19 it('gives access to its document root node', function() {
20 var c = canvas.fromXML('<section></section>');
21 expect(c.doc().wlxmlTag).to.equal('section');
24 describe('DocumentElement', function() {
25 it('knows index of its child', function() {
26 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
28 child = root.children()[1];
29 expect(root.childIndex(child)).to.equal(1);
34 describe('document representation api', function() {
35 describe('document root element', function() {
36 var c = canvas.fromXML('<section></section>');
37 it('exists', function() {
38 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
40 it('is of type DocumentNodeElement', function() {
41 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
45 describe('DocumentElements comparison', function() {
46 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
47 var c = canvas.fromXML('<section><div></div><div></div></section>'),
48 first_div1 = c.doc().children()[0],
49 first_div2 = c.doc().children()[0],
50 second_div = c.doc().children()[1];
51 expect(first_div1.sameNode(first_div1)).to.be.true;
52 expect(first_div1.sameNode(first_div2)).to.be.true;
53 expect(first_div1.sameNode(second_div)).to.be.false;
57 describe('traversing', function() {
58 it('reports element nodes', function() {
59 var c = canvas.fromXML('<section><div></div></section>'),
60 children = c.doc().children();
61 expect(children.length).to.equal(1);
62 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
64 c = canvas.fromXML('<section><div></div><div></div></section>'),
65 children = c.doc().children();
66 expect(children.length).to.equal(2);
67 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
68 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
71 it('reports text nodes', function() {
72 var c = canvas.fromXML('<section>Alice</section>'),
73 children = c.doc().children();
74 expect(children.length).to.equal(1);
75 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
78 describe('accessing parents', function() {
79 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
80 var c = canvas.fromXML('<section><div></div></section>'),
81 div = c.doc().children()[0];
82 expect(div.parent().sameNode(c.doc())).to.be.true;
84 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
85 var c = canvas.fromXML('<section>Alice</section>'),
86 text = c.doc().children()[0];
87 expect(text.parent().sameNode(c.doc())).to.be.true;
91 describe('free text handling', function() {
92 it('sees free text', function() {
93 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
94 children = c.doc().children();
95 expect(children.length).to.equal(3);
96 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
97 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
98 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
102 describe('white characters handling', function() {
103 it('says empty element node has no children', function() {
104 var c = canvas.fromXML('<section></section>');
105 expect(c.doc().children().length).to.equal(0);
107 it('says element node with one space has one DocumentTextElement', function() {
108 var c = canvas.fromXML('<section> </section>');
109 expect(c.doc().children().length).to.equal(1);
110 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
112 it('ignores white space surrounding block elements', function() {
113 var c = canvas.fromXML('<section> <div></div> </section>');
114 var children = c.doc().children();
115 expect(children.length).to.equal(1);
116 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
118 it('ignores white space between block elements', function() {
119 var c = canvas.fromXML('<section><div></div> <div></div></section>');
120 var children = c.doc().children();
121 expect(children.length === 2);
122 [0,1].forEach(function(idx) {
123 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
129 describe('manipulation api', function() {
131 describe('Basic Element inserting', function() {
132 it('can put new NodeElement at the end', function() {
133 var c = canvas.fromXML('<section></section>'),
134 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
135 children = c.doc().children();
137 expect(children.length).to.equal(1);
138 expect(children[0].sameNode(appended));
141 it('can put new NodeElement after another NodeElement', function() {
142 var c = canvas.fromXML('<section><div></div></section>'),
143 div = c.doc().children()[0],
144 added = div.after({tag: 'header', klass: 'some.class'}),
145 children = c.doc().children();
146 expect(children.length).to.equal(2);
147 expect(children[1].sameNode(added));
151 describe('wrapping', function() {
152 it('wraps DocumentNodeElement', function() {
153 var c = canvas.fromXML('<section><div></div></section>'),
154 div = c.doc().children()[0];
156 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
157 parent = div.parent(),
158 parent2 = c.doc().children()[0];
160 expect(returned.sameNode(parent)).to.be.true;
161 expect(returned.sameNode(parent2)).to.be.true;
163 it('wraps DocumentTextElement', function() {
164 var c = canvas.fromXML('<section>Alice</section>'),
165 text = c.doc().children()[0];
167 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
168 parent = text.parent(),
169 parent2 = c.doc().children()[0];
171 expect(returned.sameNode(parent)).to.be.true;
172 expect(returned.sameNode(parent2)).to.be.true;