@ckeditor/ckeditor5-utils 35.0.0 → 35.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/package.json +5 -5
  2. package/src/areconnectedthroughproperties.js +75 -0
  3. package/src/ckeditorerror.js +195 -0
  4. package/src/collection.js +619 -0
  5. package/src/comparearrays.js +45 -0
  6. package/src/config.js +216 -0
  7. package/src/count.js +22 -0
  8. package/src/diff.js +113 -0
  9. package/src/difftochanges.js +76 -0
  10. package/src/dom/createelement.js +41 -0
  11. package/src/dom/emittermixin.js +301 -0
  12. package/src/dom/getancestors.js +27 -0
  13. package/src/dom/getborderwidths.js +24 -0
  14. package/src/dom/getcommonancestor.js +25 -0
  15. package/src/dom/getdatafromelement.js +20 -0
  16. package/src/dom/getpositionedancestor.js +23 -0
  17. package/src/dom/global.js +23 -0
  18. package/src/dom/indexof.js +21 -0
  19. package/src/dom/insertat.js +17 -0
  20. package/src/dom/iscomment.js +17 -0
  21. package/src/dom/isnode.js +24 -0
  22. package/src/dom/isrange.js +16 -0
  23. package/src/dom/istext.js +16 -0
  24. package/src/dom/isvisible.js +23 -0
  25. package/src/dom/iswindow.js +25 -0
  26. package/src/dom/position.js +328 -0
  27. package/src/dom/rect.js +364 -0
  28. package/src/dom/remove.js +18 -0
  29. package/src/dom/resizeobserver.js +145 -0
  30. package/src/dom/scroll.js +270 -0
  31. package/src/dom/setdatainelement.js +20 -0
  32. package/src/dom/tounit.js +25 -0
  33. package/src/elementreplacer.js +43 -0
  34. package/src/emittermixin.js +471 -0
  35. package/src/env.js +168 -0
  36. package/src/eventinfo.js +26 -0
  37. package/src/fastdiff.js +229 -0
  38. package/src/first.js +20 -0
  39. package/src/focustracker.js +103 -0
  40. package/src/index.js +36 -0
  41. package/src/inserttopriorityarray.js +21 -0
  42. package/src/isiterable.js +16 -0
  43. package/src/keyboard.js +222 -0
  44. package/src/keystrokehandler.js +114 -0
  45. package/src/language.js +20 -0
  46. package/src/locale.js +79 -0
  47. package/src/mapsequal.js +27 -0
  48. package/src/mix.js +44 -0
  49. package/src/nth.js +28 -0
  50. package/src/objecttomap.js +25 -0
  51. package/src/observablemixin.js +605 -0
  52. package/src/priorities.js +32 -0
  53. package/src/spy.js +22 -0
  54. package/src/toarray.js +7 -0
  55. package/src/tomap.js +27 -0
  56. package/src/translation-service.js +180 -0
  57. package/src/uid.js +55 -0
  58. package/src/unicode.js +91 -0
  59. package/src/version.js +148 -0
