@ckeditor/ckeditor5-utils 34.2.0 → 35.0.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.
- package/CHANGELOG.md +324 -0
- package/LICENSE.md +1 -1
- package/package.json +19 -8
- package/src/areconnectedthroughproperties.js +0 -92
- package/src/ckeditorerror.js +0 -217
- package/src/collection.js +0 -785
- package/src/comparearrays.js +0 -51
- package/src/config.js +0 -246
- package/src/count.js +0 -26
- package/src/diff.js +0 -138
- package/src/difftochanges.js +0 -86
- package/src/dom/createelement.js +0 -49
- package/src/dom/emittermixin.js +0 -341
- package/src/dom/getancestors.js +0 -31
- package/src/dom/getborderwidths.js +0 -27
- package/src/dom/getcommonancestor.js +0 -31
- package/src/dom/getdatafromelement.js +0 -24
- package/src/dom/getpositionedancestor.js +0 -28
- package/src/dom/global.js +0 -26
- package/src/dom/indexof.js +0 -25
- package/src/dom/insertat.js +0 -19
- package/src/dom/iscomment.js +0 -20
- package/src/dom/isnode.js +0 -26
- package/src/dom/isrange.js +0 -18
- package/src/dom/istext.js +0 -18
- package/src/dom/isvisible.js +0 -25
- package/src/dom/iswindow.js +0 -30
- package/src/dom/position.js +0 -518
- package/src/dom/rect.js +0 -443
- package/src/dom/remove.js +0 -21
- package/src/dom/resizeobserver.js +0 -378
- package/src/dom/scroll.js +0 -302
- package/src/dom/setdatainelement.js +0 -24
- package/src/dom/tounit.js +0 -27
- package/src/elementreplacer.js +0 -57
- package/src/emittermixin.js +0 -719
- package/src/env.js +0 -190
- package/src/eventinfo.js +0 -79
- package/src/fastdiff.js +0 -261
- package/src/first.js +0 -24
- package/src/focustracker.js +0 -157
- package/src/index.js +0 -45
- package/src/inserttopriorityarray.js +0 -42
- package/src/isiterable.js +0 -18
- package/src/keyboard.js +0 -301
- package/src/keystrokehandler.js +0 -130
- package/src/language.js +0 -26
- package/src/locale.js +0 -176
- package/src/mapsequal.js +0 -32
- package/src/mix.js +0 -47
- package/src/nth.js +0 -31
- package/src/objecttomap.js +0 -29
- package/src/observablemixin.js +0 -908
- package/src/priorities.js +0 -44
- package/src/spy.js +0 -25
- package/src/toarray.js +0 -18
- package/src/tomap.js +0 -29
- package/src/translation-service.js +0 -216
- package/src/uid.js +0 -59
- package/src/unicode.js +0 -106
- package/src/version.js +0 -157
package/src/env.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
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
|
-
/* globals navigator:false */
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @module utils/env
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const userAgent = navigator.userAgent.toLowerCase();
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A namespace containing environment and browser information.
|
|
16
|
-
*
|
|
17
|
-
* @namespace
|
|
18
|
-
*/
|
|
19
|
-
const env = {
|
|
20
|
-
/**
|
|
21
|
-
* Indicates that the application is running on Macintosh.
|
|
22
|
-
*
|
|
23
|
-
* @static
|
|
24
|
-
* @type {Boolean}
|
|
25
|
-
*/
|
|
26
|
-
isMac: isMac( userAgent ),
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Indicates that the application is running on Windows.
|
|
30
|
-
*
|
|
31
|
-
* @static
|
|
32
|
-
* @type {Boolean}
|
|
33
|
-
*/
|
|
34
|
-
isWindows: isWindows( userAgent ),
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Indicates that the application is running in Firefox (Gecko).
|
|
38
|
-
*
|
|
39
|
-
* @static
|
|
40
|
-
* @type {Boolean}
|
|
41
|
-
*/
|
|
42
|
-
isGecko: isGecko( userAgent ),
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Indicates that the application is running in Safari.
|
|
46
|
-
*
|
|
47
|
-
* @static
|
|
48
|
-
* @type {Boolean}
|
|
49
|
-
*/
|
|
50
|
-
isSafari: isSafari( userAgent ),
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Indicates the the application is running in iOS.
|
|
54
|
-
*
|
|
55
|
-
* @static
|
|
56
|
-
* @type {Boolean}
|
|
57
|
-
*/
|
|
58
|
-
isiOS: isiOS( userAgent ),
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Indicates that the application is running on Android mobile device.
|
|
62
|
-
*
|
|
63
|
-
* @static
|
|
64
|
-
* @type {Boolean}
|
|
65
|
-
*/
|
|
66
|
-
isAndroid: isAndroid( userAgent ),
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Indicates that the application is running in a browser using the Blink engine.
|
|
70
|
-
*
|
|
71
|
-
* @static
|
|
72
|
-
* @type {Boolean}
|
|
73
|
-
*/
|
|
74
|
-
isBlink: isBlink( userAgent ),
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Environment features information.
|
|
78
|
-
*
|
|
79
|
-
* @memberOf module:utils/env~env
|
|
80
|
-
* @namespace
|
|
81
|
-
*/
|
|
82
|
-
features: {
|
|
83
|
-
/**
|
|
84
|
-
* Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
|
|
85
|
-
* More information about unicode properties might be found
|
|
86
|
-
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
87
|
-
*
|
|
88
|
-
* @type {Boolean}
|
|
89
|
-
*/
|
|
90
|
-
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
export default env;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Checks if User Agent represented by the string is running on Macintosh.
|
|
98
|
-
*
|
|
99
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
100
|
-
* @returns {Boolean} Whether User Agent is running on Macintosh or not.
|
|
101
|
-
*/
|
|
102
|
-
export function isMac( userAgent ) {
|
|
103
|
-
return userAgent.indexOf( 'macintosh' ) > -1;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Checks if User Agent represented by the string is running on Windows.
|
|
108
|
-
*
|
|
109
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
110
|
-
* @returns {Boolean} Whether User Agent is running on Windows or not.
|
|
111
|
-
*/
|
|
112
|
-
export function isWindows( userAgent ) {
|
|
113
|
-
return userAgent.indexOf( 'windows' ) > -1;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Checks if User Agent represented by the string is Firefox (Gecko).
|
|
118
|
-
*
|
|
119
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
120
|
-
* @returns {Boolean} Whether User Agent is Firefox or not.
|
|
121
|
-
*/
|
|
122
|
-
export function isGecko( userAgent ) {
|
|
123
|
-
return !!userAgent.match( /gecko\/\d+/ );
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Checks if User Agent represented by the string is Safari.
|
|
128
|
-
*
|
|
129
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
130
|
-
* @returns {Boolean} Whether User Agent is Safari or not.
|
|
131
|
-
*/
|
|
132
|
-
export function isSafari( userAgent ) {
|
|
133
|
-
return userAgent.indexOf( ' applewebkit/' ) > -1 && userAgent.indexOf( 'chrome' ) === -1;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Checks if User Agent represented by the string is running in iOS.
|
|
138
|
-
*
|
|
139
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
140
|
-
* @returns {Boolean} Whether User Agent is running in iOS or not.
|
|
141
|
-
*/
|
|
142
|
-
export function isiOS( userAgent ) {
|
|
143
|
-
// "Request mobile site" || "Request desktop site".
|
|
144
|
-
return !!userAgent.match( /iphone|ipad/i ) || ( isMac( userAgent ) && navigator.maxTouchPoints > 0 );
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Checks if User Agent represented by the string is Android mobile device.
|
|
149
|
-
*
|
|
150
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
151
|
-
* @returns {Boolean} Whether User Agent is Safari or not.
|
|
152
|
-
*/
|
|
153
|
-
export function isAndroid( userAgent ) {
|
|
154
|
-
return userAgent.indexOf( 'android' ) > -1;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Checks if User Agent represented by the string is Blink engine.
|
|
159
|
-
*
|
|
160
|
-
* @param {String} userAgent **Lowercase** `navigator.userAgent` string.
|
|
161
|
-
* @returns {Boolean} Whether User Agent is Blink engine or not.
|
|
162
|
-
*/
|
|
163
|
-
export function isBlink( userAgent ) {
|
|
164
|
-
// The Edge browser before switching to the Blink engine used to report itself as Chrome (and "Edge/")
|
|
165
|
-
// but after switching to the Blink it replaced "Edge/" with "Edg/".
|
|
166
|
-
return userAgent.indexOf( 'chrome/' ) > -1 && userAgent.indexOf( 'edge/' ) < 0;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
|
|
171
|
-
* More information about unicode properties might be found
|
|
172
|
-
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
173
|
-
*
|
|
174
|
-
* @returns {Boolean}
|
|
175
|
-
*/
|
|
176
|
-
export function isRegExpUnicodePropertySupported() {
|
|
177
|
-
let isSupported = false;
|
|
178
|
-
|
|
179
|
-
// Feature detection for Unicode properties. Added in ES2018. Currently Firefox does not support it.
|
|
180
|
-
// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.
|
|
181
|
-
|
|
182
|
-
try {
|
|
183
|
-
// Usage of regular expression literal cause error during build (ckeditor/ckeditor5-dev#534).
|
|
184
|
-
isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
|
|
185
|
-
} catch ( error ) {
|
|
186
|
-
// Firefox throws a SyntaxError when the group is unsupported.
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return isSupported;
|
|
190
|
-
}
|
package/src/eventinfo.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
/**
|
|
7
|
-
* @module utils/eventinfo
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import spy from './spy';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The event object passed to event callbacks. It is used to provide information about the event as well as a tool to
|
|
14
|
-
* manipulate it.
|
|
15
|
-
*/
|
|
16
|
-
export default class EventInfo {
|
|
17
|
-
/**
|
|
18
|
-
* @param {Object} source The emitter.
|
|
19
|
-
* @param {String} name The event name.
|
|
20
|
-
*/
|
|
21
|
-
constructor( source, name ) {
|
|
22
|
-
/**
|
|
23
|
-
* The object that fired the event.
|
|
24
|
-
*
|
|
25
|
-
* @readonly
|
|
26
|
-
* @member {Object}
|
|
27
|
-
*/
|
|
28
|
-
this.source = source;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The event name.
|
|
32
|
-
*
|
|
33
|
-
* @readonly
|
|
34
|
-
* @member {String}
|
|
35
|
-
*/
|
|
36
|
-
this.name = name;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Path this event has followed. See {@link module:utils/emittermixin~EmitterMixin#delegate}.
|
|
40
|
-
*
|
|
41
|
-
* @readonly
|
|
42
|
-
* @member {Array.<Object>}
|
|
43
|
-
*/
|
|
44
|
-
this.path = [];
|
|
45
|
-
|
|
46
|
-
// The following methods are defined in the constructor because they must be re-created per instance.
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Stops the event emitter to call further callbacks for this event interaction.
|
|
50
|
-
*
|
|
51
|
-
* @method #stop
|
|
52
|
-
*/
|
|
53
|
-
this.stop = spy();
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Removes the current callback from future interactions of this event.
|
|
57
|
-
*
|
|
58
|
-
* @method #off
|
|
59
|
-
*/
|
|
60
|
-
this.off = spy();
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The value which will be returned by {@link module:utils/emittermixin~EmitterMixin#fire}.
|
|
64
|
-
*
|
|
65
|
-
* It's `undefined` by default and can be changed by an event listener:
|
|
66
|
-
*
|
|
67
|
-
* dataController.fire( 'getSelectedContent', ( evt ) => {
|
|
68
|
-
* // This listener will make `dataController.fire( 'getSelectedContent' )`
|
|
69
|
-
* // always return an empty DocumentFragment.
|
|
70
|
-
* evt.return = new DocumentFragment();
|
|
71
|
-
*
|
|
72
|
-
* // Make sure no other listeners are executed.
|
|
73
|
-
* evt.stop();
|
|
74
|
-
* } );
|
|
75
|
-
*
|
|
76
|
-
* @member #return
|
|
77
|
-
*/
|
|
78
|
-
}
|
|
79
|
-
}
|
package/src/fastdiff.js
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
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
|
-
/**
|
|
7
|
-
* @module utils/fastdiff
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Finds positions of the first and last change in the given string/array and generates a set of changes:
|
|
12
|
-
*
|
|
13
|
-
* fastDiff( '12a', '12xyza' );
|
|
14
|
-
* // [ { index: 2, type: 'insert', values: [ 'x', 'y', 'z' ] } ]
|
|
15
|
-
*
|
|
16
|
-
* fastDiff( '12a', '12aa' );
|
|
17
|
-
* // [ { index: 3, type: 'insert', values: [ 'a' ] } ]
|
|
18
|
-
*
|
|
19
|
-
* fastDiff( '12xyza', '12a' );
|
|
20
|
-
* // [ { index: 2, type: 'delete', howMany: 3 } ]
|
|
21
|
-
*
|
|
22
|
-
* fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
|
|
23
|
-
* // [ { index: 3, type: 'delete', howMany: 1 } ]
|
|
24
|
-
*
|
|
25
|
-
* fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
|
|
26
|
-
* // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
|
|
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
|
-
* fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
|
|
32
|
-
* return a.value === b.value;
|
|
33
|
-
* } );
|
|
34
|
-
* // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
|
|
35
|
-
*
|
|
36
|
-
* The resulted set of changes can be applied to the input in order to transform it into the output, for example:
|
|
37
|
-
*
|
|
38
|
-
* let input = '12abc3';
|
|
39
|
-
* const output = '2ab';
|
|
40
|
-
* const changes = fastDiff( input, output );
|
|
41
|
-
*
|
|
42
|
-
* changes.forEach( change => {
|
|
43
|
-
* if ( change.type == 'insert' ) {
|
|
44
|
-
* input = input.substring( 0, change.index ) + change.values.join( '' ) + input.substring( change.index );
|
|
45
|
-
* } else if ( change.type == 'delete' ) {
|
|
46
|
-
* input = input.substring( 0, change.index ) + input.substring( change.index + change.howMany );
|
|
47
|
-
* }
|
|
48
|
-
* } );
|
|
49
|
-
*
|
|
50
|
-
* // input equals output now
|
|
51
|
-
*
|
|
52
|
-
* or in case of arrays:
|
|
53
|
-
*
|
|
54
|
-
* let input = [ '1', '2', 'a', 'b', 'c', '3' ];
|
|
55
|
-
* const output = [ '2', 'a', 'b' ];
|
|
56
|
-
* const changes = fastDiff( input, output );
|
|
57
|
-
*
|
|
58
|
-
* changes.forEach( change => {
|
|
59
|
-
* if ( change.type == 'insert' ) {
|
|
60
|
-
* input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
|
|
61
|
-
* } else if ( change.type == 'delete' ) {
|
|
62
|
-
* input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
|
|
63
|
-
* }
|
|
64
|
-
* } );
|
|
65
|
-
*
|
|
66
|
-
* // input equals output now
|
|
67
|
-
*
|
|
68
|
-
* By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
|
|
69
|
-
* the {@link module:utils/diff~diff `diff()`} function:
|
|
70
|
-
*
|
|
71
|
-
* fastDiff( '12a', '12xyza' );
|
|
72
|
-
* // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
|
|
73
|
-
*
|
|
74
|
-
* The default output format of this function is compatible with the output format of
|
|
75
|
-
* {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
|
|
76
|
-
* compatible with the output of {@link module:utils/diff~diff `diff()`}:
|
|
77
|
-
*
|
|
78
|
-
* const a = '1234';
|
|
79
|
-
* const b = '12xyz34';
|
|
80
|
-
*
|
|
81
|
-
* // Both calls will return the same results (grouped changes format).
|
|
82
|
-
* fastDiff( a, b );
|
|
83
|
-
* diffToChanges( diff( a, b ) );
|
|
84
|
-
*
|
|
85
|
-
* // Again, both calls will return the same results (atomic changes format).
|
|
86
|
-
* fastDiff( a, b, null, true );
|
|
87
|
-
* diff( a, b );
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* @param {Array|String} a Input array or string.
|
|
91
|
-
* @param {Array|String} b Input array or string.
|
|
92
|
-
* @param {Function} [cmp] Optional function used to compare array values, by default `===` (strict equal operator) is used.
|
|
93
|
-
* @param {Boolean} [atomicChanges=false] Whether an array of `inset|delete|equal` operations should
|
|
94
|
-
* be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
|
|
95
|
-
* @returns {Array} Array of changes.
|
|
96
|
-
*/
|
|
97
|
-
export default function fastDiff( a, b, cmp, atomicChanges = false ) {
|
|
98
|
-
// Set the comparator function.
|
|
99
|
-
cmp = cmp || function( a, b ) {
|
|
100
|
-
return a === b;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// Convert the string (or any array-like object - eg. NodeList) to an array by using the slice() method because,
|
|
104
|
-
// unlike Array.from(), it returns array of UTF-16 code units instead of the code points of a string.
|
|
105
|
-
// One code point might be a surrogate pair of two code units. All text offsets are expected to be in code units.
|
|
106
|
-
// See ckeditor/ckeditor5#3147.
|
|
107
|
-
//
|
|
108
|
-
// We need to make sure here that fastDiff() works identical to diff().
|
|
109
|
-
if ( !Array.isArray( a ) ) {
|
|
110
|
-
a = Array.prototype.slice.call( a );
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if ( !Array.isArray( b ) ) {
|
|
114
|
-
b = Array.prototype.slice.call( b );
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Find first and last change.
|
|
118
|
-
const changeIndexes = findChangeBoundaryIndexes( a, b, cmp );
|
|
119
|
-
|
|
120
|
-
// Transform into changes array.
|
|
121
|
-
return atomicChanges ? changeIndexesToAtomicChanges( changeIndexes, b.length ) : changeIndexesToChanges( b, changeIndexes );
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Finds position of the first and last change in the given arrays. For example:
|
|
125
|
-
//
|
|
126
|
-
// const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
|
|
127
|
-
// console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
|
|
128
|
-
//
|
|
129
|
-
// 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`.
|
|
130
|
-
// Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
|
|
131
|
-
// can be generated.
|
|
132
|
-
//
|
|
133
|
-
// @param {Array} arr1
|
|
134
|
-
// @param {Array} arr2
|
|
135
|
-
// @param {Function} cmp Comparator function.
|
|
136
|
-
// @returns {Object}
|
|
137
|
-
// @returns {Number} return.firstIndex Index of the first change in both values (always the same for both).
|
|
138
|
-
// @returns {Number} result.lastIndexOld Index of the last common value in `arr1`.
|
|
139
|
-
// @returns {Number} result.lastIndexNew Index of the last common value in `arr2`.
|
|
140
|
-
function findChangeBoundaryIndexes( arr1, arr2, cmp ) {
|
|
141
|
-
// Find the first difference between passed values.
|
|
142
|
-
const firstIndex = findFirstDifferenceIndex( arr1, arr2, cmp );
|
|
143
|
-
|
|
144
|
-
// If arrays are equal return -1 indexes object.
|
|
145
|
-
if ( firstIndex === -1 ) {
|
|
146
|
-
return { firstIndex: -1, lastIndexOld: -1, lastIndexNew: -1 };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Remove the common part of each value and reverse them to make it simpler to find the last difference between them.
|
|
150
|
-
const oldArrayReversed = cutAndReverse( arr1, firstIndex );
|
|
151
|
-
const newArrayReversed = cutAndReverse( arr2, firstIndex );
|
|
152
|
-
|
|
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
|
-
|
|
166
|
-
// Use `lastIndex` to calculate proper offset, starting from the beginning (`lastIndex` kind of starts from the end).
|
|
167
|
-
const lastIndexOld = arr1.length - lastIndex;
|
|
168
|
-
const lastIndexNew = arr2.length - lastIndex;
|
|
169
|
-
|
|
170
|
-
return { firstIndex, lastIndexOld, lastIndexNew };
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
|
|
174
|
-
//
|
|
175
|
-
// @param {Array} arr1
|
|
176
|
-
// @param {Array} arr2
|
|
177
|
-
// @param {Function} cmp Comparator function.
|
|
178
|
-
// @returns {Number}
|
|
179
|
-
function findFirstDifferenceIndex( arr1, arr2, cmp ) {
|
|
180
|
-
for ( let i = 0; i < Math.max( arr1.length, arr2.length ); i++ ) {
|
|
181
|
-
if ( arr1[ i ] === undefined || arr2[ i ] === undefined || !cmp( arr1[ i ], arr2[ i ] ) ) {
|
|
182
|
-
return i;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return -1; // Return -1 if arrays are equal.
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
|
|
190
|
-
//
|
|
191
|
-
// @param {Array} arr Array to be processed.
|
|
192
|
-
// @param {Number} howMany How many elements from array beginning to remove.
|
|
193
|
-
// @returns {Array} Shortened and reversed array.
|
|
194
|
-
function cutAndReverse( arr, howMany ) {
|
|
195
|
-
return arr.slice( howMany ).reverse();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Generates changes array based on change indexes from `findChangeBoundaryIndexes` function. This function will
|
|
199
|
-
// generate array with 0 (no changes), 1 (deletion or insertion) or 2 records (insertion and deletion).
|
|
200
|
-
//
|
|
201
|
-
// @param {Array} newArray New array for which change indexes were calculated.
|
|
202
|
-
// @param {Object} changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
|
|
203
|
-
// @returns {Array.<Object>} Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
|
|
204
|
-
function changeIndexesToChanges( newArray, changeIndexes ) {
|
|
205
|
-
const result = [];
|
|
206
|
-
const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
|
|
207
|
-
|
|
208
|
-
// Order operations as 'insert', 'delete' array to keep compatibility with {@link module:utils/difftochanges~diffToChanges}
|
|
209
|
-
// in most cases. However, 'diffToChanges' does not stick to any order so in some cases
|
|
210
|
-
// (for example replacing '12345' with 'abcd') it will generate 'delete', 'insert' order.
|
|
211
|
-
if ( lastIndexNew - firstIndex > 0 ) {
|
|
212
|
-
result.push( {
|
|
213
|
-
index: firstIndex,
|
|
214
|
-
type: 'insert',
|
|
215
|
-
values: newArray.slice( firstIndex, lastIndexNew )
|
|
216
|
-
} );
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if ( lastIndexOld - firstIndex > 0 ) {
|
|
220
|
-
result.push( {
|
|
221
|
-
index: firstIndex + ( lastIndexNew - firstIndex ), // Increase index of what was inserted.
|
|
222
|
-
type: 'delete',
|
|
223
|
-
howMany: lastIndexOld - firstIndex
|
|
224
|
-
} );
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return result;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
|
|
231
|
-
//
|
|
232
|
-
// @param {Object} changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
|
|
233
|
-
// @param {Number} newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
|
|
234
|
-
// @returns {Array.<String>} Array of changes compatible with {@link module:utils/diff~diff} format.
|
|
235
|
-
function changeIndexesToAtomicChanges( changeIndexes, newLength ) {
|
|
236
|
-
const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
|
|
237
|
-
|
|
238
|
-
// No changes.
|
|
239
|
-
if ( firstIndex === -1 ) {
|
|
240
|
-
return Array( newLength ).fill( 'equal' );
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
let result = [];
|
|
244
|
-
if ( firstIndex > 0 ) {
|
|
245
|
-
result = result.concat( Array( firstIndex ).fill( 'equal' ) );
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if ( lastIndexNew - firstIndex > 0 ) {
|
|
249
|
-
result = result.concat( Array( lastIndexNew - firstIndex ).fill( 'insert' ) );
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if ( lastIndexOld - firstIndex > 0 ) {
|
|
253
|
-
result = result.concat( Array( lastIndexOld - firstIndex ).fill( 'delete' ) );
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if ( lastIndexNew < newLength ) {
|
|
257
|
-
result = result.concat( Array( newLength - lastIndexNew ).fill( 'equal' ) );
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return result;
|
|
261
|
-
}
|
package/src/first.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
/**
|
|
7
|
-
* @module utils/first
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Returns first item of the given `iterable`.
|
|
12
|
-
*
|
|
13
|
-
* @param {Iterable.<*>} iterable
|
|
14
|
-
* @returns {*}
|
|
15
|
-
*/
|
|
16
|
-
export default function first( iterable ) {
|
|
17
|
-
const iteratorItem = iterable.next();
|
|
18
|
-
|
|
19
|
-
if ( iteratorItem.done ) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return iteratorItem.value;
|
|
24
|
-
}
|