@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/locale.js CHANGED
@@ -2,175 +2,78 @@
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/locale
8
7
  */
9
-
10
8
  /* globals console */
11
-
12
9
  import toArray from './toarray';
13
10
  import { _translate } from './translation-service';
14
11
  import { getLanguageDirection } from './language';
15
-
16
12
  /**
17
13
  * Represents the localization services.
18
14
  */
19
15
  export default class Locale {
20
- /**
21
- * Creates a new instance of the locale class. Learn more about
22
- * {@glink features/ui-language configuring the language of the editor}.
23
- *
24
- * @param {Object} [options] Locale configuration.
25
- * @param {String} [options.uiLanguage='en'] The editor UI language code in the
26
- * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
27
- * @param {String} [options.contentLanguage] The editor content language code in the
28
- * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
29
- * See {@link #contentLanguage}.
30
- */
31
- constructor( options = {} ) {
32
- /**
33
- * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
34
- *
35
- * If the {@link #contentLanguage content language} was not specified in the `Locale` constructor,
36
- * it also defines the language of the content.
37
- *
38
- * @readonly
39
- * @member {String}
40
- */
41
- this.uiLanguage = options.uiLanguage || 'en';
42
-
43
- /**
44
- * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
45
- *
46
- * Usually the same as the {@link #uiLanguage editor language}, it can be customized by passing an optional
47
- * argument to the `Locale` constructor.
48
- *
49
- * @readonly
50
- * @member {String}
51
- */
52
- this.contentLanguage = options.contentLanguage || this.uiLanguage;
53
-
54
- /**
55
- * Text direction of the {@link #uiLanguage editor UI language}. Either `'ltr'` or `'rtl'`.
56
- *
57
- * @readonly
58
- * @member {String}
59
- */
60
- this.uiLanguageDirection = getLanguageDirection( this.uiLanguage );
61
-
62
- /**
63
- * Text direction of the {@link #contentLanguage editor content language}.
64
- *
65
- * If the content language was passed directly to the `Locale` constructor, this property represents the
66
- * direction of that language.
67
- *
68
- * If the {@link #contentLanguage editor content language} was derived from the {@link #uiLanguage editor language},
69
- * the content language direction is the same as the {@link #uiLanguageDirection UI language direction}.
70
- *
71
- * The value is either `'ltr'` or `'rtl'`.
72
- *
73
- * @readonly
74
- * @member {String}
75
- */
76
- this.contentLanguageDirection = getLanguageDirection( this.contentLanguage );
77
-
78
- /**
79
- * Translates the given message to the {@link #uiLanguage}. This method is also available in
80
- * {@link module:core/editor/editor~Editor#t `Editor`} and {@link module:ui/view~View#t `View`}.
81
- *
82
- * This method's context is statically bound to the `Locale` instance and **should always be called as a function**:
83
- *
84
- * const t = locale.t;
85
- * t( 'Label' );
86
- *
87
- * The message can be either a string or an object implementing the {@link module:utils/translation-service~Message} interface.
88
- *
89
- * The message may contain placeholders (`%<index>`) for value(s) that are passed as a `values` parameter.
90
- * For an array of values, the `%<index>` will be changed to an element of that array at the given index.
91
- * For a single value passed as the second argument, only the `%0` placeholders will be changed to the provided value.
92
- *
93
- * t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
94
- * t( 'Created file "%0", fileName );
95
- *
96
- * The message supports plural forms. To specify the plural form, use the `plural` property. Singular or plural form
97
- * will be chosen depending on the first value from the passed `values`. The value of the `plural` property is used
98
- * as a default plural translation when the translation for the target language is missing.
99
- *
100
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space' for the English language.
101
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Add 5 spaces' for the English language.
102
- * t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Add 2 spaces' for the English language.
103
- *
104
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Dodaj spację' for the Polish language.
105
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Dodaj 5 spacji' for the Polish language.
106
- * t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Dodaj 2 spacje' for the Polish language.
107
- *
108
- * * The message should provide an ID using the `id` property when the message strings are not unique and their
109
- * translations should be different.
110
- *
111
- * translate( 'en', { string: 'image', id: 'ADD_IMAGE' } );
112
- * translate( 'en', { string: 'image', id: 'AN_IMAGE' } );
113
- *
114
- * @method #t
115
- * @param {String|module:utils/translation-service~Message} message A message that will be localized (translated).
116
- * @param {String|Number|Array.<String|Number>} [values] A value or an array of values that will fill message placeholders.
117
- * For messages supporting plural forms the first value will determine the plural form.
118
- * @returns {String}
119
- */
120
- this.t = ( message, values ) => this._t( message, values );
121
- }
122
-
123
- /**
124
- * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
125
- *
126
- * **Note**: This property was deprecated. Please use {@link #uiLanguage} and {@link #contentLanguage}
127
- * properties instead.
128
- *
129
- * @deprecated
130
- * @member {String}
131
- */
132
- get language() {
133
- /**
134
- * The {@link module:utils/locale~Locale#language `Locale#language`} property was deprecated and will
135
- * be removed in the near future. Please use the {@link #uiLanguage} and {@link #contentLanguage} properties instead.
136
- *
137
- * @error locale-deprecated-language-property
138
- */
139
- console.warn(
140
- 'locale-deprecated-language-property: ' +
141
- 'The Locale#language property has been deprecated and will be removed in the near future. ' +
142
- 'Please use #uiLanguage and #contentLanguage properties instead.' );
143
-
144
- return this.uiLanguage;
145
- }
146
-
147
- /**
148
- * An unbound version of the {@link #t} method.
149
- *
150
- * @private
151
- * @param {String|module:utils/translation-service~Message} message
152
- * @param {Number|String|Array.<Number|String>} [values]
153
- * @returns {String}
154
- */
155
- _t( message, values = [] ) {
156
- values = toArray( values );
157
-
158
- if ( typeof message === 'string' ) {
159
- message = { string: message };
160
- }
161
-
162
- const hasPluralForm = !!message.plural;
163
- const quantity = hasPluralForm ? values[ 0 ] : 1;
164
-
165
- const translatedString = _translate( this.uiLanguage, message, quantity );
166
-
167
- return interpolateString( translatedString, values );
168
- }
16
+ /**
17
+ * Creates a new instance of the locale class. Learn more about
18
+ * {@glink features/ui-language configuring the language of the editor}.
19
+ *
20
+ * @param {Object} [options] Locale configuration.
21
+ * @param {String} [options.uiLanguage='en'] The editor UI language code in the
22
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
23
+ * @param {String} [options.contentLanguage] The editor content language code in the
24
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
25
+ * See {@link #contentLanguage}.
26
+ */
27
+ constructor(options = {}) {
28
+ this.uiLanguage = options.uiLanguage || 'en';
29
+ this.contentLanguage = options.contentLanguage || this.uiLanguage;
30
+ this.uiLanguageDirection = getLanguageDirection(this.uiLanguage);
31
+ this.contentLanguageDirection = getLanguageDirection(this.contentLanguage);
32
+ this.t = (message, values) => this._t(message, values);
33
+ }
34
+ /**
35
+ * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
36
+ *
37
+ * **Note**: This property was deprecated. Please use {@link #uiLanguage} and {@link #contentLanguage}
38
+ * properties instead.
39
+ *
40
+ * @deprecated
41
+ * @member {String}
42
+ */
43
+ get language() {
44
+ /**
45
+ * The {@link module:utils/locale~Locale#language `Locale#language`} property was deprecated and will
46
+ * be removed in the near future. Please use the {@link #uiLanguage} and {@link #contentLanguage} properties instead.
47
+ *
48
+ * @error locale-deprecated-language-property
49
+ */
50
+ console.warn('locale-deprecated-language-property: ' +
51
+ 'The Locale#language property has been deprecated and will be removed in the near future. ' +
52
+ 'Please use #uiLanguage and #contentLanguage properties instead.');
53
+ return this.uiLanguage;
54
+ }
55
+ /**
56
+ * An unbound version of the {@link #t} method.
57
+ *
58
+ * @private
59
+ * @param {String|module:utils/translation-service~Message} message
60
+ * @param {Number|String|Array.<Number|String>} [values]
61
+ * @returns {String}
62
+ */
63
+ _t(message, values = []) {
64
+ values = toArray(values);
65
+ if (typeof message === 'string') {
66
+ message = { string: message };
67
+ }
68
+ const hasPluralForm = !!message.plural;
69
+ const quantity = hasPluralForm ? values[0] : 1;
70
+ const translatedString = _translate(this.uiLanguage, message, quantity);
71
+ return interpolateString(translatedString, values);
72
+ }
169
73
  }
