1 define(function(require) {
5 /* globals describe, it, afterEach */
7 var $ = require('libs/jquery'),
8 _ = require('libs/underscore'),
9 chai = require('libs/chai'),
10 sinon = require('libs/sinon'),
11 wlxml = require('wlxml/wlxml'),
12 canvas = require('modules/documentCanvas/canvas/canvas'),
13 keyboard = require('modules/documentCanvas/canvas/keyboard'),
14 keyEvent = require('modules/documentCanvas/canvas/keyEvent'),
15 corePlugin = require('./core.js'),
18 var K = keyboard.KEYS;
20 var getDocumentFromXML = function(xml, options) {
21 var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
22 doc.registerExtension(corePlugin.documentExtension);
27 var getCanvasFromXML = function(xml, elements) {
28 var c = canvas.fromXMLDocument(getDocumentFromXML(xml), elements),
30 view.attr('canvas-test', true);
31 /* globals document */
32 $(document.body).append(view);
35 var removeCanvas = function() {
36 $('[canvas-test]').remove();
39 var getTextNodes = function(text, doc) {
42 var search = function(node) {
43 node.contents().forEach(function(node) {
44 if(node.nodeType === Node.TEXT_NODE) {
45 if(node.getText() === text) {
57 var getTextNode = function(text, doc) {
58 var nodes = getTextNodes(text, doc),
60 if(nodes.length === 0) {
61 error = 'Text not found';
62 } else if(nodes.length > 1) {
63 error = 'Text not unique';
64 } else if(nodes[0].getText() !== text) {
65 error = 'I was trying to cheat your test :(';
68 throw new Error(error);
73 var getTextElement = function(text, c) {
74 var node = getTextNode(text, c.wlxmlDocument),
75 element = node && node.getData('canvasElement');
76 if(!(element && element.getText() === text)) {
83 describe('Document extensions', function() {
84 describe('break content', function() {
85 it('break text into two nodes', function() {
86 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
87 textNode = doc.root.contents()[0].contents()[0];
89 var result = textNode.breakContent({offset:3});
91 var section = doc.root;
92 expect(section.contents().length).to.equal(2);
93 expect(section.contents()[0].contents()[0].getText()).to.equal('Ali');
94 expect(section.contents()[1].contents()[0].getText()).to.equal('ce');
96 expect(result.first.sameNode(section.contents()[0])).to.equal(true);
97 expect(result.second.sameNode(section.contents()[1])).to.equal(true);
98 expect(result.emptyText).to.equal(undefined, 'no new text node created');
100 it('puts empty text node when breaking at the very beginning', function() {
101 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
102 textNode = doc.root.contents()[0].contents()[0];
104 var result = textNode.breakContent({offset:0}),
105 firstNode = doc.root.contents()[0];
107 expect(result.emptyText.sameNode(firstNode.contents()[0]));
108 expect(result.emptyText.getText()).to.equal('');
110 it('puts empty text node when breaking at the very end', function() {
111 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
112 textNode = doc.root.contents()[0].contents()[0];
114 var result = textNode.breakContent({offset:5}),
115 secondNode = doc.root.contents()[1];
117 expect(result.emptyText.sameNode(secondNode.contents()[0]));
118 expect(result.emptyText.getText()).to.equal('');
122 describe('mergin text with preceding content', function() {
123 it('does nothing if text node parent has no preceding element', function() {
124 var doc = getDocumentFromXML('<section><div><div>some text</div></div></section>'),
125 text = getTextNode('some text', doc),
128 doc.on('change', spy);
129 text.mergeContentUp();
130 expect(spy.callCount).to.equal(0);
132 it('does nothing if text node parent is precedeed by text node', function() {
133 var doc = getDocumentFromXML('<section><div></div>another text<div>some text</div></section>'),
134 text = getTextNode('some text', doc),
137 doc.on('change', spy);
138 text.mergeContentUp();
139 expect(spy.callCount).to.equal(0);
141 it('does nothing if text node is not first child of its parent', function() {
142 var doc = getDocumentFromXML('<section><div></div><div><a></a>some text</div></section>'),
143 text = getTextNode('some text', doc),
146 doc.on('change', spy);
147 text.mergeContentUp();
148 expect(spy.callCount).to.equal(0);
150 it('moves text node and its siblings to the block element preceding text node parent', function() {
151 var doc = getDocumentFromXML('<section><div></div><div>some text<span>is</span> here!</div></section>'),
152 text = getTextNode('some text', doc);
154 text.mergeContentUp();
156 var contents = doc.root.contents();
157 expect(contents.length).to.equal(1);
158 expect(contents[0].contents().length).to.equal(3);
159 expect(contents[0].contents()[0].getText()).to.equal('some text');
160 expect(contents[0].contents()[1].getTagName()).to.equal('span');
161 expect(contents[0].contents()[2].getText()).to.equal(' here!');
166 describe('Keyboard interactions', function() {
168 var Keyboard = function(canvas) {
169 this.canvas = canvas;
172 _.extend(Keyboard.prototype, {
173 press: function(key) {
174 this.canvas.triggerKeyEvent(keyEvent.fromParams({key:key}), this.selection);
175 this.selection = this.canvas.getSelection();
178 withCaret: function(where) {
179 var offset = where.indexOf('|'),
180 text = where.split('|').join(''),
181 el = getTextElement(text, this.canvas),
182 selection = this.canvas.createSelection({type: 'caret', element: el, offset: offset});
184 throw new Error('Invalid caret');
186 this.selection = selection;
189 withSelection: function(start, end) {
190 var startOffset = start.indexOf('|'),
191 endOffset = end.indexOf('|'),
192 startText= start.split('|').join(''),
193 endText = end.split('|').join(''),
194 startElement = getTextElement(startText, this.canvas),
195 endElement = getTextElement(endText, this.canvas),
196 selection = this.canvas.createSelection({
197 type: 'textSelection',
198 anchorElement: startElement,
199 anchorOffset: startOffset,
200 focusElement: endElement,
201 focusOffset: endOffset
203 if(startOffset === -1 || endOffset === -1) {
204 throw new Error('Invalid text selection');
206 this.selection = selection;
211 describe('deleting text with selection', function() {
212 afterEach(removeCanvas);
214 [K.BACKSPACE, K.DELETE].forEach(function(key) {
215 it('deletes text withing a single text element ' + key, function() {
216 var c = getCanvasFromXML('<section><div>Alice</div></section>'),
219 k.withSelection('A|lice', 'Alic|e').press(key);
220 expect(c.wlxmlDocument.root.contents()[0].contents()[0].getText()).to.equal('Ae');
223 var selection = c.getSelection();
224 expect(selection.type).to.equal('caret');
225 expect(selection.element.getText()).to.equal('Ae');
226 expect(selection.offset).to.equal(1);
228 it('deletes text across two paragraphs ' + key, function() {
229 var c = getCanvasFromXML('<section><div class="p">Alice</div><div class="p">cat</div></section>'),
232 k.withSelection('A|lice', 'c|at').press(key);
233 var rootContents = c.wlxmlDocument.root.contents();
235 expect(rootContents.length).to.equal(2);
236 expect(rootContents[0].contents()[0].getText()).to.equal('A');
237 expect(rootContents[1].contents()[0].getText()).to.equal('at');
239 var selection = c.getSelection();
240 expect(selection.type).to.equal('caret');
241 expect(selection.element.wlxmlNode.getText()).to.equal(key === K.BACKSPACE ? 'A' : 'at');
244 it('keeps an empty paragraph after deleting its whole text ' + key, function() {
245 var c = getCanvasFromXML('<section><div class="p">Alice</div></section>'),
248 k.withSelection('|Alice', 'Alice|').press(key);
249 var rootContents = c.wlxmlDocument.root.contents();
251 expect(rootContents.length).to.equal(1);
252 expect(rootContents[0].contents()[0].getText()).to.equal('');
254 var selection = c.getSelection();
255 expect(selection.type).to.equal('caret');
256 expect(selection.element.wlxmlNode.parent().sameNode(c.wlxmlDocument.root.contents()[0]));
263 describe('backspace at the beginning', function() {
264 afterEach(removeCanvas);
266 it('merges two adjacent paragraphs', function() {
267 var c = getCanvasFromXML('<section><div class="p">A</div><div class="p">B</div></section>'),
270 k.withCaret('|B').press(K.BACKSPACE);
272 var rootContents = c.wlxmlDocument.root.contents();
273 expect(rootContents.length).to.equal(1);
274 expect(rootContents[0].getClass()).to.equal('p');
275 expect(rootContents[0].contents()[0].getText()).to.equal('AB');
277 var selection = c.getSelection();
278 expect(selection.type).to.equal('caret');
279 expect(selection.element.sameNode(getTextElement('AB', c))).to.equal(true);
280 expect(selection.offset).to.equal(1);
282 it('merges a paragraph with a header', function() {
283 var c = getCanvasFromXML('<section><header>A</header><div class="p">B</div></section>'),
286 k.withCaret('|B').press(K.BACKSPACE);
288 var rootContents = c.wlxmlDocument.root.contents();
289 expect(rootContents.length).to.equal(1);
290 expect(rootContents[0].getTagName()).to.equal('header');
291 expect(rootContents[0].contents()[0].getText()).to.equal('AB');
293 var selection = c.getSelection();
294 expect(selection.type).to.equal('caret');
295 expect(selection.element.sameNode(getTextElement('AB', c))).to.equal(true);
296 expect(selection.offset).to.equal(1);
298 it('merges two adjacent headers', function() {
299 var c = getCanvasFromXML('<section><header>A</header><header>B</header></section>'),
302 k.withCaret('|B').press(K.BACKSPACE);
303 var rootContents = c.wlxmlDocument.root.contents();
304 expect(rootContents.length).to.equal(1);
305 expect(rootContents[0].getTagName()).to.equal('header');
306 expect(rootContents[0].contents()[0].getText()).to.equal('AB');
308 var selection = c.getSelection();
309 expect(selection.type).to.equal('caret');
310 expect(selection.element.sameNode(getTextElement('AB', c))).to.equal(true);
311 expect(selection.offset).to.equal(1);
313 it('merges a header with a paragraph', function() {
314 var c = getCanvasFromXML('<section><div class="p">A</div><header>B</header></section>'),
317 k.withCaret('|B').press(K.BACKSPACE);
319 var rootContents = c.wlxmlDocument.root.contents();
320 expect(rootContents.length).to.equal(1);
321 expect(rootContents[0].is('p')).to.equal(true);
322 expect(rootContents[0].contents()[0].getText()).to.equal('AB');
324 var selection = c.getSelection();
325 expect(selection.type).to.equal('caret');
326 expect(selection.element.sameNode(getTextElement('AB', c))).to.equal(true);
327 expect(selection.offset).to.equal(1);
329 it('merges a paragraph into a last list item', function() {
330 var c = getCanvasFromXML('<section><div class="list"><div class="item">item</div></div><div class="p">paragraph</div></section>'),
331 list = c.wlxmlDocument.root.contents()[0],
334 k.withCaret('|paragraph').press(K.BACKSPACE);
336 var rootContents = c.wlxmlDocument.root.contents();
337 expect(rootContents.length).to.equal(1);
338 expect(rootContents[0].sameNode(list)).to.equal(true);
340 var items = list.contents();
341 expect(items.length).to.equal(1);
342 expect(items[0].contents()[0].getText()).to.equal('itemparagraph');
344 var selection = c.getSelection();
345 expect(selection.type).to.equal('caret');
346 expect(selection.element.sameNode(getTextElement('itemparagraph', c))).to.equal(true);
347 expect(selection.offset).to.equal(4);
349 it('merges a list item with a list item', function() {
350 var c = getCanvasFromXML('<section><div class="list"><div class="item">item1</div><div class="item">item2</div></div></section>'),
351 list = c.wlxmlDocument.root.contents()[0],
354 k.withCaret('|item2').press(K.BACKSPACE);
356 var rootContents = c.wlxmlDocument.root.contents();
357 expect(rootContents.length).to.equal(1);
359 expect(rootContents[0].sameNode(list)).to.equal(true);
361 var items = list.contents();
363 expect(items.length).to.equal(1);
364 expect(items[0].contents()[0].getText()).to.equal('item1item2');
366 var selection = c.getSelection();
367 expect(selection.type).to.equal('caret');
368 expect(selection.element.sameNode(getTextElement('item1item2', c))).to.equal(true);
369 expect(selection.offset).to.equal(5);
371 it('creates a new paragraph preceding the list from a first list item', function() {
372 var c = getCanvasFromXML('<section><div class="list"><div class="item">item1</div><div class="item">item2</div></div></section>'),
373 list = c.wlxmlDocument.root.contents()[0],
376 k.withCaret('|item1').press(K.BACKSPACE);
378 var rootContents = c.wlxmlDocument.root.contents();
379 expect(rootContents.length).to.equal(2);
381 expect(rootContents[0].getClass()).to.equal('p');
382 expect(rootContents[0].contents()[0].getText()).to.equal('item1');
384 expect(rootContents[1].sameNode(list)).to.equal(true);
386 var selection = c.getSelection();
387 expect(selection.type).to.equal('caret');
388 expect(selection.element.sameNode(getTextElement('item1', c))).to.equal(true);
389 expect(selection.offset).to.equal(0);
391 it('removes list after moving up its only item', function() {
392 var c = getCanvasFromXML('<section><div class="list"><div class="item">item</div></div></section>'),
395 k.withCaret('|item').press(K.BACKSPACE);
396 var rootContents = c.wlxmlDocument.root.contents();
397 expect(rootContents.length).to.equal(1);
399 expect(rootContents[0].getClass()).to.equal('p');
400 expect(rootContents[0].contents()[0].getText()).to.equal('item');
402 var selection = c.getSelection();
403 expect(selection.type).to.equal('caret');
404 expect(selection.element.sameNode(getTextElement('item', c))).to.equal(true);
405 expect(selection.offset).to.equal(0);
409 describe('splitting with enter', function() {
410 afterEach(removeCanvas);
412 it('splits paragraph into two in the middle', function() {
413 var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
416 k.withCaret('para|graph').press(K.ENTER);
418 var rootContents = c.wlxmlDocument.root.contents();
419 expect(rootContents.length).to.equal(2);
420 expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
421 expect(rootContents[0].contents()[0].getText()).to.equal('para');
422 expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
423 expect(rootContents[1].contents()[0].getText()).to.equal('graph');
425 var selection = c.getSelection();
426 expect(selection.type).to.equal('caret');
427 expect(selection.element.sameNode(getTextElement('graph', c))).to.equal(true);
428 expect(selection.offset).to.equal(0);
430 it('splits paragraph into two at the beginning', function() {
431 var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
434 k.withCaret('|paragraph').press(K.ENTER);
436 var rootContents = c.wlxmlDocument.root.contents();
437 expect(rootContents.length).to.equal(2);
438 expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
439 expect(rootContents[0].contents()[0].getText()).to.equal('');
440 expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
441 expect(rootContents[1].contents()[0].getText()).to.equal('paragraph');
443 var selection = c.getSelection();
444 expect(selection.type).to.equal('caret');
445 expect(selection.element.sameNode(getTextElement('', c))).to.equal(true);
446 expect(selection.offset).to.equal(0);
448 it('splits paragraph into two at the end', function() {
449 var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
452 k.withCaret('paragraph|').press(K.ENTER);
454 var rootContents = c.wlxmlDocument.root.contents();
455 expect(rootContents.length).to.equal(2);
456 expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
457 expect(rootContents[0].contents()[0].getText()).to.equal('paragraph');
458 expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
459 expect(rootContents[1].contents()[0].getText()).to.equal('');
461 var selection = c.getSelection();
462 expect(selection.type).to.equal('caret');
463 expect(selection.element.sameNode(getTextElement('', c))).to.equal(true);
464 expect(selection.offset).to.equal(0);