| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
37×
1×
9×
9×
9×
9×
9×
1×
32×
32×
32×
32×
32×
32×
32×
1×
1×
1×
8×
8×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1000×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| /**
* Editor.spec.js
* (c) 2015~ Summernote Team
* summernote may be freely distributed under the MIT license./
*/
import chai from 'chai';
import spies from 'chai-spies';
import chaidom from '../../chaidom';
import $ from 'jquery';
import env from '../../../src/js/base/core/env';
import range from '../../../src/js/base/core/range';
import Context from '../../../src/js/base/Context';
import '../../../src/js/bs4/settings';
describe('Editor', () => {
var expect = chai.expect;
chai.use(spies);
chai.use(chaidom);
var editor, context, $editable;
function expectContents(context, markup) {
expect(context.layoutInfo.editable.html()).to.equalsIgnoreCase(markup);
}
function expectToHaveBeenCalled(context, customEvent, handler) {
const $note = context.layoutInfo.note;
const spy = chai.spy();
$note.on(customEvent, spy);
handler();
expect(spy).to.have.been.called();
}
beforeEach(function() {
$('body').empty(); // important !
var $note = $('<div><p>hello</p></div>');
var options = $.extend({}, $.summernote.options);
context = new Context($note, options);
editor = context.modules.editor;
$editable = context.layoutInfo.editable;
// [workaround]
// - Firefox need setTimeout for applying contents
// - IE8-11 can't create range in headless mode
Iif (env.isFF || env.isMSIE || env.isEdge) {
this.skip();
}
});
describe('initialize', () => {
it('should bind custom events', () => {
[
'keydown', 'keyup', 'blur', 'mousedown', 'mouseup',
'scroll', 'focusin', 'focusout',
].forEach((eventName) => {
expectToHaveBeenCalled(context, 'summernote.' + eventName, () => {
$editable.trigger(eventName);
});
});
expectToHaveBeenCalled(context, 'summernote.change', () => {
editor.insertText('hello');
});
});
});
Eif (env.isWebkit) {
describe('undo and redo', () => {
it('should control history', () => {
editor.insertText(' world');
expectContents(context, '<p>hello world</p>');
editor.undo();
expectContents(context, '<p>hello</p>');
editor.redo();
expectContents(context, '<p>hello world</p>');
});
});
}
describe('tab', () => {
it('should insert tab', () => {
editor.tab();
expectContents(context, '<p>hello </p>');
});
});
describe('insertParagraph', () => {
it('should insert paragraph', () => {
editor.insertParagraph();
expectContents(context, '<p>hello</p><p><br></p>');
editor.insertParagraph();
expectContents(context, '<p>hello</p><p><br></p><p><br></p>');
});
});
Eif (env.isWebkit) {
describe('insertImage', () => {
it('should insert image', () => {
var source = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAF0lEQVQYGWP8////fwYsgAmLGFiIHhIAT+oECGHuN2UAAAAASUVORK5CYII=';
return editor.insertImage(source, 'image').then(() => {
expectContents(context, '<p>hello<img src="' + source + '" data-filename="image" style="width: 0px;"></p>');
});
});
});
}
describe('insertOrderedList and insertUnorderedList', () => {
it('should toggle paragraph to list', () => {
editor.insertOrderedList();
expectContents(context, '<ol><li>hello</li></ol>');
editor.insertUnorderedList();
expectContents(context, '<ul><li>hello</li></ul>');
editor.insertUnorderedList();
expectContents(context, '<p>hello</p>');
});
});
describe('indent and outdent', () => {
// [workaround] style is different by browser
Eif (env.isPhantom) {
it('should indent and outdent paragraph', () => {
editor.indent();
expectContents(context, '<p style="margin-left: 25px;">hello</p>');
editor.outdent();
expectContents(context, '<p style="">hello</p>');
});
}
it('should indent and outdent list', () => {
editor.insertOrderedList();
expectContents(context, '<ol><li>hello</li></ol>');
editor.indent();
expectContents(context, '<ol><li><ol><li>hello</li></ol></li></ol>');
editor.indent();
expectContents(context, '<ol><li><ol><li><ol><li>hello</li></ol></li></ol></li></ol>');
editor.outdent();
expectContents(context, '<ol><li><ol><li>hello</li></ol></li></ol>');
editor.outdent();
expectContents(context, '<ol><li>hello</li></ol>');
});
});
describe('insertNode', () => {
it('should insert node', () => {
editor.insertNode($('<span> world</span>')[0]);
expectContents(context, '<p>hello<span> world</span></p>');
});
it('should be limited', () => {
var options = $.extend({}, $.summernote.options);
options.maxTextLength = 5;
context = new Context($('<div><p>hello</p></div>'), options);
editor = context.modules.editor;
editor.insertNode($('<span> world</span>')[0]);
expectContents(context, '<p>hello</p>');
});
it('should insert node in last focus', () => {
$editable.appendTo('body');
context.invoke('editor.focus');
editor.insertNode($('<span> world</span>')[0]);
$('body').focus();
editor.insertNode($('<span> hello</span>')[0]);
expectContents(context, '<p><span> world</span><span> hello</span>hello</p>');
$editable.remove();
});
});
describe('insertText', () => {
it('should insert text', () => {
editor.insertText(' world');
expectContents(context, '<p>hello world</p>');
});
it('should be limited', () => {
var options = $.extend({}, $.summernote.options);
options.maxTextLength = 5;
context = new Context($('<div><p>hello</p></div>'), options);
editor = context.modules.editor;
editor.insertText(' world');
expectContents(context, '<p>hello</p>');
});
it('should insert text in last focus', () => {
$editable.appendTo('body');
context.invoke('editor.focus');
editor.insertText(' world');
$('body').focus();
editor.insertText(' summernote ');
expectContents(context, '<p> world summernote hello</p>');
$editable.remove();
});
});
describe('pasteHTML', () => {
it('should paste html', () => {
editor.pasteHTML('<span> world</span>');
expectContents(context, '<p>hello<span> world</span></p>');
});
it('should not call change change event more than once per paste event', () => {
var generateLargeHtml = () => {
var html = '<div>';
for (var i = 0; i < 1000; i++) {
html += '<p>HTML element #' + i + '</p>';
}
html += '</div>';
return html;
};
var $note = context.layoutInfo.note;
var spy = chai.spy();
$note.on('summernote.change', spy);
var html = generateLargeHtml();
editor.pasteHTML(html);
expect(spy).to.have.been.called.once;
});
it('should be limited', () => {
var options = $.extend({}, $.summernote.options);
options.maxTextLength = 5;
context = new Context($('<div><p>hello</p></div>'), options);
editor = context.modules.editor;
editor.pasteHTML('<span> world</span>');
expectContents(context, '<p>hello</p>');
});
});
describe('insertHorizontalRule', () => {
it('should insert horizontal rule', () => {
editor.insertHorizontalRule();
expectContents(context, '<p>hello</p><hr><p><br></p>');
});
});
describe('insertTable', () => {
it('should insert table', () => {
var markup = [
'<p>hello</p>',
'<table class="table table-bordered"><tbody>',
'<tr><td><br></td><td><br></td></tr>',
'<tr><td><br></td><td><br></td></tr>',
'</tbody></table>',
'<p><br></p>',
].join('');
editor.insertTable('2x2');
expectContents(context, markup);
});
});
describe('empty', () => {
it('should make contents empty', () => {
editor.empty();
expect(editor.isEmpty()).to.be.true;
});
});
describe('formatBlock', () => {
it('should apply formatBlock', () => {
$editable.appendTo('body');
editor.formatBlock('blockquote');
// start <p>hello</p> => <blockquote>hello</blockquote>
expectContents(context, '<blockquote>hello</blockquote>');
});
it('should apply multi formatBlock', () => {
// set multi block html
var codes = [
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
];
context.invoke('code', codes.join(''));
// run formatBlock
$editable.appendTo('body');
editor.formatBlock('blockquote');
// check current range position in blockquote element
var nodeName = $editable.children()[0].nodeName;
expect(nodeName).to.equalsIgnoreCase('blockquote');
});
it('should apply multi test 2 - formatBlock', () => {
var codes = [
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
];
context.invoke('code', codes.join(''));
$editable.appendTo('body');
var startNode = $editable.find('p').first()[0];
var endNode = $editable.find('p').last()[0];
// all p tags is wrapped
range.create(startNode, 1, endNode, 1).normalize().select();
editor.formatBlock('blockquote');
var nodeName = $editable.children()[0].nodeName;
expect(nodeName).to.equalsIgnoreCase('blockquote');
// p -> blockquote, p is none
expect($editable.find('p').length).to.equals(0);
});
it('should apply custom className in formatBlock', () => {
var $target = $('<blockquote class="blockquote" />');
$editable.appendTo('body');
editor.formatBlock('blockquote', $target);
// start <p>hello</p> => <blockquote class="blockquote">hello</blockquote>
expectContents(context, '<blockquote class="blockquote">hello</blockquote>');
});
it('should find exact target in formatBlock', () => {
var $target = $('<a class="dropdown-item" href="#" data-value="blockquote" role="listitem" aria-label="blockquote"><blockquote class="blockquote">Blockquote</blockquote></a>');
$editable.appendTo('body');
editor.formatBlock('blockquote', $target);
// start <p>hello</p> => <blockquote class="blockquote">hello</blockquote>
expectContents(context, '<blockquote class="blockquote">hello</blockquote>');
});
});
describe('createLink', () => {
it('should create normal link', () => {
var text = 'hello';
var pNode = $editable.find('p')[0];
var textNode = pNode.childNodes[0];
var startIndex = textNode.wholeText.indexOf(text);
var endIndex = startIndex + text.length;
range.create(textNode, startIndex, textNode, endIndex).normalize().select();
// check creation normal link
editor.createLink({
url: 'http://summernote.org',
text: 'summernote',
});
expectContents(context, '<p>hello<a href="http://summernote.org">summernote</a></p>');
});
it('should create a link with range', () => {
var text = 'hello';
var pNode = $editable.find('p')[0];
var textNode = pNode.childNodes[0];
var startIndex = textNode.wholeText.indexOf(text);
var endIndex = startIndex + text.length;
var rng = range.create(textNode, startIndex, textNode, endIndex);
editor.createLink({
url: 'http://summernote.org',
text: 'summernote',
range: rng,
});
expectContents(context, '<p><a href="http://summernote.org">summernote</a></p>');
});
it('should create a link with isNewWindow', () => {
var text = 'hello';
var pNode = $editable.find('p')[0];
var textNode = pNode.childNodes[0];
var startIndex = textNode.wholeText.indexOf(text);
var endIndex = startIndex + text.length;
var rng = range.create(textNode, startIndex, textNode, endIndex);
editor.createLink({
url: 'http://summernote.org',
text: 'summernote',
range: rng,
isNewWindow: true,
});
expectContents(context, '<p><a href="http://summernote.org" target="_blank">summernote</a></p>');
});
it('should create a relative link without scheme', () => {
var text = 'hello';
var pNode = $editable.find('p')[0];
var textNode = pNode.childNodes[0];
var startIndex = textNode.wholeText.indexOf(text);
var endIndex = startIndex + text.length;
var rng = range.create(textNode, startIndex, textNode, endIndex);
editor.createLink({
url: '/relative/url',
text: 'summernote',
range: rng,
isNewWindow: true,
});
expectContents(context, '<p><a href="/relative/url" target="_blank">summernote</a></p>');
});
it('should modify a link', () => {
context.invoke('code', '<p><a href="http://summernote.org">hello world</a></p>');
var anchorNode = $editable.find('a')[0];
var rng = range.createFromNode(anchorNode);
editor.createLink({
url: 'http://wow.summernote.org',
text: 'summernote wow',
range: rng,
});
expectContents(context, '<p><a href="http://wow.summernote.org">summernote wow</a></p>');
});
it('should be limited when creating a link', () => {
var options = $.extend({}, $.summernote.options);
options.maxTextLength = 5;
context = new Context($('<div><p>hello</p></div>'), options);
editor = context.modules.editor;
editor.createLink({
url: 'http://summernote.org',
text: 'summernote',
});
expectContents(context, '<p>hello</p>');
});
it('should be limited when modifying a link', () => {
var options = $.extend({}, $.summernote.options);
options.maxTextLength = 5;
context = new Context($('<p><a href="http://summernote.org">hello</a></p>'), options);
var editable = context.layoutInfo.editable;
var anchorNode = editable.find('a')[0];
var rng = range.createFromNode(anchorNode);
editor = context.modules.editor;
editor.createLink({
url: 'http://summernote.org',
text: 'hello world',
range: rng,
});
expectContents(context, '<a href="http://summernote.org">hello</a>');
});
});
});
|