@ckeditor/ckeditor5-utils 40.0.0 → 40.2.0

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 (135) hide show
  1. package/CHANGELOG.md +26 -26
  2. package/LICENSE.md +3 -3
  3. package/package.json +1 -1
  4. package/src/abortabledebounce.d.ts +17 -0
  5. package/src/abortabledebounce.js +22 -0
  6. package/src/areconnectedthroughproperties.d.ts +11 -11
  7. package/src/areconnectedthroughproperties.js +73 -73
  8. package/src/ckeditorerror.d.ts +123 -123
  9. package/src/ckeditorerror.js +176 -176
  10. package/src/collection.d.ts +433 -427
  11. package/src/collection.js +583 -575
  12. package/src/comparearrays.d.ts +30 -30
  13. package/src/comparearrays.js +47 -47
  14. package/src/config.d.ts +163 -163
  15. package/src/config.js +163 -162
  16. package/src/count.d.ts +18 -18
  17. package/src/count.js +24 -24
  18. package/src/delay.d.ts +19 -19
  19. package/src/delay.js +26 -26
  20. package/src/diff.d.ts +31 -31
  21. package/src/diff.js +115 -115
  22. package/src/difftochanges.d.ts +59 -59
  23. package/src/difftochanges.js +79 -79
  24. package/src/dom/createelement.d.ts +57 -57
  25. package/src/dom/createelement.js +40 -40
  26. package/src/dom/emittermixin.d.ts +142 -142
  27. package/src/dom/emittermixin.js +239 -239
  28. package/src/dom/findclosestscrollableancestor.d.ts +11 -11
  29. package/src/dom/findclosestscrollableancestor.js +31 -31
  30. package/src/dom/getancestors.d.ts +17 -17
  31. package/src/dom/getancestors.js +27 -27
  32. package/src/dom/getborderwidths.d.ts +24 -24
  33. package/src/dom/getborderwidths.js +24 -24
  34. package/src/dom/getcommonancestor.d.ts +12 -12
  35. package/src/dom/getcommonancestor.js +25 -25
  36. package/src/dom/getdatafromelement.d.ts +14 -14
  37. package/src/dom/getdatafromelement.js +20 -20
  38. package/src/dom/getpositionedancestor.d.ts +10 -10
  39. package/src/dom/getpositionedancestor.js +22 -22
  40. package/src/dom/global.d.ts +32 -32
  41. package/src/dom/global.js +35 -35
  42. package/src/dom/indexof.d.ts +14 -14
  43. package/src/dom/indexof.js +21 -21
  44. package/src/dom/insertat.d.ts +15 -15
  45. package/src/dom/insertat.js +17 -17
  46. package/src/dom/iscomment.d.ts +11 -11
  47. package/src/dom/iscomment.js +14 -14
  48. package/src/dom/isnode.d.ts +11 -11
  49. package/src/dom/isnode.js +21 -21
  50. package/src/dom/isrange.d.ts +11 -11
  51. package/src/dom/isrange.js +13 -13
  52. package/src/dom/istext.d.ts +11 -11
  53. package/src/dom/istext.js +13 -13
  54. package/src/dom/isvalidattributename.d.ts +10 -10
  55. package/src/dom/isvalidattributename.js +22 -22
  56. package/src/dom/isvisible.d.ts +18 -18
  57. package/src/dom/isvisible.js +20 -20
  58. package/src/dom/iswindow.d.ts +11 -11
  59. package/src/dom/iswindow.js +22 -22
  60. package/src/dom/position.d.ts +211 -211
  61. package/src/dom/position.js +313 -313
  62. package/src/dom/rect.d.ts +195 -195
  63. package/src/dom/rect.js +474 -474
  64. package/src/dom/remove.d.ts +13 -13
  65. package/src/dom/remove.js +18 -18
  66. package/src/dom/resizeobserver.d.ts +74 -74
  67. package/src/dom/resizeobserver.js +126 -126
  68. package/src/dom/scroll.d.ts +73 -73
  69. package/src/dom/scroll.js +383 -383
  70. package/src/dom/setdatainelement.d.ts +14 -14
  71. package/src/dom/setdatainelement.js +20 -20
  72. package/src/dom/tounit.d.ts +22 -22
  73. package/src/dom/tounit.js +16 -16
  74. package/src/elementreplacer.d.ts +31 -31
  75. package/src/elementreplacer.js +43 -43
  76. package/src/emittermixin.d.ts +312 -312
  77. package/src/emittermixin.js +453 -453
  78. package/src/env.d.ts +117 -117
  79. package/src/env.js +122 -122
  80. package/src/eventinfo.d.ts +58 -58
  81. package/src/eventinfo.js +26 -26
  82. package/src/fastdiff.d.ts +112 -112
  83. package/src/fastdiff.js +248 -248
  84. package/src/first.d.ts +11 -11
  85. package/src/first.js +17 -17
  86. package/src/focustracker.d.ts +75 -75
  87. package/src/focustracker.js +95 -95
  88. package/src/index.d.ts +64 -61
  89. package/src/index.js +63 -60
  90. package/src/inserttopriorityarray.d.ts +30 -30
  91. package/src/inserttopriorityarray.js +21 -21
  92. package/src/isiterable.d.ts +14 -14
  93. package/src/isiterable.js +16 -16
  94. package/src/keyboard.d.ts +126 -126
  95. package/src/keyboard.js +221 -221
  96. package/src/keystrokehandler.d.ts +87 -87
  97. package/src/keystrokehandler.js +122 -122
  98. package/src/language.d.ts +17 -17
  99. package/src/language.js +19 -19
  100. package/src/locale.d.ts +120 -120
  101. package/src/locale.js +76 -76
  102. package/src/mapsequal.d.ts +15 -15
  103. package/src/mapsequal.js +27 -27
  104. package/src/mix.d.ts +85 -85
  105. package/src/mix.js +50 -50
  106. package/src/nth.d.ts +16 -16
  107. package/src/nth.js +24 -24
  108. package/src/objecttomap.d.ts +23 -23
  109. package/src/objecttomap.js +27 -27
  110. package/src/observablemixin.d.ts +560 -560
  111. package/src/observablemixin.js +580 -580
  112. package/src/priorities.d.ts +33 -33
  113. package/src/priorities.js +23 -23
  114. package/src/retry.d.ts +33 -0
  115. package/src/retry.js +47 -0
  116. package/src/splicearray.d.ts +26 -26
  117. package/src/splicearray.js +40 -40
  118. package/src/spy.d.ts +21 -21
  119. package/src/spy.js +22 -22
  120. package/src/toarray.d.ts +25 -25
  121. package/src/toarray.js +7 -7
  122. package/src/tomap.d.ts +19 -19
  123. package/src/tomap.js +29 -29
  124. package/src/translation-service.d.ts +168 -168
  125. package/src/translation-service.js +198 -198
  126. package/src/uid.d.ts +15 -15
  127. package/src/uid.js +57 -57
  128. package/src/unicode.d.ts +54 -54
  129. package/src/unicode.js +85 -85
  130. package/src/verifylicense.d.ts +15 -15
  131. package/src/verifylicense.js +87 -87
  132. package/src/version.d.ts +10 -10
  133. package/src/version.js +153 -153
  134. package/src/wait.d.ts +16 -0
  135. package/src/wait.js +29 -0
