@ckeditor/ckeditor5-utils 34.2.0 → 35.1.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 (61) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/LICENSE.md +1 -1
  3. package/package.json +19 -8
  4. package/src/areconnectedthroughproperties.js +54 -71
  5. package/src/ckeditorerror.js +92 -114
  6. package/src/collection.js +594 -762
  7. package/src/comparearrays.js +22 -28
  8. package/src/config.js +193 -223
  9. package/src/count.js +8 -12
  10. package/src/diff.js +85 -110
  11. package/src/difftochanges.js +47 -57
  12. package/src/dom/createelement.js +17 -25
  13. package/src/dom/emittermixin.js +202 -263
  14. package/src/dom/getancestors.js +9 -13
  15. package/src/dom/getborderwidths.js +10 -13
  16. package/src/dom/getcommonancestor.js +9 -15
  17. package/src/dom/getdatafromelement.js +5 -9
  18. package/src/dom/getpositionedancestor.js +9 -14
  19. package/src/dom/global.js +15 -4
  20. package/src/dom/indexof.js +7 -11
  21. package/src/dom/insertat.js +2 -4
  22. package/src/dom/iscomment.js +2 -5
  23. package/src/dom/isnode.js +10 -12
  24. package/src/dom/isrange.js +2 -4
  25. package/src/dom/istext.js +2 -4
  26. package/src/dom/isvisible.js +2 -4
  27. package/src/dom/iswindow.js +11 -16
  28. package/src/dom/position.js +220 -410
  29. package/src/dom/rect.js +335 -414
  30. package/src/dom/remove.js +5 -8
  31. package/src/dom/resizeobserver.js +109 -342
  32. package/src/dom/scroll.js +151 -183
  33. package/src/dom/setdatainelement.js +5 -9
  34. package/src/dom/tounit.js +10 -12
  35. package/src/elementreplacer.js +30 -44
  36. package/src/emittermixin.js +368 -634
  37. package/src/env.js +109 -116
  38. package/src/eventinfo.js +12 -65
  39. package/src/fastdiff.js +96 -128
  40. package/src/first.js +8 -12
  41. package/src/focustracker.js +77 -133
  42. package/src/index.js +0 -9
  43. package/src/inserttopriorityarray.js +9 -30
  44. package/src/isiterable.js +2 -4
  45. package/src/keyboard.js +117 -196
  46. package/src/keystrokehandler.js +72 -88
  47. package/src/language.js +9 -15
  48. package/src/locale.js +61 -158
  49. package/src/mapsequal.js +12 -17
  50. package/src/mix.js +17 -16
  51. package/src/nth.js +8 -11
  52. package/src/objecttomap.js +7 -11
  53. package/src/observablemixin.js +474 -778
  54. package/src/priorities.js +20 -32
  55. package/src/spy.js +3 -6
  56. package/src/toarray.js +2 -13
  57. package/src/tomap.js +8 -10
  58. package/src/translation-service.js +57 -93
  59. package/src/uid.js +34 -38
  60. package/src/unicode.js +28 -43
  61. package/src/version.js +134 -143
package/src/diff.js CHANGED
@@ -2,16 +2,12 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/diff
8
7
  */
9
-
10
- import fastDiff from '../src/fastdiff';
11
-
8
+ import fastDiff from './fastdiff';
12
9
  // The following code is based on the "O(NP) Sequence Comparison Algorithm"
13
10
  // by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
14
-
15
11
  /**
16
12
  * Calculates the difference between two arrays or strings producing an array containing a list of changes
17
13
  * necessary to transform input into output.
@@ -26,113 +22,92 @@ import fastDiff from '../src/fastdiff';
26
22
  * @param {Array|String} a Input array or string.
27
23
  * @param {Array|String} b Output array or string.
28
24
  * @param {Function} [cmp] Optional function used to compare array values, by default === is used.
29
- * @returns {Array} Array of changes.
25
+ * @returns {Array.<module:utils/diff~DiffResult>} Array of changes.
30
26
  */