170
-
171
74
  // Fills the `%0, %1, ...` string placeholders with values.
172
- function interpolateString( string, values ) {
173
- return string.replace( /%(\d+)/g, ( match, index ) => {
174
- return ( index < values.length ) ? values[ index ] : match;
175
- } );
75
+ function interpolateString(string, values) {
76
+ return string.replace(/%(\d+)/g, (match, index) => {
77
+ return (index < values.length) ? values[index] : match;
78
+ });
176
79
  }
package/src/mapsequal.js CHANGED
@@ -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/mapsequal
8
7
  */
9
-
10
8
  /**
11
9
  * Checks whether given {Map}s are equal, that is has same size and same key-value pairs.
12
10
  *
@@ -14,19 +12,16 @@
14
12
  * @param {Map} mapB The second map to compare.
15
13
  * @returns {Boolean} `true` if given maps are equal, `false` otherwise.
16
14
  */
17
- export default function mapsEqual( mapA, mapB ) {
18
- if ( mapA.size != mapB.size ) {
19
- return false;
20
- }
21
-
22
- for ( const attr of mapA.entries() ) {
23
- const valA = JSON.stringify( attr[ 1 ] );
24
- const valB = JSON.stringify( mapB.get( attr[ 0 ] ) );
25
-
26
- if ( valA !== valB ) {
27
- return false;
28
- }
29
- }
30
-
31
- return true;
15
+ export default function mapsEqual(mapA, mapB) {
16
+ if (mapA.size != mapB.size) {
17
+ return false;
18
+ }
19
+ for (const attr of mapA.entries()) {
20
+ const valA = JSON.stringify(attr[1]);
21
+ const valB = JSON.stringify(mapB.get(attr[0]));
22
+ if (valA !== valB) {
23
+ return false;
24
+ }
25
+ }
26
+ return true;
32
27
  }
package/src/mix.js CHANGED
@@ -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/mix
8
7
  */
9
-
10
8
  /**
11
9
  * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
12
10
  * prototype of first object (a constructor).
@@ -27,21 +25,24 @@
27
25
  *
28
26
  * Note: Properties which already exist in the base class will not be overriden.
29
27
  *
28
+ * @depreciated Use mixin pattern, see: https://www.typescriptlang.org/docs/handbook/mixins.html.
30
29
  * @param {Function} [baseClass] Class which prototype will be extended.
31
30
  * @param {Object} [...mixins] Objects from which to get properties.
32
31
  */
33
- export default function mix( baseClass, ...mixins ) {
34
- mixins.forEach( mixin => {
35
- Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
36
- .forEach( key => {
37
- if ( key in baseClass.prototype ) {
38
- return;
39
- }
40
-
41
- const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
42
- sourceDescriptor.enumerable = false;
43
-
44
- Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
45
- } );
46
- } );
32
+ export default function mix(baseClass, ...mixins) {
33
+ mixins.forEach(mixin => {
34
+ const propertyNames = Object.getOwnPropertyNames(mixin);
35
+ const propertySymbols = Object.getOwnPropertySymbols(mixin);
36
+ propertyNames.concat(propertySymbols).forEach(key => {
37
+ if (key in baseClass.prototype) {
38
+ return;
39
+ }
40
+ if (typeof mixin == 'function' && (key == 'length' || key == 'name' || key == 'prototype')) {
41
+ return;
42
+ }
43
+ const sourceDescriptor = Object.getOwnPropertyDescriptor(mixin, key);
44
+ sourceDescriptor.enumerable = false;
45
+ Object.defineProperty(baseClass.prototype, key, sourceDescriptor);
46
+ });
47
+ });
47
48
  }
package/src/nth.js CHANGED
@@ -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/nth
8
7
  */
9
-
10
8
  /**
11
9
  * Returns `nth` (starts from `0` of course) item of the given `iterable`.
12
10
  *
@@ -19,13 +17,12 @@
19
17
  * @param {Iterable.<*>} iterable
20
18
  * @returns {*}
21
19
  */
22
- export default function nth( index, iterable ) {
23
- for ( const item of iterable ) {
24
- if ( index === 0 ) {
25
- return item;
26
- }
27
- index -= 1;
28
- }
29
-
30
- return null;
20
+ export default function nth(index, iterable) {
21
+ for (const item of iterable) {
22
+ if (index === 0) {
23
+ return item;
24
+ }
25
+ index -= 1;
26
+ }
27
+ return null;
31
28
  }
@@ -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/objecttomap
8
7
  */
9
-
10
8
  /**
11
9
  * Transforms object to map.
12
10
  *
@@ -15,15 +13,13 @@
15
13
  *
16
14
  * **Note**: For mixed data (`Object` or `Iterable`) there's a dedicated {@link module:utils/tomap~toMap} function.
17
15
  *
18
- * @param {Object} obj Object to transform.
16
+ * @param {Object|null} obj Object to transform.
19
17
  * @returns {Map} Map created from object.
20
18
  */
21
- export default function objectToMap( obj ) {
22
- const map = new Map();
23
-
24
- for ( const key in obj ) {
25
- map.set( key, obj[ key ] );
26
- }
27
-
28
- return map;
19
+ export default function objectToMap(obj) {
20
+ const map = new Map();
21
+ for (const key in obj) {
22
+ map.set(key, obj[key]);
23
+ }
24
+ return map;
29
25
  }