@@ -0,0 +1,619 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module utils/collection
7
+ */
8
+ import EmitterMixin from './emittermixin';
9
+ import CKEditorError from './ckeditorerror';
10
+ import uid from './uid';
11
+ import isIterable from './isiterable';
12
+ import mix from './mix';
13
+ /**
14
+ * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
15
+ * in the collection (like in an array) or by their ids.
16
+ *
17
+ * If an object without an `id` property is being added to the collection, the `id` property will be generated
18
+ * automatically. Note that the automatically generated id is unique only within this single collection instance.
19
+ *
20
+ * By default an item in the collection is identified by its `id` property. The name of the identifier can be
21
+ * configured through the constructor of the collection.
22
+ *
23
+ * @mixes module:utils/emittermixin~EmitterMixin
24
+ */
25
+ class Collection {
26
+ /**
27
+ * Creates a new Collection instance.
28
+ *
29
+ * You can provide an iterable of initial items the collection will be created with:
30
+ *
31
+ * const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
32
+ *
33
+ * console.log( collection.get( 0 ) ); // -> { id: 'John' }
34
+ * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
35
+ * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
36
+ *
37
+ * Or you can first create a collection and then add new items using the {@link #add} method:
38
+ *
39
+ * const collection = new Collection();
40
+ *
41
+ * collection.add( { id: 'John' } );
42
+ * console.log( collection.get( 0 ) ); // -> { id: 'John' }
43
+ *
44
+ * Whatever option you choose, you can always pass a configuration object as the last argument
45
+ * of the constructor:
46
+ *
47
+ * const emptyCollection = new Collection( { idProperty: 'name' } );
48
+ * emptyCollection.add( { name: 'John' } );
49
+ * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
50
+ *
51
+ * const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
52
+ * nonEmptyCollection.add( { name: 'George' } );
53
+ * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
54
+ * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
55
+ *
56
+ * @param {Iterable.<Object>|Object} [initialItemsOrOptions] The initial items of the collection or
57
+ * the options object.
58
+ * @param {Object} [options={}] The options object, when the first argument is an array of initial items.
59
+ * @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
60
+ * Items that do not have such a property will be assigned one when added to the collection.
61
+ */
62
+ constructor(initialItemsOrOptions = {}, options = {}) {
63
+ const hasInitialItems = isIterable(initialItemsOrOptions);
64
+ if (!hasInitialItems) {
65
+ options = initialItemsOrOptions;
66
+ }
67
+ this._items = [];
68
+ this._itemMap = new Map();
69
+ this._idProperty = options.idProperty || 'id';
70
+ this._bindToExternalToInternalMap = new WeakMap();
71
+ this._bindToInternalToExternalMap = new WeakMap();
72
+ this._skippedIndexesFromExternal = [];
73
+ // Set the initial content of the collection (if provided in the constructor).
74
+ if (hasInitialItems) {
75
+ for (const item of initialItemsOrOptions) {
76
+ this._items.push(item);
77
+ this._itemMap.set(this._getItemIdBeforeAdding(item), item);
78
+ }
79
+ }
80
+ }
81
+ /**
82
+ * The number of items available in the collection.
83
+ *
84
+ * @member {Number} #length
85
+ */
86
+ get length() {
87
+ return this._items.length;
88
+ }
89
+ /**
90
+ * Returns the first item from the collection or null when collection is empty.
91
+ *
92
+ * @returns {Object|null} The first item or `null` if collection is empty.
93
+ */
94
+ get first() {
95
+ return this._items[0] || null;
96
+ }
97
+ /**
98
+ * Returns the last item from the collection or null when collection is empty.
99
+ *
100
+ * @returns {Object|null} The last item or `null` if collection is empty.
101
+ */
102
+ get last() {
103
+ return this._items[this.length - 1] || null;
104
+ }
105
+ /**
106
+ * Adds an item into the collection.
107
+ *
108
+ * If the item does not have an id, then it will be automatically generated and set on the item.
109
+ *
110
+ * @chainable
111
+ * @param {Object} item
112
+ * @param {Number} [index] The position of the item in the collection. The item
113
+ * is pushed to the collection when `index` not specified.
114
+ * @fires add
115
+ * @fires change
116
+ */
117
+ add(item, index) {
118
+ return this.addMany([item], index);
119
+ }
120
+ /**
121
+ * Adds multiple items into the collection.
122
+ *
123
+ * Any item not containing an id will get an automatically generated one.
124
+ *
125
+ * @chainable
126
+ * @param {Iterable.<Object>} items
127
+ * @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
128
+ * @fires add
129
+ * @fires change
130
+ */
131
+ addMany(items, index) {
132
+ if (index === undefined) {
133
+ index = this._items.length;
134
+ }
135
+ else if (index > this._items.length || index < 0) {
136
+ /**
137
+ * The `index` passed to {@link module:utils/collection~Collection#addMany `Collection#addMany()`}
138
+ * is invalid. It must be a number between 0 and the collection's length.
139
+ *
140
+ * @error collection-add-item-invalid-index
141
+ */
142
+ throw new CKEditorError('collection-add-item-invalid-index', this);
143
+ }
144
+ let offset = 0;
145
+ for (const item of items) {
146
+ const itemId = this._getItemIdBeforeAdding(item);
147
+ const currentItemIndex = index + offset;
148
+ this._items.splice(currentItemIndex, 0, item);
149
+ this._itemMap.set(itemId, item);
150
+ this.fire('add', item, currentItemIndex);
151
+ offset++;
152
+ }
153
+ this.fire('change', {
154
+ added: items,
155
+ removed: [],
156
+ index
157
+ });
158
+ return this;
159
+ }
160
+ /**
161
+ * Gets an item by its ID or index.
162
+ *
163
+ * @param {String|Number} idOrIndex The item ID or index in the collection.
164
+ * @returns {Object|null} The requested item or `null` if such item does not exist.
165
+ */
166
+ get(idOrIndex) {
167
+ let item;
168
+ if (typeof idOrIndex == 'string') {
169
+ item = this._itemMap.get(idOrIndex);
170
+ }
171
+ else if (typeof idOrIndex == 'number') {
172
+ item = this._items[idOrIndex];
173
+ }
174
+ else {
175
+ /**
176
+ * An index or ID must be given.
177
+ *
178
+ * @error collection-get-invalid-arg
179
+ */
180
+ throw new CKEditorError('collection-get-invalid-arg', this);
181
+ }
182
+ return item || null;
183
+ }
184
+ /**
185
+ * Returns a Boolean indicating whether the collection contains an item.
186
+ *
187
+ * @param {Object|String} itemOrId The item or its ID in the collection.
188
+ * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
189
+ */
190
+ has(itemOrId) {
191
+ if (typeof itemOrId == 'string') {
192
+ return this._itemMap.has(itemOrId);
193
+ }
194
+ else { // Object
195
+ const idProperty = this._idProperty;
196
+ const id = itemOrId[idProperty];
197
+ return id && this._itemMap.has(id);
198
+ }
199
+ }
200
+ /**
201
+ * Gets an index of an item in the collection.
202
+ * When an item is not defined in the collection, the index will equal -1.
203
+ *
204
+ * @param {Object|String} itemOrId The item or its ID in the collection.
205
+ * @returns {Number} The index of a given item.
206
+ */
207
+ getIndex(itemOrId) {
208
+ let item;
209
+ if (typeof itemOrId == 'string') {
210
+ item = this._itemMap.get(itemOrId);
211
+ }
212
+ else {
213
+ item = itemOrId;
214
+ }
215
+ return item ? this._items.indexOf(item) : -1;
216
+ }
217
+ /**
218
+ * Removes an item from the collection.
219
+ *
220
+ * @param {Object|Number|String} subject The item to remove, its ID or index in the collection.
221
+ * @returns {Object} The removed item.
222
+ * @fires remove
223
+ * @fires change
224
+ */
225
+ remove(subject) {
226
+ const [item, index] = this._remove(subject);
227
+ this.fire('change', {
228
+ added: [],
229
+ removed: [item],
230
+ index
231
+ });
232
+ return item;
233
+ }
234
+ /**
235
+ * Executes the callback for each item in the collection and composes an array or values returned by this callback.
236
+ *
237
+ * @param {Function} callback
238
+ * @param {Object} callback.item
239
+ * @param {Number} callback.index
240
+ * @param {Object} [ctx] Context in which the `callback` will be called.
241
+ * @returns {Array} The result of mapping.
242
+ */
243
+ map(callback, ctx) {
244
+ return this._items.map(callback, ctx);
245
+ }
246
+ /**
247
+ * Finds the first item in the collection for which the `callback` returns a true value.
248
+ *
249
+ * @param {Function} callback
250
+ * @param {Object} callback.item
251
+ * @param {Number} callback.index
252
+ * @param {Object} [ctx] Context in which the `callback` will be called.
253
+ * @returns {Object|undefined} The item for which `callback` returned a true value.
254
+ */
255
+ find(callback, ctx) {
256
+ return this._items.find(callback, ctx);
257
+ }
258
+ /**
259
+ * Returns an array with items for which the `callback` returned a true value.
260
+ *
261
+ * @param {Function} callback
262
+ * @param {Object} callback.item
263
+ * @param {Number} callback.index
264
+ * @param {Object} [ctx] Context in which the `callback` will be called.
265
+ * @returns {Array} The array with matching items.
266
+ */
267
+ filter(callback, ctx) {
268
+ return this._items.filter(callback, ctx);
269
+ }
270
+ /**
271
+ * Removes all items from the collection and destroys the binding created using
272
+ * {@link #bindTo}.
273
+ *
274
+ * @fires remove
275
+ * @fires change
276
+ */
277
+ clear() {
278
+ if (this._bindToCollection) {
279
+ this.stopListening(this._bindToCollection);
280
+ this._bindToCollection = null;
281
+ }
282
+ const removedItems = Array.from(this._items);
283
+ while (this.length) {
284
+ this._remove(0);
285
+ }
286
+ this.fire('change', {
287
+ added: [],
288
+ removed: removedItems,
289
+ index: 0
290
+ });
291
+ }
292
+ /**
293
+ * Binds and synchronizes the collection with another one.
294
+ *
295
+ * The binding can be a simple factory:
296
+ *
297
+ * class FactoryClass {
298
+ * constructor( data ) {
299
+ * this.label = data.label;
300
+ * }
301
+ * }
302
+ *
303
+ * const source = new Collection( { idProperty: 'label' } );
304
+ * const target = new Collection();
305
+ *
306
+ * target.bindTo( source ).as( FactoryClass );
307
+ *
308
+ * source.add( { label: 'foo' } );
309
+ * source.add( { label: 'bar' } );
310
+ *
311
+ * console.log( target.length ); // 2
312
+ * console.log( target.get( 1 ).label ); // 'bar'
313
+ *
314
+ * source.remove( 0 );
315
+ * console.log( target.length ); // 1
316
+ * console.log( target.get( 0 ).label ); // 'bar'
317
+ *
318
+ * or the factory driven by a custom callback:
319
+ *
320
+ * class FooClass {
321
+ * constructor( data ) {
322
+ * this.label = data.label;
323
+ * }
324
+ * }
325
+ *
326
+ * class BarClass {
327
+ * constructor( data ) {
328
+ * this.label = data.label;
329
+ * }
330
+ * }
331
+ *
332
+ * const source = new Collection( { idProperty: 'label' } );
333
+ * const target = new Collection();
334
+ *
335
+ * target.bindTo( source ).using( ( item ) => {
336
+ * if ( item.label == 'foo' ) {
337
+ * return new FooClass( item );
338
+ * } else {
339
+ * return new BarClass( item );
340
+ * }
341
+ * } );
342
+ *
343
+ * source.add( { label: 'foo' } );
344
+ * source.add( { label: 'bar' } );
345
+ *
346
+ * console.log( target.length ); // 2
347
+ * console.log( target.get( 0 ) instanceof FooClass ); // true
348
+ * console.log( target.get( 1 ) instanceof BarClass ); // true
349
+ *
350
+ * or the factory out of property name:
351
+ *
352
+ * const source = new Collection( { idProperty: 'label' } );
353
+ * const target = new Collection();
354
+ *
355
+ * target.bindTo( source ).using( 'label' );
356
+ *
357
+ * source.add( { label: { value: 'foo' } } );
358
+ * source.add( { label: { value: 'bar' } } );
359
+ *
360
+ * console.log( target.length ); // 2
361
+ * console.log( target.get( 0 ).value ); // 'foo'
362
+ * console.log( target.get( 1 ).value ); // 'bar'
363
+ *
364
+ * It's possible to skip specified items by returning falsy value:
365
+ *
366
+ * const source = new Collection();
367
+ * const target = new Collection();
368
+ *
369
+ * target.bindTo( source ).using( item => {
370
+ * if ( item.hidden ) {
371
+ * return null;
372
+ * }
373
+ *
374
+ * return item;
375
+ * } );
376
+ *
377
+ * source.add( { hidden: true } );
378
+ * source.add( { hidden: false } );
379
+ *
380
+ * console.log( source.length ); // 2
381
+ * console.log( target.length ); // 1
382
+ *
383
+ * **Note**: {@link #clear} can be used to break the binding.
384
+ *
385
+ * @param {module:utils/collection~Collection} externalCollection A collection to be bound.
386
+ * @returns {module:utils/collection~CollectionBindToChain} The binding chain object.
387
+ */
388
+ bindTo(externalCollection) {
389
+ if (this._bindToCollection) {
390
+ /**
391
+ * The collection cannot be bound more than once.
392
+ *
393
+ * @error collection-bind-to-rebind
394
+ */
395
+ throw new CKEditorError('collection-bind-to-rebind', this);
396
+ }
397
+ this._bindToCollection = externalCollection;
398
+ return {
399
+ as: Class => {
400
+ this._setUpBindToBinding(item => new Class(item));
401
+ },
402
+ using: callbackOrProperty => {
403
+ if (typeof callbackOrProperty == 'function') {
404
+ this._setUpBindToBinding(callbackOrProperty);
405
+ }
406
+ else {
407
+ this._setUpBindToBinding(item => item[callbackOrProperty]);
408
+ }
409
+ }
410
+ };
411
+ }
412
+ /**
413
+ * Finalizes and activates a binding initiated by {#bindTo}.
414
+ *
415
+ * @private
416
+ * @param {Function} factory A function which produces collection items.
417
+ */
418
+ _setUpBindToBinding(factory) {
419
+ const externalCollection = this._bindToCollection;
420
+ // Adds the item to the collection once a change has been done to the external collection.
421
+ //
422
+ // @private
423
+ const addItem = (evt, externalItem, index) => {
424
+ const isExternalBoundToThis = externalCollection._bindToCollection == this;
425
+ const externalItemBound = externalCollection._bindToInternalToExternalMap.get(externalItem);
426
+ // If an external collection is bound to this collection, which makes it a 2–way binding,
427
+ // and the particular external collection item is already bound, don't add it here.
428
+ // The external item has been created **out of this collection's item** and (re)adding it will
429
+ // cause a loop.
430
+ if (isExternalBoundToThis && externalItemBound) {
431
+ this._bindToExternalToInternalMap.set(externalItem, externalItemBound);
432
+ this._bindToInternalToExternalMap.set(externalItemBound, externalItem);
433
+ }
434
+ else {
435
+ const item = factory(externalItem);
436
+ // When there is no item we need to remember skipped index first and then we can skip this item.
437
+ if (!item) {
438
+ this._skippedIndexesFromExternal.push(index);
439
+ return;
440
+ }
441
+ // Lets try to put item at the same index as index in external collection
442
+ // but when there are a skipped items in one or both collections we need to recalculate this index.
443
+ let finalIndex = index;
444
+ // When we try to insert item after some skipped items from external collection we need
445
+ // to include this skipped items and decrease index.
446
+ //
447
+ // For the following example:
448
+ // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal' ]
449
+ // internal -> [ A ]
450
+ //
451
+ // Another item is been added at the end of external collection:
452
+ // external.add( 'D' )
453
+ // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal', 'D' ]
454
+ //
455
+ // We can't just add 'D' to internal at the same index as index in external because
456
+ // this will produce empty indexes what is invalid:
457
+ // internal -> [ 'A', empty, empty, 'D' ]
458
+ //
459
+ // So we need to include skipped items and decrease index
460
+ // internal -> [ 'A', 'D' ]
461
+ for (const skipped of this._skippedIndexesFromExternal) {
462
+ if (index > skipped) {
463
+ finalIndex--;
464
+ }
465
+ }
466
+ // We need to take into consideration that external collection could skip some items from
467
+ // internal collection.
468
+ //
469
+ // For the following example:
470
+ // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external' ]
471
+ // external -> [ A ]
472
+ //
473
+ // Another item is been added at the end of external collection:
474
+ // external.add( 'D' )
475
+ // external -> [ 'A', 'D' ]
476
+ //
477
+ // We need to include skipped items and place new item after them:
478
+ // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external', 'D' ]
479
+ for (const skipped of externalCollection._skippedIndexesFromExternal) {
480
+ if (finalIndex >= skipped) {
481
+ finalIndex++;
482
+ }
483
+ }
484
+ this._bindToExternalToInternalMap.set(externalItem, item);
485
+ this._bindToInternalToExternalMap.set(item, externalItem);
486
+ this.add(item, finalIndex);
487
+ // After adding new element to internal collection we need update indexes
488
+ // of skipped items in external collection.
489
+ for (let i = 0; i < externalCollection._skippedIndexesFromExternal.length; i++) {
490
+ if (finalIndex <= externalCollection._skippedIndexesFromExternal[i]) {
491
+ externalCollection._skippedIndexesFromExternal[i]++;
492
+ }
493
+ }
494
+ }
495
+ };
496
+ // Load the initial content of the collection.
497
+ for (const externalItem of externalCollection) {
498
+ addItem(null, externalItem, externalCollection.getIndex(externalItem));
499
+ }
500
+ // Synchronize the with collection as new items are added.
501
+ this.listenTo(externalCollection, 'add', addItem);
502
+ // Synchronize the with collection as new items are removed.
503
+ this.listenTo(externalCollection, 'remove', (evt, externalItem, index) => {
504
+ const item = this._bindToExternalToInternalMap.get(externalItem);
505
+ if (item) {
506
+ this.remove(item);
507
+ }
508
+ // After removing element from external collection we need update/remove indexes
509
+ // of skipped items in internal collection.
510
+ this._skippedIndexesFromExternal = this._skippedIndexesFromExternal.reduce((result, skipped) => {
511
+ if (index < skipped) {
512
+ result.push(skipped - 1);
513
+ }
514
+ if (index > skipped) {
515
+ result.push(skipped);
516
+ }
517
+ return result;
518
+ }, []);
519
+ });
520
+ }
521
+ /**
522
+ * Returns an unique id property for a given `item`.
523
+ *
524
+ * The method will generate new id and assign it to the `item` if it doesn't have any.
525
+ *
526
+ * @private
527
+ * @param {Object} item Item to be added.
528
+ * @returns {String}
529
+ */
530
+ _getItemIdBeforeAdding(item) {
531
+ const idProperty = this._idProperty;
532
+ let itemId;
533
+ if ((idProperty in item)) {
534
+ itemId = item[idProperty];
535
+ if (typeof itemId != 'string') {
536
+ /**
537
+ * This item's ID should be a string.
538
+ *
539
+ * @error collection-add-invalid-id
540
+ */
541
+ throw new CKEditorError('collection-add-invalid-id', this);
542
+ }
543
+ if (this.get(itemId)) {
544
+ /**
545
+ * This item already exists in the collection.
546
+ *
547
+ * @error collection-add-item-already-exists
548
+ */
549
+ throw new CKEditorError('collection-add-item-already-exists', this);
550
+ }
551
+ }
552
+ else {
553
+ item[idProperty] = itemId = uid();
554
+ }
555
+ return itemId;
556
+ }
557
+ /**
558
+ * Core {@link #remove} method implementation shared in other functions.
559
+ *
560
+ * In contrast this method **does not** fire the {@link #event:change} event.
561
+ *
562
+ * @private
563
+ * @param {Object|Number|String} subject The item to remove, its id or index in the collection.
564
+ * @returns {Array} Returns an array with the removed item and its index.
565
+ * @fires remove
566
+ */
567
+ _remove(subject) {
568
+ let index, id, item;
569
+ let itemDoesNotExist = false;
570
+ const idProperty = this._idProperty;
571
+ if (typeof subject == 'string') {
572
+ id = subject;
573
+ item = this._itemMap.get(id);
574
+ itemDoesNotExist = !item;
575
+ if (item) {
576
+ index = this._items.indexOf(item);
577
+ }
578
+ }
579
+ else if (typeof subject == 'number') {
580
+ index = subject;
581
+ item = this._items[index];
582
+ itemDoesNotExist = !item;
583
+ if (item) {
584
+ id = item[idProperty];
585
+ }
586
+ }
587
+ else {
588
+ item = subject;
589
+ id = item[idProperty];
590
+ index = this._items.indexOf(item);
591
+ itemDoesNotExist = (index == -1 || !this._itemMap.get(id));
592
+ }
593
+ if (itemDoesNotExist) {
594
+ /**
595
+ * Item not found.
596
+ *
597
+ * @error collection-remove-404
598
+ */
599
+ throw new CKEditorError('collection-remove-404', this);
600
+ }
601
+ this._items.splice(index, 1);
602
+ this._itemMap.delete(id);
603
+ const externalItem = this._bindToInternalToExternalMap.get(item);
604
+ this._bindToInternalToExternalMap.delete(item);
605
+ this._bindToExternalToInternalMap.delete(externalItem);
606
+ this.fire('remove', item, index);
607
+ return [item, index];
608
+ }
609
+ /**
610
+ * Iterable interface.
611
+ *
612
+ * @returns {Iterator.<*>}
613
+ */
614
+ [Symbol.iterator]() {
615
+ return this._items[Symbol.iterator]();
616
+ }
617
+ }
618
+ mix(Collection, EmitterMixin);
619
+ export default Collection;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module utils/comparearrays
7
+ */
8
+ /**
9
+ * Compares how given arrays relate to each other. One array can be: same as another array, prefix of another array
10
+ * or completely different. If arrays are different, first index at which they differ is returned. Otherwise,
11
+ * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
12
+ * it means that arrays differ.
13
+ *
14
+ * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'same'
15
+ * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'prefix'
16
+ * compareArrays( [ 0, 2 ], [ 0 ] ); // 'extension'
17
+ * compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
18
+ * compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
19
+ *
20
+ * @param {Array} a Array that is compared.
21
+ * @param {Array} b Array to compare with.
22
+ * @returns {module:utils/comparearrays~ArrayRelation|Number} How array `a` is related to `b`.
23
+ */
24
+ export default function compareArrays(a, b) {
25
+ const minLen = Math.min(a.length, b.length);
26
+ for (let i = 0; i < minLen; i++) {
27
+ if (a[i] != b[i]) {
28
+ // The arrays are different.
29
+ return i;
30
+ }
31
+ }
32
+ // Both arrays were same at all points.
33
+ if (a.length == b.length) {
34
+ // If their length is also same, they are the same.
35
+ return 'same';
36
+ }
37
+ else if (a.length < b.length) {
38
+ // Compared array is shorter so it is a prefix of the other array.
39
+ return 'prefix';
40
+ }
41
+ else {
42
+ // Compared array is longer so it is an extension of the other array.
43
+ return 'extension';
44
+ }
45
+ }