This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
listcommand.js
459 lines (388 loc) · 18.1 KB
/
listcommand.js
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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
import Model from '@ckeditor/ckeditor5-engine/src/model/model';
import ListCommand from '../src/listcommand';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
describe( 'ListCommand', () => {
let editor, command, model, doc, root;
beforeEach( () => {
editor = new Editor();
editor.model = new Model();
model = editor.model;
doc = model.document;
root = doc.createRoot();
command = new ListCommand( editor, 'bulleted' );
model.schema.register( 'listItem', {
inheritAllFrom: '$block',
allowAttributes: [ 'listType', 'listIndent' ]
} );
model.schema.register( 'paragraph', {
inheritAllFrom: '$block',
allowIn: 'widget'
} );
model.schema.register( 'widget', { inheritAllFrom: '$block' } );
model.schema.addChildCheck( ( ctx, childDef ) => {
if ( ctx.endsWith( 'widget' ) && childDef.name == 'listItem' ) {
return false;
}
} );
setData(
model,
'<paragraph>foo</paragraph>' +
'<listItem listType="bulleted" listIndent="0">bulleted</listItem>' +
'<listItem listType="numbered" listIndent="0">numbered</listItem>' +
'<paragraph>bar</paragraph>' +
'<widget>' +
'<paragraph>xyz</paragraph>' +
'</widget>'
);
model.change( writer => {
writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
} );
} );
afterEach( () => {
command.destroy();
} );
describe( 'ListCommand', () => {
describe( 'constructor()', () => {
it( 'should create list command with given type and value set to false', () => {
expect( command.type ).to.equal( 'bulleted' );
expect( command.value ).to.be.false;
const numberedList = new ListCommand( editor, 'numbered' );
expect( numberedList.type ).to.equal( 'numbered' );
} );
} );
describe( 'value', () => {
it( 'should be false if first position in selection is not in a list item', () => {
model.change( writer => {
writer.setSelection( doc.getRoot().getChild( 3 ), 0 );
} );
expect( command.value ).to.be.false;
} );
it( 'should be false if first position in selection is in a list item of different type', () => {
model.change( writer => {
writer.setSelection( doc.getRoot().getChild( 2 ), 0 );
} );
expect( command.value ).to.be.false;
} );
it( 'should be true if first position in selection is in a list item of same type', () => {
model.change( writer => {
writer.setSelection( doc.getRoot().getChild( 1 ), 0 );
} );
expect( command.value ).to.be.true;
} );
} );
describe( 'isEnabled', () => {
it( 'should be true if entire selection is in a list', () => {
setData( model, '<listItem listType="bulleted" listIndent="0">[a]</listItem>' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be true if entire selection is in a block which can be turned into a list', () => {
setData( model, '<paragraph>[a]</paragraph>' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be true if selection first position is in a block which can be turned into a list', () => {
setData( model, '<paragraph>[a</paragraph><widget>b]</widget>' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be false if selection first position is in an element which cannot be converted to a list item', () => {
setData( model, '<widget><paragraph>[a</paragraph></widget><paragraph>b]</paragraph>' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be false in a root which does not allow blocks at all', () => {
doc.createRoot( 'paragraph', 'inlineOnlyRoot' );
setData( model, 'a[]b', { rootName: 'inlineOnlyRoot' } );
expect( command.isEnabled ).to.be.false;
} );
} );
describe( 'execute()', () => {
it( 'should use parent batch', () => {
model.change( writer => {
expect( writer.batch.operations.length ).to.equal( 0 );
command.execute();
expect( writer.batch.operations.length ).to.be.above( 0 );
} );
} );
describe( 'collapsed selection', () => {
it( 'should rename closest block to listItem and set correct attributes', () => {
setData( model, '<paragraph>fo[]o</paragraph>' );
command.execute();
expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
} );
it( 'should rename closest listItem to paragraph', () => {
setData( model, '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
command.execute();
// Attributes will be removed by post fixer.
expect( getData( model ) ).to.equal( '<paragraph listIndent="0" listType="bulleted">fo[]o</paragraph>' );
} );
it( 'should change closest listItem\' type', () => {
setData( model, '<listItem listIndent="0" listType="numbered">fo[]o</listItem>' );
command.execute();
expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
} );
it( 'should handle outdenting sub-items when list item is turned off', () => {
/* eslint-disable max-len */
// Taken from docs.
//
// 1 * --------
// 2 * --------
// 3 * -------- <- this is turned off.
// 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
// 5 * -------- <- this should be still be a child of item above, so indent = 1.
// 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
// 7 * -------- <- this should be still be a child of item above, so indent = 1.
// 8 * -------- <- this has to become indent = 0.
// 9 * -------- <- this should still be a child of item above, so indent = 1.
// 10 * -------- <- this should still be a child of item above, so indent = 2.
// 11 * -------- <- this should still be at the same level as item above, so indent = 2.
// 12 * -------- <- this and all below are left unchanged.
// 13 * --------
// 14 * --------
//
// After turning off "3", the list becomes:
//
// 1 * --------
// 2 * --------
//
// 3 --------
//
// 4 * --------
// 5 * --------
// 6 * --------
// 7 * --------
// 8 * --------
// 9 * --------
// 10 * --------
// 11 * --------
// 12 * --------
// 13 * --------
// 14 * --------
/* eslint-enable max-len */
setData(
model,
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">[]---</listItem>' +
'<listItem listIndent="3" listType="bulleted">---</listItem>' +
'<listItem listIndent="4" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>' +
'<listItem listIndent="3" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>' +
'<listItem listIndent="3" listType="bulleted">---</listItem>' +
'<listItem listIndent="3" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>'
);
command.execute();
const expectedData =
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<paragraph listIndent="2" listType="bulleted">[]---</paragraph>' + // Attributes will be removed by post fixer.
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
} );
describe( 'non-collapsed selection', () => {
beforeEach( () => {
setData(
model,
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<paragraph>---</paragraph>' +
'<paragraph>---</paragraph>' +
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>'
);
} );
// https://github.com/ckeditor/ckeditor5-list/issues/62
it( 'should not rename blocks which cannot become listItems (list item is not allowed in their parent)', () => {
model.schema.register( 'restricted' );
model.schema.extend( 'restricted', { allowIn: '$root' } );
model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
model.schema.extend( 'fooBlock', { allowIn: 'restricted' } );
setData(
model,
'<paragraph>a[bc</paragraph>' +
'<restricted><fooBlock></fooBlock></restricted>' +
'<paragraph>de]f</paragraph>'
);
command.execute();
expect( getData( model ) ).to.equal(
'<listItem listIndent="0" listType="bulleted">a[bc</listItem>' +
'<restricted><fooBlock></fooBlock></restricted>' +
'<listItem listIndent="0" listType="bulleted">de]f</listItem>'
);
} );
it( 'should not rename blocks which cannot become listItems (block is an object)', () => {
model.schema.register( 'image', {
isBlock: true,
isObject: true,
allowIn: '$root'
} );
setData(
model,
'<paragraph>a[bc</paragraph>' +
'<image></image>' +
'<paragraph>de]f</paragraph>'
);
command.execute();
expect( getData( model ) ).to.equal(
'<listItem listIndent="0" listType="bulleted">a[bc</listItem>' +
'<image></image>' +
'<listItem listIndent="0" listType="bulleted">de]f</listItem>'
);
} );
it( 'should rename closest block to listItem and set correct attributes', () => {
// From first paragraph to second paragraph.
// Command value=false, we are turning on list items.
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionAt( root.getChild( 2 ), 0 ),
writer.createPositionAt( root.getChild( 3 ), 'end' )
) );
} );
command.execute();
const expectedData =
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">[---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---]</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
it( 'should rename closest listItem to paragraph', () => {
// From second bullet list item to first numbered list item.
// Command value=true, we are turning off list items.
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionAt( root.getChild( 1 ), 0 ),
writer.createPositionAt( root.getChild( 4 ), 'end' )
) );
} );
// Convert paragraphs, leave numbered list items.
command.execute();
const expectedData =
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<paragraph listIndent="0" listType="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
'<paragraph>---</paragraph>' +
'<paragraph>---</paragraph>' +
'<paragraph listIndent="0" listType="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
it( 'should change closest listItem\'s type', () => {
// From first numbered lsit item to third bulleted list item.
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionAt( root.getChild( 4 ), 0 ),
writer.createPositionAt( root.getChild( 6 ), 0 )
) );
} );
// Convert paragraphs, leave numbered list items.
command.execute();
const expectedData =
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<paragraph>---</paragraph>' +
'<paragraph>---</paragraph>' +
'<listItem listIndent="0" listType="bulleted">[---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">]---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
it( 'should handle outdenting sub-items when list item is turned off', () => {
// From first numbered list item to third bulleted list item.
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionAt( root.getChild( 1 ), 0 ),
writer.createPositionAt( root.getChild( 5 ), 'end' )
) );
} );
// Convert paragraphs, leave numbered list items.
command.execute();
const expectedData =
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<paragraph listIndent="0" listType="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
'<paragraph>---</paragraph>' +
'<paragraph>---</paragraph>' +
'<paragraph listIndent="0" listType="numbered">---</paragraph>' + // Attributes will be removed by post fixer.
'<paragraph listIndent="0" listType="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
// Example from docs.
it( 'should change type of all items in nested list if one of items changed', () => {
setData(
model,
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="numbered">---</listItem>' +
'<listItem listIndent="2" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="numbered">---</listItem>' +
'<listItem listIndent="2" listType="numbered">---</listItem>' +
'<listItem listIndent="2" listType="numbered">-[-</listItem>' +
'<listItem listIndent="1" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="numbered">---</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="numbered">-]-</listItem>' +
'<listItem listIndent="1" listType="numbered">---</listItem>' +
'<listItem listIndent="2" listType="numbered">---</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>'
);
// * ------ <-- do not fix, top level item
// * ------ <-- fix, because latter list item of this item's list is changed
// * ------ <-- do not fix, item is not affected (different list)
// * ------ <-- fix, because latter list item of this item's list is changed
// * ------ <-- fix, because latter list item of this item's list is changed
// * ---[-- <-- already in selection
// * ------ <-- already in selection
// * ------ <-- already in selection
// * ------ <-- already in selection, but does not cause other list items to change because is top-level
// * ---]-- <-- already in selection
// * ------ <-- fix, because preceding list item of this item's list is changed
// * ------ <-- do not fix, item is not affected (different list)
// * ------ <-- do not fix, top level item
command.execute();
const expectedData =
'<listItem listIndent="0" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="numbered">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="bulleted">-[-</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="0" listType="bulleted">---</listItem>' +
'<listItem listIndent="1" listType="bulleted">-]-</listItem>' +
'<listItem listIndent="1" listType="bulleted">---</listItem>' +
'<listItem listIndent="2" listType="numbered">---</listItem>' +
'<listItem listIndent="0" listType="numbered">---</listItem>';
expect( getData( model ) ).to.equal( expectedData );
} );
} );
} );
} );
} );