31
- export default function diff( a, b, cmp ) {
32
- // Set the comparator function.
33
- cmp = cmp || function( a, b ) {
34
- return a === b;
35
- };
36
-
37
- const aLength = a.length;
38
- const bLength = b.length;
39
-
40
- // Perform `fastDiff` for longer strings/arrays (see #269).
41
- if ( aLength > 200 || bLength > 200 || aLength + bLength > 300 ) {
42
- return diff.fastDiff( a, b, cmp, true );
43
- }
44
-
45
- // Temporary action type statics.
46
- let _insert, _delete;
47
-
48
- // Swapped the arrays to use the shorter one as the first one.
49
- if ( bLength < aLength ) {
50
- const tmp = a;
51
-
52
- a = b;
53
- b = tmp;
54
-
55
- // We swap the action types as well.
56
- _insert = 'delete';
57
- _delete = 'insert';
58
- } else {
59
- _insert = 'insert';
60
- _delete = 'delete';
61
- }
62
-
63
- const m = a.length;
64
- const n = b.length;
65
- const delta = n - m;
66
-
67
- // Edit scripts, for each diagonal.
68
- const es = {};
69
- // Furthest points, the furthest y we can get on each diagonal.
70
- const fp = {};
71
-
72
- function snake( k ) {
73
- // We use -1 as an alternative below to handle initial values ( instead of filling the fp with -1 first ).
74
- // Furthest points (y) on the diagonal below k.
75
- const y1 = ( fp[ k - 1 ] !== undefined ? fp[ k - 1 ] : -1 ) + 1;
76
- // Furthest points (y) on the diagonal above k.
77
- const y2 = fp[ k + 1 ] !== undefined ? fp[ k + 1 ] : -1;
78
- // The way we should go to get further.
79
- const dir = y1 > y2 ? -1 : 1;
80
-
81
- // Clone previous changes array (if any).
82
- if ( es[ k + dir ] ) {
83
- es[ k ] = es[ k + dir ].slice( 0 );
84
- }
85
-
86
- // Create changes array.
87
- if ( !es[ k ] ) {
88
- es[ k ] = [];
89
- }
90
-
91
- // Push the action.
92
- es[ k ].push( y1 > y2 ? _insert : _delete );
93
-
94
- // Set the beginning coordinates.
95
- let y = Math.max( y1, y2 );
96
- let x = y - k;
97
-
98
- // Traverse the diagonal as long as the values match.
99
- while ( x < m && y < n && cmp( a[ x ], b[ y ] ) ) {
100
- x++;
101
- y++;
102
- // Push no change action.
103
- es[ k ].push( 'equal' );
104
- }
105
-
106
- return y;
107
- }
108
-
109
- let p = 0;
110
- let k;
111
-
112
- // Traverse the graph until we reach the end of the longer string.
113
- do {
114
- // Updates furthest points and edit scripts for diagonals below delta.
115
- for ( k = -p; k < delta; k++ ) {
116
- fp[ k ] = snake( k );
117
- }
118
-
119
- // Updates furthest points and edit scripts for diagonals above delta.
120
- for ( k = delta + p; k > delta; k-- ) {
121
- fp[ k ] = snake( k );
122
- }
123
-
124
- // Updates furthest point and edit script for the delta diagonal.
125
- // note that the delta diagonal is the one which goes through the sink (m, n).
126
- fp[ delta ] = snake( delta );
127
-
128
- p++;
129
- } while ( fp[ delta ] !== n );
130
-
131
- // Return the final list of edit changes.
132
- // We remove the first item that represents the action for the injected nulls.
133
- return es[ delta ].slice( 1 );
27
+ export default function diff(a, b, cmp) {
28
+ // Set the comparator function.
29
+ cmp = cmp || function (a, b) {
30
+ return a === b;
31
+ };
32
+ const aLength = a.length;
33
+ const bLength = b.length;
34
+ // Perform `fastDiff` for longer strings/arrays (see #269).
35
+ if (aLength > 200 || bLength > 200 || aLength + bLength > 300) {
36
+ return diff.fastDiff(a, b, cmp, true);
37
+ }
38
+ // Temporary action type statics.
39
+ let _insert, _delete;
40
+ // Swapped the arrays to use the shorter one as the first one.
41
+ if (bLength < aLength) {
42
+ const tmp = a;
43
+ a = b;
44
+ b = tmp;
45
+ // We swap the action types as well.
46
+ _insert = 'delete';
47
+ _delete = 'insert';
48
+ }
49
+ else {
50
+ _insert = 'insert';
51
+ _delete = 'delete';
52
+ }
53
+ const m = a.length;
54
+ const n = b.length;
55
+ const delta = n - m;
56
+ // Edit scripts, for each diagonal.
57
+ const es = {};
58
+ // Furthest points, the furthest y we can get on each diagonal.
59
+ const fp = {};
60
+ function snake(k) {
61
+ // We use -1 as an alternative below to handle initial values ( instead of filling the fp with -1 first ).
62
+ // Furthest points (y) on the diagonal below k.
63
+ const y1 = (fp[k - 1] !== undefined ? fp[k - 1] : -1) + 1;
64
+ // Furthest points (y) on the diagonal above k.
65
+ const y2 = fp[k + 1] !== undefined ? fp[k + 1] : -1;
66
+ // The way we should go to get further.
67
+ const dir = y1 > y2 ? -1 : 1;
68
+ // Clone previous changes array (if any).
69
+ if (es[k + dir]) {
70
+ es[k] = es[k + dir].slice(0);
71
+ }
72
+ // Create changes array.
73
+ if (!es[k]) {
74
+ es[k] = [];
75
+ }
76
+ // Push the action.
77
+ es[k].push(y1 > y2 ? _insert : _delete);
78
+ // Set the beginning coordinates.
79
+ let y = Math.max(y1, y2);
80
+ let x = y - k;
81
+ // Traverse the diagonal as long as the values match.
82
+ while (x < m && y < n && cmp(a[x], b[y])) {
83
+ x++;
84
+ y++;
85
+ // Push no change action.
86
+ es[k].push('equal');
87
+ }
88
+ return y;
89
+ }
90
+ let p = 0;
91
+ let k;
92
+ // Traverse the graph until we reach the end of the longer string.
93
+ do {
94
+ // Updates furthest points and edit scripts for diagonals below delta.
95
+ for (k = -p; k < delta; k++) {
96
+ fp[k] = snake(k);
97
+ }
98
+ // Updates furthest points and edit scripts for diagonals above delta.
99
+ for (k = delta + p; k > delta; k--) {
100
+ fp[k] = snake(k);
101
+ }
102
+ // Updates furthest point and edit script for the delta diagonal.
103
+ // note that the delta diagonal is the one which goes through the sink (m, n).
104
+ fp[delta] = snake(delta);
105
+ p++;
106
+ } while (fp[delta] !== n);
107
+ // Return the final list of edit changes.
108
+ // We remove the first item that represents the action for the injected nulls.
109
+ return es[delta].slice(1);
134
110
  }
135
-
136
111
  // Store the API in static property to easily overwrite it in tests.
137
112
  // Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
138
113
  diff.fastDiff = fastDiff;
@@ -2,11 +2,9 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/difftochanges
8
7
  */
9
-
10
8
  /**
11
9
  * Creates a set of changes which need to be applied to the input in order to transform
12
10
  * it into the output. This function can be used with strings or arrays.
@@ -25,62 +23,54 @@
25
23
  *
26
24
  * input.join( '' ) == output.join( '' ); // -> true
27
25
  *
28
- * @param {Array.<'equal'|'insert'|'delete'>} diff Result of {@link module:utils/diff~diff}.
26
+ * @param {Array.<module:utils/diff~DiffResult>} diff Result of {@link module:utils/diff~diff}.
29
27
  * @param {String|Array} output The string or array which was passed as diff's output.
30
- * @returns {Array.<Object>} Set of changes (insert or delete) which need to be applied to the input
28
+ * @returns {Array.<module:utils/difftochanges~Change>} Set of changes (insert or delete) which need to be applied to the input
31
29
  * in order to transform it into the output.
32
30
  */
33
- export default function diffToChanges( diff, output ) {
34
- const changes = [];
35
- let index = 0;
36
- let lastOperation;
37
-
38
- diff.forEach( change => {
39
- if ( change == 'equal' ) {
40
- pushLast();
41
-
42
- index++;
43
- } else if ( change == 'insert' ) {
44
- if ( isContinuationOf( 'insert' ) ) {
45
- lastOperation.values.push( output[ index ] );
46
- } else {
47
- pushLast();
48
-
49
- lastOperation = {
50
- type: 'insert',
51
- index,
52
- values: [ output[ index ] ]
53
- };
54
- }
55
-
56
- index++;
57
- } else /* if ( change == 'delete' ) */ {
58
- if ( isContinuationOf( 'delete' ) ) {
59
- lastOperation.howMany++;
60
- } else {
61
- pushLast();
62
-
63
- lastOperation = {
64
- type: 'delete',
65
- index,
66
- howMany: 1
67
- };
68
- }
69
- }
70
- } );
71
-
72
- pushLast();
73
-
74
- return changes;
75
-
76
- function pushLast() {
77
- if ( lastOperation ) {
78
- changes.push( lastOperation );
79
- lastOperation = null;
80
- }
81
- }
82
-
83
- function isContinuationOf( expected ) {
84
- return lastOperation && lastOperation.type == expected;
85
- }
31
+ export default function diffToChanges(diff, output) {
32
+ const changes = [];
33
+ let index = 0;
34
+ let lastOperation = null;
35
+ diff.forEach(change => {
36
+ if (change == 'equal') {
37
+ pushLast();
38
+ index++;
39
+ }
40
+ else if (change == 'insert') {
41
+ if (lastOperation && lastOperation.type == 'insert') {
42
+ lastOperation.values.push(output[index]);
43
+ }
44
+ else {
45
+ pushLast();
46
+ lastOperation = {
47
+ type: 'insert',
48
+ index,
49
+ values: [output[index]]
50
+ };
51
+ }
52
+ index++;
53
+ }
54
+ else /* if ( change == 'delete' ) */ {
55
+ if (lastOperation && lastOperation.type == 'delete') {
56
+ lastOperation.howMany++;
57
+ }
58
+ else {
59
+ pushLast();
60
+ lastOperation = {
61
+ type: 'delete',
62
+ index,
63
+ howMany: 1
64
+ };
65
+ }
66
+ }
67
+ });
68
+ pushLast();
69
+ return changes;
70
+ function pushLast() {
71
+ if (lastOperation) {
72
+ changes.push(lastOperation);
73
+ lastOperation = null;
74
+ }
75
+ }
86
76
  }
@@ -2,14 +2,11 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/dom/createelement
8
7
  */
9
-
10
8
  import isIterable from '../isiterable';
11
9
  import { isString } from 'lodash-es';
12
-
13
10
  /**
14
11
  * Creates element with attributes and children.
15
12
  *
@@ -21,29 +18,24 @@ import { isString } from 'lodash-es';
21
18
  * @param {Document} doc Document used to create element.
22
19
  * @param {String} name Name of the element.
23
20
  * @param {Object} [attributes] Object keys will become attributes keys and object values will became attributes values.
24
- * @param {Node|String|Array.<Node|String>} [children] Child or array of children. Strings will be automatically turned
21
+ * @param {Node|String|Iterable.<Node|String>} [children] Child or any iterable of children. Strings will be automatically turned
25
22
  * into Text nodes.
26
23
  * @returns {Element} Created element.
27
24
  */
28
- export default function createElement( doc, name, attributes = {}, children = [] ) {
29
- const namespace = attributes && attributes.xmlns;
30
- const element = namespace ? doc.createElementNS( namespace, name ) : doc.createElement( name );
31
-
32
- for ( const key in attributes ) {
33
- element.setAttribute( key, attributes[ key ] );
34
- }
35
-
36
- if ( isString( children ) || !isIterable( children ) ) {
37
- children = [ children ];
38
- }
39
-
40
- for ( let child of children ) {
41
- if ( isString( child ) ) {
42
- child = doc.createTextNode( child );
43
- }
44
-
45
- element.appendChild( child );
46
- }
47
-
48
- return element;
25
+ export default function createElement(doc, name, attributes = {}, children = []) {
26
+ const namespace = attributes && attributes.xmlns;
27
+ const element = namespace ? doc.createElementNS(namespace, name) : doc.createElement(name);
28
+ for (const key in attributes) {
29
+ element.setAttribute(key, attributes[key]);
30
+ }
31
+ if (isString(children) || !isIterable(children)) {
32
+ children = [children];
33
+ }
34
+ for (let child of children) {
35
+ if (isString(child)) {
36
+ child = doc.createTextNode(child);
37
+ }
38
+ element.appendChild(child);
39
+ }
40
+ return element;
49
41
  }