package/src/fastdiff.js CHANGED
@@ -1,248 +1,248 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/fastdiff
7
- */
8
- /**
9
- * Finds positions of the first and last change in the given string/array and generates a set of changes:
10
- *
11
- * ```ts
12
- * fastDiff( '12a', '12xyza' );
13
- * // [ { index: 2, type: 'insert', values: [ 'x', 'y', 'z' ] } ]
14
- *
15
- * fastDiff( '12a', '12aa' );
16
- * // [ { index: 3, type: 'insert', values: [ 'a' ] } ]
17
- *
18
- * fastDiff( '12xyza', '12a' );
19
- * // [ { index: 2, type: 'delete', howMany: 3 } ]
20
- *
21
- * fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
22
- * // [ { index: 3, type: 'delete', howMany: 1 } ]
23
- *
24
- * fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
25
- * // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
26
- * ```
27
- *
28
- * Passed arrays can contain any type of data, however to compare them correctly custom comparator function
29
- * should be passed as a third parameter:
30
- *
31
- * ```ts
32
- * fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
33
- * return a.value === b.value;
34
- * } );
35
- * // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
36
- * ```
37
- *
38
- * The resulted set of changes can be applied to the input in order to transform it into the output, for example:
39
- *
40
- * ```ts
41
- * let input = '12abc3';
42
- * const output = '2ab';
43
- * const changes = fastDiff( input, output );
44
- *
45
- * changes.forEach( change => {
46
- * if ( change.type == 'insert' ) {
47
- * input = input.substring( 0, change.index ) + change.values.join( '' ) + input.substring( change.index );
48
- * } else if ( change.type == 'delete' ) {
49
- * input = input.substring( 0, change.index ) + input.substring( change.index + change.howMany );
50
- * }
51
- * } );
52
- *
53
- * // input equals output now
54
- * ```
55
- *
56
- * or in case of arrays:
57
- *
58
- * ```ts
59
- * let input = [ '1', '2', 'a', 'b', 'c', '3' ];
60
- * const output = [ '2', 'a', 'b' ];
61
- * const changes = fastDiff( input, output );
62
- *
63
- * changes.forEach( change => {
64
- * if ( change.type == 'insert' ) {
65
- * input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
66
- * } else if ( change.type == 'delete' ) {
67
- * input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
68
- * }
69
- * } );
70
- *
71
- * // input equals output now
72
- * ```
73
- *
74
- * By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
75
- * the {@link module:utils/diff~diff `diff()`} function:
76
- *
77
- * ```ts
78
- * fastDiff( '12a', '12xyza', undefined, true );
79
- * // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
80
- * ```
81
- *
82
- * The default output format of this function is compatible with the output format of
83
- * {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
84
- * compatible with the output of {@link module:utils/diff~diff `diff()`}:
85
- *
86
- * ```ts
87
- * const a = '1234';
88
- * const b = '12xyz34';
89
- *
90
- * // Both calls will return the same results (grouped changes format).
91
- * fastDiff( a, b );
92
- * diffToChanges( diff( a, b ) );
93
- *
94
- * // Again, both calls will return the same results (atomic changes format).
95
- * fastDiff( a, b, undefined, true );
96
- * diff( a, b );
97
- * ```
98
- *
99
- * @typeParam T The type of array elements.
100
- * @typeParam AtomicChanges The type of `atomicChanges` parameter (selects the result type).
101
- * @param a Input array or string.
102
- * @param b Input array or string.
103
- * @param cmp Optional function used to compare array values, by default `===` (strict equal operator) is used.
104
- * @param atomicChanges Whether an array of `inset|delete|equal` operations should
105
- * be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
106
- * Defaults to `false`.
107
- * @returns Array of changes. The elements are either {@link module:utils/diff~DiffResult} or {@link module:utils/difftochanges~Change},
108
- * depending on `atomicChanges` parameter.
109
- */
110
- export default function fastDiff(a, b, cmp, atomicChanges) {
111
- // Set the comparator function.
112
- cmp = cmp || function (a, b) {
113
- return a === b;
114
- };
115
- // Convert the string (or any array-like object - eg. NodeList) to an array by using the slice() method because,
116
- // unlike Array.from(), it returns array of UTF-16 code units instead of the code points of a string.
117
- // One code point might be a surrogate pair of two code units. All text offsets are expected to be in code units.
118
- // See ckeditor/ckeditor5#3147.
119
- //
120
- // We need to make sure here that fastDiff() works identical to diff().
121
- const arrayA = Array.isArray(a) ? a : Array.prototype.slice.call(a);
122
- const arrayB = Array.isArray(b) ? b : Array.prototype.slice.call(b);
123
- // Find first and last change.
124
- const changeIndexes = findChangeBoundaryIndexes(arrayA, arrayB, cmp);
125
- // Transform into changes array.
126
- const result = atomicChanges ?
127
- changeIndexesToAtomicChanges(changeIndexes, arrayB.length) :
128
- changeIndexesToChanges(arrayB, changeIndexes);
129
- return result;
130
- }
131
- /**
132
- * Finds position of the first and last change in the given arrays. For example:
133
- *
134
- * ```ts
135
- * const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
136
- * console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
137
- * ```
138
- *
139
- * The above indexes means that in the first array the modified part is `1[23]4` and in the second array it is `1[342]4`.
140
- * Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
141
- * can be generated.
142
- */
143
- function findChangeBoundaryIndexes(arr1, arr2, cmp) {
144
- // Find the first difference between passed values.
145
- const firstIndex = findFirstDifferenceIndex(arr1, arr2, cmp);
146
- // If arrays are equal return -1 indexes object.
147
- if (firstIndex === -1) {
148
- return { firstIndex: -1, lastIndexOld: -1, lastIndexNew: -1 };
149
- }
150
- // Remove the common part of each value and reverse them to make it simpler to find the last difference between them.
151
- const oldArrayReversed = cutAndReverse(arr1, firstIndex);
152
- const newArrayReversed = cutAndReverse(arr2, firstIndex);
153
- // Find the first difference between reversed values.
154
- // It should be treated as "how many elements from the end the last difference occurred".
155
- //
156
- // For example:
157
- //
158
- // initial -> after cut -> reversed:
159
- // oldValue: '321ba' -> '21ba' -> 'ab12'
160
- // newValue: '31xba' -> '1xba' -> 'abx1'
161
- // lastIndex: -> 2
162
- //
163
- // So the last change occurred two characters from the end of the arrays.
164
- const lastIndex = findFirstDifferenceIndex(oldArrayReversed, newArrayReversed, cmp);
165
- // Use `lastIndex` to calculate proper offset, starting from the beginning (`lastIndex` kind of starts from the end).
166
- const lastIndexOld = arr1.length - lastIndex;
167
- const lastIndexNew = arr2.length - lastIndex;
168
- return { firstIndex, lastIndexOld, lastIndexNew };
169
- }
170
- /**
171
- * Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
172
- */
173
- function findFirstDifferenceIndex(arr1, arr2, cmp) {
174
- for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
175
- if (arr1[i] === undefined || arr2[i] === undefined || !cmp(arr1[i], arr2[i])) {
176
- return i;
177
- }
178
- }
179
- return -1; // Return -1 if arrays are equal.
180
- }
181
- /**
182
- * Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
183
- *
184
- * @param arr Array to be processed.
185
- * @param howMany How many elements from array beginning to remove.
186
- * @returns Shortened and reversed array.
187
- */
188
- function cutAndReverse(arr, howMany) {
189
- return arr.slice(howMany).reverse();
190
- }
191
- /**
192
- * Generates changes array based on change indexes from `findChangeBoundaryIndexes` function. This function will
193
- * generate array with 0 (no changes), 1 (deletion or insertion) or 2 records (insertion and deletion).
194
- *
195
- * @param newArray New array for which change indexes were calculated.
196
- * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
197
- * @returns Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
198
- */
199
- function changeIndexesToChanges(newArray, changeIndexes) {
200
- const result = [];
201
- const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
202
- // Order operations as 'insert', 'delete' array to keep compatibility with {@link module:utils/difftochanges~diffToChanges}
203
- // in most cases. However, 'diffToChanges' does not stick to any order so in some cases
204
- // (for example replacing '12345' with 'abcd') it will generate 'delete', 'insert' order.
205
- if (lastIndexNew - firstIndex > 0) {
206
- result.push({
207
- index: firstIndex,
208
- type: 'insert',
209
- values: newArray.slice(firstIndex, lastIndexNew)
210
- });
211
- }
212
- if (lastIndexOld - firstIndex > 0) {
213
- result.push({
214
- index: firstIndex + (lastIndexNew - firstIndex),
215
- type: 'delete',
216
- howMany: lastIndexOld - firstIndex
217
- });
218
- }
219
- return result;
220
- }
221
- /**
222
- * Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
223
- *
224
- * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
225
- * @param newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
226
- * @returns Array of changes compatible with {@link module:utils/diff~diff} format.
227
- */
228
- function changeIndexesToAtomicChanges(changeIndexes, newLength) {
229
- const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
230
- // No changes.
231
- if (firstIndex === -1) {
232
- return Array(newLength).fill('equal');
233
- }
234
- let result = [];
235
- if (firstIndex > 0) {
236
- result = result.concat(Array(firstIndex).fill('equal'));
237
- }
238
- if (lastIndexNew - firstIndex > 0) {
239
- result = result.concat(Array(lastIndexNew - firstIndex).fill('insert'));
240
- }
241
- if (lastIndexOld - firstIndex > 0) {
242
- result = result.concat(Array(lastIndexOld - firstIndex).fill('delete'));
243
- }
244
- if (lastIndexNew < newLength) {
245
- result = result.concat(Array(newLength - lastIndexNew).fill('equal'));
246
- }
247
- return result;
248
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/fastdiff
7
+ */
8
+ /**
9
+ * Finds positions of the first and last change in the given string/array and generates a set of changes:
10
+ *
11
+ * ```ts
12
+ * fastDiff( '12a', '12xyza' );
13
+ * // [ { index: 2, type: 'insert', values: [ 'x', 'y', 'z' ] } ]
14
+ *
15
+ * fastDiff( '12a', '12aa' );
16
+ * // [ { index: 3, type: 'insert', values: [ 'a' ] } ]
17
+ *
18
+ * fastDiff( '12xyza', '12a' );
19
+ * // [ { index: 2, type: 'delete', howMany: 3 } ]
20
+ *
21
+ * fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
22
+ * // [ { index: 3, type: 'delete', howMany: 1 } ]
23
+ *
24
+ * fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
25
+ * // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
26
+ * ```
27
+ *
28
+ * Passed arrays can contain any type of data, however to compare them correctly custom comparator function
29
+ * should be passed as a third parameter:
30
+ *
31
+ * ```ts
32
+ * fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
33
+ * return a.value === b.value;
34
+ * } );
35
+ * // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
36
+ * ```
37
+ *
38
+ * The resulted set of changes can be applied to the input in order to transform it into the output, for example:
39
+ *
40
+ * ```ts
41
+ * let input = '12abc3';
42
+ * const output = '2ab';
43
+ * const changes = fastDiff( input, output );
44
+ *
45
+ * changes.forEach( change => {
46
+ * if ( change.type == 'insert' ) {
47
+ * input = input.substring( 0, change.index ) + change.values.join( '' ) + input.substring( change.index );
48
+ * } else if ( change.type == 'delete' ) {
49
+ * input = input.substring( 0, change.index ) + input.substring( change.index + change.howMany );
50
+ * }
51
+ * } );
52
+ *
53
+ * // input equals output now
54
+ * ```
55
+ *
56
+ * or in case of arrays:
57
+ *
58
+ * ```ts
59
+ * let input = [ '1', '2', 'a', 'b', 'c', '3' ];
60
+ * const output = [ '2', 'a', 'b' ];
61
+ * const changes = fastDiff( input, output );
62
+ *
63
+ * changes.forEach( change => {
64
+ * if ( change.type == 'insert' ) {
65
+ * input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
66
+ * } else if ( change.type == 'delete' ) {
67
+ * input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
68
+ * }
69
+ * } );
70
+ *
71
+ * // input equals output now
72
+ * ```
73
+ *
74
+ * By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
75
+ * the {@link module:utils/diff~diff `diff()`} function:
76
+ *
77
+ * ```ts
78
+ * fastDiff( '12a', '12xyza', undefined, true );
79
+ * // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
80
+ * ```
81
+ *
82
+ * The default output format of this function is compatible with the output format of
83
+ * {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
84
+ * compatible with the output of {@link module:utils/diff~diff `diff()`}:
85
+ *
86
+ * ```ts
87
+ * const a = '1234';
88
+ * const b = '12xyz34';
89
+ *
90
+ * // Both calls will return the same results (grouped changes format).
91
+ * fastDiff( a, b );
92
+ * diffToChanges( diff( a, b ) );
93
+ *
94
+ * // Again, both calls will return the same results (atomic changes format).
95
+ * fastDiff( a, b, undefined, true );
96
+ * diff( a, b );
97
+ * ```
98
+ *
99
+ * @typeParam T The type of array elements.
100
+ * @typeParam AtomicChanges The type of `atomicChanges` parameter (selects the result type).
101
+ * @param a Input array or string.
102
+ * @param b Input array or string.
103
+ * @param cmp Optional function used to compare array values, by default `===` (strict equal operator) is used.
104
+ * @param atomicChanges Whether an array of `inset|delete|equal` operations should
105
+ * be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
106
+ * Defaults to `false`.
107
+ * @returns Array of changes. The elements are either {@link module:utils/diff~DiffResult} or {@link module:utils/difftochanges~Change},
108
+ * depending on `atomicChanges` parameter.
109
+ */
110
+ export default function fastDiff(a, b, cmp, atomicChanges) {
111
+ // Set the comparator function.
112
+ cmp = cmp || function (a, b) {
113
+ return a === b;
114
+ };
115
+ // Convert the string (or any array-like object - eg. NodeList) to an array by using the slice() method because,
116
+ // unlike Array.from(), it returns array of UTF-16 code units instead of the code points of a string.
117
+ // One code point might be a surrogate pair of two code units. All text offsets are expected to be in code units.
118
+ // See ckeditor/ckeditor5#3147.
119
+ //
120
+ // We need to make sure here that fastDiff() works identical to diff().
121
+ const arrayA = Array.isArray(a) ? a : Array.prototype.slice.call(a);
122
+ const arrayB = Array.isArray(b) ? b : Array.prototype.slice.call(b);
123
+ // Find first and last change.
124
+ const changeIndexes = findChangeBoundaryIndexes(arrayA, arrayB, cmp);
125
+ // Transform into changes array.
126
+ const result = atomicChanges ?
127
+ changeIndexesToAtomicChanges(changeIndexes, arrayB.length) :
128
+ changeIndexesToChanges(arrayB, changeIndexes);
129
+ return result;
130
+ }
131
+ /**
132
+ * Finds position of the first and last change in the given arrays. For example:
133
+ *
134
+ * ```ts
135
+ * const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
136
+ * console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
137
+ * ```
138
+ *
139
+ * The above indexes means that in the first array the modified part is `1[23]4` and in the second array it is `1[342]4`.
140
+ * Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
141
+ * can be generated.
142
+ */
143
+ function findChangeBoundaryIndexes(arr1, arr2, cmp) {
144
+ // Find the first difference between passed values.
145
+ const firstIndex = findFirstDifferenceIndex(arr1, arr2, cmp);
146
+ // If arrays are equal return -1 indexes object.
147
+ if (firstIndex === -1) {
148
+ return { firstIndex: -1, lastIndexOld: -1, lastIndexNew: -1 };
149
+ }
150
+ // Remove the common part of each value and reverse them to make it simpler to find the last difference between them.
151
+ const oldArrayReversed = cutAndReverse(arr1, firstIndex);
152
+ const newArrayReversed = cutAndReverse(arr2, firstIndex);
153
+ // Find the first difference between reversed values.
154
+ // It should be treated as "how many elements from the end the last difference occurred".
155
+ //
156
+ // For example:
157
+ //
158
+ // initial -> after cut -> reversed:
159
+ // oldValue: '321ba' -> '21ba' -> 'ab12'
160
+ // newValue: '31xba' -> '1xba' -> 'abx1'
161
+ // lastIndex: -> 2
162
+ //
163
+ // So the last change occurred two characters from the end of the arrays.
164
+ const lastIndex = findFirstDifferenceIndex(oldArrayReversed, newArrayReversed, cmp);
165
+ // Use `lastIndex` to calculate proper offset, starting from the beginning (`lastIndex` kind of starts from the end).
166
+ const lastIndexOld = arr1.length - lastIndex;
167
+ const lastIndexNew = arr2.length - lastIndex;
168
+ return { firstIndex, lastIndexOld, lastIndexNew };
169
+ }
170
+ /**
171
+ * Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
172
+ */
173
+ function findFirstDifferenceIndex(arr1, arr2, cmp) {
174
+ for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
175
+ if (arr1[i] === undefined || arr2[i] === undefined || !cmp(arr1[i], arr2[i])) {
176
+ return i;
177
+ }
178
+ }
179
+ return -1; // Return -1 if arrays are equal.
180
+ }
181
+ /**
182
+ * Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
183
+ *
184
+ * @param arr Array to be processed.
185
+ * @param howMany How many elements from array beginning to remove.
186
+ * @returns Shortened and reversed array.
187
+ */
188
+ function cutAndReverse(arr, howMany) {
189
+ return arr.slice(howMany).reverse();
190
+ }
191
+ /**
192
+ * Generates changes array based on change indexes from `findChangeBoundaryIndexes` function. This function will
193
+ * generate array with 0 (no changes), 1 (deletion or insertion) or 2 records (insertion and deletion).
194
+ *
195
+ * @param newArray New array for which change indexes were calculated.
196
+ * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
197
+ * @returns Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
198
+ */
199
+ function changeIndexesToChanges(newArray, changeIndexes) {
200
+ const result = [];
201
+ const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
202
+ // Order operations as 'insert', 'delete' array to keep compatibility with {@link module:utils/difftochanges~diffToChanges}
203
+ // in most cases. However, 'diffToChanges' does not stick to any order so in some cases
204
+ // (for example replacing '12345' with 'abcd') it will generate 'delete', 'insert' order.
205
+ if (lastIndexNew - firstIndex > 0) {
206
+ result.push({
207
+ index: firstIndex,
208
+ type: 'insert',
209
+ values: newArray.slice(firstIndex, lastIndexNew)
210
+ });
211
+ }
212
+ if (lastIndexOld - firstIndex > 0) {
213
+ result.push({
214
+ index: firstIndex + (lastIndexNew - firstIndex),
215
+ type: 'delete',
216
+ howMany: lastIndexOld - firstIndex
217
+ });
218
+ }
219
+ return result;
220
+ }
221
+ /**
222
+ * Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
223
+ *
224
+ * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
225
+ * @param newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
226
+ * @returns Array of changes compatible with {@link module:utils/diff~diff} format.
227
+ */
228
+ function changeIndexesToAtomicChanges(changeIndexes, newLength) {
229
+ const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
230
+ // No changes.
231
+ if (firstIndex === -1) {
232
+ return Array(newLength).fill('equal');
233
+ }
234
+ let result = [];
235
+ if (firstIndex > 0) {
236
+ result = result.concat(Array(firstIndex).fill('equal'));
237
+ }
238
+ if (lastIndexNew - firstIndex > 0) {
239
+ result = result.concat(Array(lastIndexNew - firstIndex).fill('insert'));
240
+ }
241
+ if (lastIndexOld - firstIndex > 0) {
242
+ result = result.concat(Array(lastIndexOld - firstIndex).fill('delete'));
243
+ }
244
+ if (lastIndexNew < newLength) {
245
+ result = result.concat(Array(newLength - lastIndexNew).fill('equal'));
246
+ }
247
+ return result;
248
+ }
package/src/first.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/first
7
- */
8
- /**
9
- * Returns first item of the given `iterator`.
10
- */
11
- export default function first<T>(iterator: Iterator<T>): T | null;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/first
7
+ */
8
+ /**
9
+ * Returns first item of the given `iterator`.
10
+ */
11
+ export default function first<T>(iterator: Iterator<T>): T | null;
package/src/first.js CHANGED
@@ -1,17 +1,17 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/first
7
- */
8
- /**
9
- * Returns first item of the given `iterator`.
10
- */
11
- export default function first(iterator) {
12
- const iteratorItem = iterator.next();
13
- if (iteratorItem.done) {
14
- return null;
15
- }
16
- return iteratorItem.value;
17
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/first
7
+ */
8
+ /**
9
+ * Returns first item of the given `iterator`.
10
+ */
11
+ export default function first(iterator) {
12
+ const iteratorItem = iterator.next();
13
+ if (iteratorItem.done) {
14
+ return null;
15
+ }
16
+ return iteratorItem.value;
17
+ }