@ckeditor/ckeditor5-utils 0.0.0-internal-20241017.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 (213) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +22 -0
  4. package/dist/abortabledebounce.d.ts +21 -0
  5. package/dist/areconnectedthroughproperties.d.ts +15 -0
  6. package/dist/ckeditorerror.d.ts +127 -0
  7. package/dist/collection.d.ts +437 -0
  8. package/dist/comparearrays.d.ts +34 -0
  9. package/dist/config.d.ts +167 -0
  10. package/dist/count.d.ts +22 -0
  11. package/dist/crc32.d.ts +30 -0
  12. package/dist/delay.d.ts +23 -0
  13. package/dist/diff.d.ts +35 -0
  14. package/dist/difftochanges.d.ts +63 -0
  15. package/dist/dom/createelement.d.ts +61 -0
  16. package/dist/dom/emittermixin.d.ts +146 -0
  17. package/dist/dom/findclosestscrollableancestor.d.ts +15 -0
  18. package/dist/dom/getancestors.d.ts +21 -0
  19. package/dist/dom/getborderwidths.d.ts +28 -0
  20. package/dist/dom/getcommonancestor.d.ts +16 -0
  21. package/dist/dom/getdatafromelement.d.ts +18 -0
  22. package/dist/dom/getpositionedancestor.d.ts +14 -0
  23. package/dist/dom/getrangefrommouseevent.d.ts +21 -0
  24. package/dist/dom/global.d.ts +36 -0
  25. package/dist/dom/indexof.d.ts +18 -0
  26. package/dist/dom/insertat.d.ts +19 -0
  27. package/dist/dom/iscomment.d.ts +15 -0
  28. package/dist/dom/isnode.d.ts +15 -0
  29. package/dist/dom/isrange.d.ts +15 -0
  30. package/dist/dom/istext.d.ts +15 -0
  31. package/dist/dom/isvalidattributename.d.ts +14 -0
  32. package/dist/dom/isvisible.d.ts +19 -0
  33. package/dist/dom/iswindow.d.ts +15 -0
  34. package/dist/dom/position.d.ts +215 -0
  35. package/dist/dom/rect.d.ts +199 -0
  36. package/dist/dom/remove.d.ts +17 -0
  37. package/dist/dom/resizeobserver.d.ts +78 -0
  38. package/dist/dom/scroll.d.ts +77 -0
  39. package/dist/dom/setdatainelement.d.ts +18 -0
  40. package/dist/dom/tounit.d.ts +26 -0
  41. package/dist/elementreplacer.d.ts +35 -0
  42. package/dist/emittermixin.d.ts +316 -0
  43. package/dist/env.d.ts +144 -0
  44. package/dist/eventinfo.d.ts +62 -0
  45. package/dist/fastdiff.d.ts +116 -0
  46. package/dist/first.d.ts +15 -0
  47. package/dist/focustracker.d.ts +83 -0
  48. package/dist/index-content.css +4 -0
  49. package/dist/index-editor.css +4 -0
  50. package/dist/index.css +4 -0
  51. package/dist/index.d.ts +70 -0
  52. package/dist/index.js +5871 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/inserttopriorityarray.d.ts +34 -0
  55. package/dist/isiterable.d.ts +18 -0
  56. package/dist/keyboard.d.ts +130 -0
  57. package/dist/keystrokehandler.d.ts +101 -0
  58. package/dist/language.d.ts +21 -0
  59. package/dist/locale.d.ts +145 -0
  60. package/dist/mapsequal.d.ts +19 -0
  61. package/dist/mix.d.ts +89 -0
  62. package/dist/nth.d.ts +20 -0
  63. package/dist/objecttomap.d.ts +27 -0
  64. package/dist/observablemixin.d.ts +564 -0
  65. package/dist/parsebase64encodedobject.d.ts +15 -0
  66. package/dist/priorities.d.ts +37 -0
  67. package/dist/retry.d.ts +37 -0
  68. package/dist/splicearray.d.ts +30 -0
  69. package/dist/spy.d.ts +25 -0
  70. package/dist/toarray.d.ts +29 -0
  71. package/dist/tomap.d.ts +23 -0
  72. package/dist/translation-service.d.ts +178 -0
  73. package/dist/uid.d.ts +19 -0
  74. package/dist/unicode.d.ts +58 -0
  75. package/dist/version.d.ts +14 -0
  76. package/dist/wait.d.ts +20 -0
  77. package/package.json +36 -0
  78. package/src/abortabledebounce.d.ts +17 -0
  79. package/src/abortabledebounce.js +22 -0
  80. package/src/areconnectedthroughproperties.d.ts +11 -0
  81. package/src/areconnectedthroughproperties.js +73 -0
  82. package/src/ckeditorerror.d.ts +123 -0
  83. package/src/ckeditorerror.js +176 -0
  84. package/src/collection.d.ts +433 -0
  85. package/src/collection.js +583 -0
  86. package/src/comparearrays.d.ts +30 -0
  87. package/src/comparearrays.js +47 -0
  88. package/src/config.d.ts +163 -0
  89. package/src/config.js +163 -0
  90. package/src/count.d.ts +18 -0
  91. package/src/count.js +25 -0
  92. package/src/crc32.d.ts +26 -0
  93. package/src/crc32.js +61 -0
  94. package/src/delay.d.ts +19 -0
  95. package/src/delay.js +26 -0
  96. package/src/diff.d.ts +31 -0
  97. package/src/diff.js +115 -0
  98. package/src/difftochanges.d.ts +59 -0
  99. package/src/difftochanges.js +79 -0
  100. package/src/dom/createelement.d.ts +57 -0
  101. package/src/dom/createelement.js +40 -0
  102. package/src/dom/emittermixin.d.ts +142 -0
  103. package/src/dom/emittermixin.js +239 -0
  104. package/src/dom/findclosestscrollableancestor.d.ts +11 -0
  105. package/src/dom/findclosestscrollableancestor.js +31 -0
  106. package/src/dom/getancestors.d.ts +17 -0
  107. package/src/dom/getancestors.js +27 -0
  108. package/src/dom/getborderwidths.d.ts +24 -0
  109. package/src/dom/getborderwidths.js +24 -0
  110. package/src/dom/getcommonancestor.d.ts +12 -0
  111. package/src/dom/getcommonancestor.js +25 -0
  112. package/src/dom/getdatafromelement.d.ts +14 -0
  113. package/src/dom/getdatafromelement.js +20 -0
  114. package/src/dom/getpositionedancestor.d.ts +10 -0
  115. package/src/dom/getpositionedancestor.js +22 -0
  116. package/src/dom/getrangefrommouseevent.d.ts +17 -0
  117. package/src/dom/getrangefrommouseevent.js +33 -0
  118. package/src/dom/global.d.ts +32 -0
  119. package/src/dom/global.js +35 -0
  120. package/src/dom/indexof.d.ts +14 -0
  121. package/src/dom/indexof.js +21 -0
  122. package/src/dom/insertat.d.ts +15 -0
  123. package/src/dom/insertat.js +17 -0
  124. package/src/dom/iscomment.d.ts +11 -0
  125. package/src/dom/iscomment.js +14 -0
  126. package/src/dom/isnode.d.ts +11 -0
  127. package/src/dom/isnode.js +21 -0
  128. package/src/dom/isrange.d.ts +11 -0
  129. package/src/dom/isrange.js +13 -0
  130. package/src/dom/istext.d.ts +11 -0
  131. package/src/dom/istext.js +13 -0
  132. package/src/dom/isvalidattributename.d.ts +10 -0
  133. package/src/dom/isvalidattributename.js +22 -0
  134. package/src/dom/isvisible.d.ts +15 -0
  135. package/src/dom/isvisible.js +30 -0
  136. package/src/dom/iswindow.d.ts +11 -0
  137. package/src/dom/iswindow.js +22 -0
  138. package/src/dom/position.d.ts +211 -0
  139. package/src/dom/position.js +313 -0
  140. package/src/dom/rect.d.ts +195 -0
  141. package/src/dom/rect.js +474 -0
  142. package/src/dom/remove.d.ts +13 -0
  143. package/src/dom/remove.js +18 -0
  144. package/src/dom/resizeobserver.d.ts +74 -0
  145. package/src/dom/resizeobserver.js +127 -0
  146. package/src/dom/scroll.d.ts +73 -0
  147. package/src/dom/scroll.js +383 -0
  148. package/src/dom/setdatainelement.d.ts +14 -0
  149. package/src/dom/setdatainelement.js +20 -0
  150. package/src/dom/tounit.d.ts +22 -0
  151. package/src/dom/tounit.js +16 -0
  152. package/src/elementreplacer.d.ts +31 -0
  153. package/src/elementreplacer.js +43 -0
  154. package/src/emittermixin.d.ts +312 -0
  155. package/src/emittermixin.js +453 -0
  156. package/src/env.d.ts +140 -0
  157. package/src/env.js +145 -0
  158. package/src/eventinfo.d.ts +58 -0
  159. package/src/eventinfo.js +26 -0
  160. package/src/fastdiff.d.ts +112 -0
  161. package/src/fastdiff.js +248 -0
  162. package/src/first.d.ts +11 -0
  163. package/src/first.js +17 -0
  164. package/src/focustracker.d.ts +79 -0
  165. package/src/focustracker.js +101 -0
  166. package/src/index.d.ts +66 -0
  167. package/src/index.js +65 -0
  168. package/src/inserttopriorityarray.d.ts +30 -0
  169. package/src/inserttopriorityarray.js +21 -0
  170. package/src/isiterable.d.ts +14 -0
  171. package/src/isiterable.js +16 -0
  172. package/src/keyboard.d.ts +126 -0
  173. package/src/keyboard.js +252 -0
  174. package/src/keystrokehandler.d.ts +97 -0
  175. package/src/keystrokehandler.js +122 -0
  176. package/src/language.d.ts +17 -0
  177. package/src/language.js +20 -0
  178. package/src/locale.d.ts +141 -0
  179. package/src/locale.js +78 -0
  180. package/src/mapsequal.d.ts +15 -0
  181. package/src/mapsequal.js +27 -0
  182. package/src/mix.d.ts +85 -0
  183. package/src/mix.js +50 -0
  184. package/src/nth.d.ts +16 -0
  185. package/src/nth.js +24 -0
  186. package/src/objecttomap.d.ts +23 -0
  187. package/src/objecttomap.js +27 -0
  188. package/src/observablemixin.d.ts +560 -0
  189. package/src/observablemixin.js +580 -0
  190. package/src/parsebase64encodedobject.d.ts +11 -0
  191. package/src/parsebase64encodedobject.js +22 -0
  192. package/src/priorities.d.ts +33 -0
  193. package/src/priorities.js +23 -0
  194. package/src/retry.d.ts +33 -0
  195. package/src/retry.js +47 -0
  196. package/src/splicearray.d.ts +26 -0
  197. package/src/splicearray.js +40 -0
  198. package/src/spy.d.ts +21 -0
  199. package/src/spy.js +22 -0
  200. package/src/toarray.d.ts +25 -0
  201. package/src/toarray.js +7 -0
  202. package/src/tomap.d.ts +19 -0
  203. package/src/tomap.js +29 -0
  204. package/src/translation-service.d.ts +174 -0
  205. package/src/translation-service.js +209 -0
  206. package/src/uid.d.ts +15 -0
  207. package/src/uid.js +45 -0
  208. package/src/unicode.d.ts +54 -0
  209. package/src/unicode.js +85 -0
  210. package/src/version.d.ts +10 -0
  211. package/src/version.js +179 -0
  212. package/src/wait.d.ts +16 -0
  213. package/src/wait.js +29 -0
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * Handles a configuration dictionary.
7
+ *
8
+ * @typeParam Cfg A type of the configuration dictionary.
9
+ */
10
+ export default class Config<Cfg> {
11
+ /**
12
+ * Store for the whole configuration.
13
+ */
14
+ private readonly _config;
15
+ /**
16
+ * Creates an instance of the {@link ~Config} class.
17
+ *
18
+ * @param configurations The initial configurations to be set. Usually, provided by the user.
19
+ * @param defaultConfigurations The default configurations. Usually, provided by the system.
20
+ */
21
+ constructor(configurations?: Partial<Cfg>, defaultConfigurations?: Partial<Cfg>);
22
+ /**
23
+ * Set configuration values.
24
+ *
25
+ * It also accepts setting a "deep configuration" by using dots in the name. For example, `'resize.width'` sets
26
+ * the value for the `width` configuration in the `resize` subset.
27
+ *
28
+ * ```ts
29
+ * config.set( 'resize.width', 500 );
30
+ * ```
31
+ *
32
+ * It accepts both a name/value pair or an object, which properties and values will be used to set
33
+ * configurations. See {@link #set:CONFIG_OBJECT}.
34
+ *
35
+ * @label KEY_VALUE
36
+ * @param name The configuration name. Configuration names are case-sensitive.
37
+ * @param value The configuration value.
38
+ */
39
+ set<K extends string>(name: K, value: GetSubConfig<Cfg, K>): void;
40
+ /**
41
+ * Set configuration values.
42
+ *
43
+ * It accepts an object, which properties and values will be used to set configurations.
44
+ *
45
+ * ```ts
46
+ * config.set( {
47
+ * width: 500
48
+ * toolbar: {
49
+ * collapsed: true
50
+ * }
51
+ * } );
52
+ *
53
+ * // Equivalent to:
54
+ * config.set( 'width', 500 );
55
+ * config.set( 'toolbar.collapsed', true );
56
+ * ```
57
+ *
58
+ * Passing an object as the value will amend the configuration, not replace it.
59
+ *
60
+ * ```ts
61
+ * config.set( 'toolbar', {
62
+ * collapsed: true,
63
+ * } );
64
+ *
65
+ * config.set( 'toolbar', {
66
+ * color: 'red',
67
+ * } );
68
+ *
69
+ * config.get( 'toolbar.collapsed' ); // true
70
+ * config.get( 'toolbar.color' ); // 'red'
71
+ * ```
72
+ *
73
+ * It accepts both a name/value pair or an object, which properties and values will be used to set
74
+ * configurations. See {@link #set:KEY_VALUE}.
75
+ *
76
+ * @label CONFIG_OBJECT
77
+ * @param config The configuration object from which take properties as
78
+ * configuration entries. Configuration names are case-sensitive.
79
+ */
80
+ set(config: Partial<Cfg>): void;
81
+ /**
82
+ * Does exactly the same as {@link #set:KEY_VALUE} with one exception – passed configuration extends
83
+ * existing one, but does not overwrite already defined values.
84
+ *
85
+ * This method is supposed to be called by plugin developers to setup plugin's configurations. It would be
86
+ * rarely used for other needs.
87
+ *
88
+ * @label KEY_VALUE
89
+ * @param name The configuration name. Configuration names are case-sensitive.
90
+ * @param value The configuration value.
91
+ */
92
+ define<K extends string>(name: K, value: GetSubConfig<Cfg, K>): void;
93
+ /**
94
+ * Does exactly the same as {@link #set:CONFIG_OBJECT} with one exception – passed configuration extends
95
+ * existing one, but does not overwrite already defined values.
96
+ *
97
+ * This method is supposed to be called by plugin developers to setup plugin's configurations. It would be
98
+ * rarely used for other needs.
99
+ *
100
+ * @label CONFIG_OBJECT
101
+ * @param config The configuration object from which take properties as
102
+ * configuration entries. Configuration names are case-sensitive.
103
+ */
104
+ define(config: Partial<Cfg>): void;
105
+ /**
106
+ * Gets the value for a configuration entry.
107
+ *
108
+ * ```ts
109
+ * config.get( 'name' );
110
+ * ```
111
+ *
112
+ * Deep configurations can be retrieved by separating each part with a dot.
113
+ *
114
+ * ```ts
115
+ * config.get( 'toolbar.collapsed' );
116
+ * ```
117
+ *
118
+ * @param name The configuration name. Configuration names are case-sensitive.
119
+ * @returns The configuration value or `undefined` if the configuration entry was not found.
120
+ */
121
+ get<K extends string>(name: K): GetSubConfig<Cfg, K> | undefined;
122
+ /**
123
+ * Iterates over all top level configuration names.
124
+ */
125
+ names(): Iterable<string>;
126
+ /**
127
+ * Saves passed configuration to the specified target (nested object).
128
+ *
129
+ * @param target Nested config object.
130
+ * @param name The configuration name or an object from which take properties as
131
+ * configuration entries. Configuration names are case-sensitive.
132
+ * @param value The configuration value. Used if a name is passed.
133
+ * @param isDefine Define if passed configuration should overwrite existing one.
134
+ */
135
+ private _setToTarget;
136
+ /**
137
+ * Get specified configuration from specified source (nested object).
138
+ *
139
+ * @param source level of nested object.
140
+ * @param name The configuration name. Configuration names are case-sensitive.
141
+ * @returns The configuration value or `undefined` if the configuration entry was not found.
142
+ */
143
+ private _getFromSource;
144
+ /**
145
+ * Iterates through passed object and calls {@link #_setToTarget} method with object key and value for each property.
146
+ *
147
+ * @param target Nested config object.
148
+ * @param configuration Configuration data set
149
+ * @param isDefine Defines if passed configuration is default configuration or not.
150
+ */
151
+ private _setObjectToTarget;
152
+ }
153
+ /**
154
+ * An utility type excluding primitive values and arrays from the union.
155
+ */
156
+ export type OnlyObject<T> = Exclude<T, undefined | null | string | number | boolean | Array<any>>;
157
+ /**
158
+ * An utility type extracting configuration value from the given name.
159
+ *
160
+ * @typeParam T The type of a configuration dictionary.
161
+ * @typeParam K The literal type of configuration name (dot-separated path).
162
+ */
163
+ export type GetSubConfig<T, K> = K extends keyof T ? T[K] : K extends `${infer K1}.${infer K2}` ? K1 extends keyof T ? GetSubConfig<OnlyObject<T[K1]>, K2> : unknown : unknown;
package/src/config.js ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/config
7
+ */
8
+ import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';
9
+ /**
10
+ * Handles a configuration dictionary.
11
+ *
12
+ * @typeParam Cfg A type of the configuration dictionary.
13
+ */
14
+ export default class Config {
15
+ /**
16
+ * Creates an instance of the {@link ~Config} class.
17
+ *
18
+ * @param configurations The initial configurations to be set. Usually, provided by the user.
19
+ * @param defaultConfigurations The default configurations. Usually, provided by the system.
20
+ */
21
+ constructor(configurations, defaultConfigurations) {
22
+ this._config = Object.create(null);
23
+ // Set default configuration.
24
+ if (defaultConfigurations) {
25
+ // Clone the configuration to make sure that the properties will not be shared
26
+ // between editors and make the watchdog feature work correctly.
27
+ this.define(cloneConfig(defaultConfigurations));
28
+ }
29
+ // Set initial configuration.
30
+ if (configurations) {
31
+ this._setObjectToTarget(this._config, configurations);
32
+ }
33
+ }
34
+ set(name, value) {
35
+ this._setToTarget(this._config, name, value);
36
+ }
37
+ define(name, value) {
38
+ const isDefine = true;
39
+ this._setToTarget(this._config, name, value, isDefine);
40
+ }
41
+ /**
42
+ * Gets the value for a configuration entry.
43
+ *
44
+ * ```ts
45
+ * config.get( 'name' );
46
+ * ```
47
+ *
48
+ * Deep configurations can be retrieved by separating each part with a dot.
49
+ *
50
+ * ```ts
51
+ * config.get( 'toolbar.collapsed' );
52
+ * ```
53
+ *
54
+ * @param name The configuration name. Configuration names are case-sensitive.
55
+ * @returns The configuration value or `undefined` if the configuration entry was not found.
56
+ */
57
+ get(name) {
58
+ return this._getFromSource(this._config, name);
59
+ }
60
+ /**
61
+ * Iterates over all top level configuration names.
62
+ */
63
+ *names() {
64
+ for (const name of Object.keys(this._config)) {
65
+ yield name;
66
+ }
67
+ }
68
+ /**
69
+ * Saves passed configuration to the specified target (nested object).
70
+ *
71
+ * @param target Nested config object.
72
+ * @param name The configuration name or an object from which take properties as
73
+ * configuration entries. Configuration names are case-sensitive.
74
+ * @param value The configuration value. Used if a name is passed.
75
+ * @param isDefine Define if passed configuration should overwrite existing one.
76
+ */
77
+ _setToTarget(target, name, value, isDefine = false) {
78
+ // In case of an object, iterate through it and call `_setToTarget` again for each property.
79
+ if (isPlainObject(name)) {
80
+ this._setObjectToTarget(target, name, isDefine);
81
+ return;
82
+ }
83
+ // The configuration name should be split into parts if it has dots. E.g. `resize.width` -> [`resize`, `width`].
84
+ const parts = name.split('.');
85
+ // Take the name of the configuration out of the parts. E.g. `resize.width` -> `width`.
86
+ name = parts.pop();
87
+ // Iterate over parts to check if currently stored configuration has proper structure.
88
+ for (const part of parts) {
89
+ // If there is no object for specified part then create one.
90
+ if (!isPlainObject(target[part])) {
91
+ target[part] = Object.create(null);
92
+ }
93
+ // Nested object becomes a target.
94
+ target = target[part];
95
+ }
96
+ // In case of value is an object.
97
+ if (isPlainObject(value)) {
98
+ // We take care of proper config structure.
99
+ if (!isPlainObject(target[name])) {
100
+ target[name] = Object.create(null);
101
+ }
102
+ target = target[name];
103
+ // And iterate through this object calling `_setToTarget` again for each property.
104
+ this._setObjectToTarget(target, value, isDefine);
105
+ return;
106
+ }
107
+ // Do nothing if we are defining configuration for non empty name.
108
+ if (isDefine && typeof target[name] != 'undefined') {
109
+ return;
110
+ }
111
+ target[name] = value;
112
+ }
113
+ /**
114
+ * Get specified configuration from specified source (nested object).
115
+ *
116
+ * @param source level of nested object.
117
+ * @param name The configuration name. Configuration names are case-sensitive.
118
+ * @returns The configuration value or `undefined` if the configuration entry was not found.
119
+ */
120
+ _getFromSource(source, name) {
121
+ // The configuration name should be split into parts if it has dots. E.g. `resize.width` -> [`resize`, `width`].
122
+ const parts = name.split('.');
123
+ // Take the name of the configuration out of the parts. E.g. `resize.width` -> `width`.
124
+ name = parts.pop();
125
+ // Iterate over parts to check if currently stored configuration has proper structure.
126
+ for (const part of parts) {
127
+ if (!isPlainObject(source[part])) {
128
+ source = null;
129
+ break;
130
+ }
131
+ // Nested object becomes a source.
132
+ source = source[part];
133
+ }
134
+ // Always returns undefined for non existing configuration.
135
+ return source ? cloneConfig(source[name]) : undefined;
136
+ }
137
+ /**
138
+ * Iterates through passed object and calls {@link #_setToTarget} method with object key and value for each property.
139
+ *
140
+ * @param target Nested config object.
141
+ * @param configuration Configuration data set
142
+ * @param isDefine Defines if passed configuration is default configuration or not.
143
+ */
144
+ _setObjectToTarget(target, configuration, isDefine) {
145
+ Object.keys(configuration).forEach(key => {
146
+ this._setToTarget(target, key, configuration[key], isDefine);
147
+ });
148
+ }
149
+ }
150
+ /**
151
+ * Clones configuration object or value.
152
+ */
153
+ function cloneConfig(source) {
154
+ return cloneDeepWith(source, leaveItemReferences);
155
+ }
156
+ /**
157
+ * A customized function for cloneDeepWith.
158
+ * In case if it's a DOM Element it will leave references to DOM Elements instead of cloning them.
159
+ * If it's a function it will leave reference to actuall function.
160
+ */
161
+ function leaveItemReferences(value) {
162
+ return isElement(value) || typeof value === 'function' ? value : undefined;
163
+ }
package/src/count.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/count
7
+ */
8
+ /**
9
+ * Returns the number of items return by the iterator.
10
+ *
11
+ * ```ts
12
+ * count( [ 1, 2, 3, 4, 5 ] ); // 5;
13
+ * ```
14
+ *
15
+ * @param iterable Any iterable.
16
+ * @returns Number of items returned by that iterable.
17
+ */
18
+ export default function count(iterable: Iterable<unknown>): number;
package/src/count.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/count
7
+ */
8
+ /**
9
+ * Returns the number of items return by the iterator.
10
+ *
11
+ * ```ts
12
+ * count( [ 1, 2, 3, 4, 5 ] ); // 5;
13
+ * ```
14
+ *
15
+ * @param iterable Any iterable.
16
+ * @returns Number of items returned by that iterable.
17
+ */
18
+ export default function count(iterable) {
19
+ let count = 0;
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ for (const _ of iterable) {
22
+ count++;
23
+ }
24
+ return count;
25
+ }
package/src/crc32.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * Calculates CRC-32 checksum for a given inputData to verify the integrity of data.
7
+ *
8
+ * @param inputData Accepts a single value (string, number, boolean), an array of strings, or an array of all of the above types.
9
+ * Non-string values are converted to strings before calculating the checksum.
10
+ * The checksum calculation is based on the concatenated string representation of the input values:
11
+ * * `crc32('foo')` is equivalent to `crc32(['foo'])`
12
+ * * `crc32(123)` is equivalent to `crc32(['123'])`
13
+ * * `crc32(true)` is equivalent to `crc32(['true'])`
14
+ * * `crc32(['foo', 123, true])` produces the same result as `crc32('foo123true')`
15
+ * * Nested arrays of strings are flattened, so `crc32([['foo', 'bar'], 'baz'])` is equivalent to `crc32(['foobar', 'baz'])`
16
+ *
17
+ * @returns The CRC-32 checksum, returned as a hexadecimal string.
18
+ */
19
+ export default function crc32(inputData: CRCData): string;
20
+ /**
21
+ * The input data for the CRC-32 checksum calculation.
22
+ * Can be a single value (string, number, boolean), an array of strings, or an array of all of the above types.
23
+ */
24
+ export type CRCData = CRCValue | Array<CRCValue>;
25
+ type CRCValue = string | number | boolean | Array<string>;
26
+ export {};
package/src/crc32.js ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/crc32
7
+ */
8
+ /**
9
+ * Generates a CRC lookup table.
10
+ * This function creates and returns a 256-element array of pre-computed CRC values for quick CRC calculation.
11
+ * It uses the polynomial 0xEDB88320 to compute each value in the loop, optimizing future CRC calculations.
12
+ */
13
+ function makeCrcTable() {
14
+ const crcTable = [];
15
+ for (let n = 0; n < 256; n++) {
16
+ let c = n;
17
+ for (let k = 0; k < 8; k++) {
18
+ if (c & 1) {
19
+ c = 0xEDB88320 ^ (c >>> 1);
20
+ }
21
+ else {
22
+ c = c >>> 1;
23
+ }
24
+ }
25
+ crcTable[n] = c;
26
+ }
27
+ return crcTable;
28
+ }
29
+ /**
30
+ * Calculates CRC-32 checksum for a given inputData to verify the integrity of data.
31
+ *
32
+ * @param inputData Accepts a single value (string, number, boolean), an array of strings, or an array of all of the above types.
33
+ * Non-string values are converted to strings before calculating the checksum.
34
+ * The checksum calculation is based on the concatenated string representation of the input values:
35
+ * * `crc32('foo')` is equivalent to `crc32(['foo'])`
36
+ * * `crc32(123)` is equivalent to `crc32(['123'])`
37
+ * * `crc32(true)` is equivalent to `crc32(['true'])`
38
+ * * `crc32(['foo', 123, true])` produces the same result as `crc32('foo123true')`
39
+ * * Nested arrays of strings are flattened, so `crc32([['foo', 'bar'], 'baz'])` is equivalent to `crc32(['foobar', 'baz'])`
40
+ *
41
+ * @returns The CRC-32 checksum, returned as a hexadecimal string.
42
+ */
43
+ export default function crc32(inputData) {
44
+ const dataArray = Array.isArray(inputData) ? inputData : [inputData];
45
+ const crcTable = makeCrcTable();
46
+ let crc = 0 ^ (-1);
47
+ // Convert data to a single string.
48
+ const dataString = dataArray.map(item => {
49
+ if (Array.isArray(item)) {
50
+ return item.join('');
51
+ }
52
+ return String(item);
53
+ }).join('');
54
+ // Calculate the CRC for the resulting string.
55
+ for (let i = 0; i < dataString.length; i++) {
56
+ const byte = dataString.charCodeAt(i);
57
+ crc = (crc >>> 8) ^ crcTable[(crc ^ byte) & 0xFF];
58
+ }
59
+ crc = (crc ^ (-1)) >>> 0; // Force unsigned integer.
60
+ return crc.toString(16).padStart(8, '0');
61
+ }
package/src/delay.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/delay
7
+ */
8
+ /**
9
+ * Returns a function wrapper that will trigger a function after a specified wait time.
10
+ * The timeout can be canceled by calling the cancel function on the returned wrapped function.
11
+ *
12
+ * @param func The function to wrap.
13
+ * @param wait The timeout in ms.
14
+ */
15
+ export default function delay<T extends (...args: Array<any>) => any>(func: T, wait: number): DelayedFunc<T>;
16
+ export interface DelayedFunc<T extends (...args: Array<any>) => any> {
17
+ (...args: Parameters<T>): void;
18
+ cancel(): void;
19
+ }
package/src/delay.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/delay
7
+ */
8
+ /* globals setTimeout, clearTimeout */
9
+ /**
10
+ * Returns a function wrapper that will trigger a function after a specified wait time.
11
+ * The timeout can be canceled by calling the cancel function on the returned wrapped function.
12
+ *
13
+ * @param func The function to wrap.
14
+ * @param wait The timeout in ms.
15
+ */
16
+ export default function delay(func, wait) {
17
+ let timer;
18
+ function delayed(...args) {
19
+ delayed.cancel();
20
+ timer = setTimeout(() => func(...args), wait);
21
+ }
22
+ delayed.cancel = () => {
23
+ clearTimeout(timer);
24
+ };
25
+ return delayed;
26
+ }
package/src/diff.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * Calculates the difference between two arrays or strings producing an array containing a list of changes
7
+ * necessary to transform input into output.
8
+ *
9
+ * ```ts
10
+ * diff( 'aba', 'acca' ); // [ 'equal', 'insert', 'insert', 'delete', 'equal' ]
11
+ * ```
12
+ *
13
+ * This function is based on the "O(NP) Sequence Comparison Algorithm" by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
14
+ * Unfortunately, while it gives the most precise results, its to complex for longer strings/arrow (above 200 items).
15
+ * Therefore, `diff()` automatically switches to {@link module:utils/fastdiff~fastDiff `fastDiff()`} when detecting
16
+ * such a scenario. The return formats of both functions are identical.
17
+ *
18
+ * @param a Input array or string.
19
+ * @param b Output array or string.
20
+ * @param cmp Optional function used to compare array values, by default === is used.
21
+ * @returns Array of changes.
22
+ */
23
+ declare function diff<T>(a: ArrayLike<T>, b: ArrayLike<T>, cmp?: (a: T, b: T) => boolean): Array<DiffResult>;
24
+ declare namespace diff {
25
+ var fastDiff: typeof import("./fastdiff.js").default;
26
+ }
27
+ export default diff;
28
+ /**
29
+ * The element of the result of {@link module:utils/diff~diff} function.
30
+ */
31
+ export type DiffResult = 'equal' | 'insert' | 'delete';
package/src/diff.js ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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/diff
7
+ */
8
+ import fastDiff from './fastdiff.js';
9
+ // The following code is based on the "O(NP) Sequence Comparison Algorithm"
10
+ // by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
11
+ /**
12
+ * Calculates the difference between two arrays or strings producing an array containing a list of changes
13
+ * necessary to transform input into output.
14
+ *
15
+ * ```ts
16
+ * diff( 'aba', 'acca' ); // [ 'equal', 'insert', 'insert', 'delete', 'equal' ]
17
+ * ```
18
+ *
19
+ * This function is based on the "O(NP) Sequence Comparison Algorithm" by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
20
+ * Unfortunately, while it gives the most precise results, its to complex for longer strings/arrow (above 200 items).
21
+ * Therefore, `diff()` automatically switches to {@link module:utils/fastdiff~fastDiff `fastDiff()`} when detecting
22
+ * such a scenario. The return formats of both functions are identical.
23
+ *
24
+ * @param a Input array or string.
25
+ * @param b Output array or string.
26
+ * @param cmp Optional function used to compare array values, by default === is used.
27
+ * @returns Array of changes.
28
+ */
29
+ export default function diff(a, b, cmp) {
30
+ // Set the comparator function.
31
+ cmp = cmp || function (a, b) {
32
+ return a === b;
33
+ };
34
+ const aLength = a.length;
35
+ const bLength = b.length;
36
+ // Perform `fastDiff` for longer strings/arrays (see #269).
37
+ if (aLength > 200 || bLength > 200 || aLength + bLength > 300) {
38
+ return diff.fastDiff(a, b, cmp, true);
39
+ }
40
+ // Temporary action type statics.
41
+ let _insert, _delete;
42
+ // Swapped the arrays to use the shorter one as the first one.
43
+ if (bLength < aLength) {
44
+ const tmp = a;
45
+ a = b;
46
+ b = tmp;
47
+ // We swap the action types as well.
48
+ _insert = 'delete';
49
+ _delete = 'insert';
50
+ }
51
+ else {
52
+ _insert = 'insert';
53
+ _delete = 'delete';
54
+ }
55
+ const m = a.length;
56
+ const n = b.length;
57
+ const delta = n - m;
58
+ // Edit scripts, for each diagonal.
59
+ const es = {};
60
+ // Furthest points, the furthest y we can get on each diagonal.
61
+ const fp = {};
62
+ function snake(k) {
63
+ // We use -1 as an alternative below to handle initial values ( instead of filling the fp with -1 first ).
64
+ // Furthest points (y) on the diagonal below k.
65
+ const y1 = (fp[k - 1] !== undefined ? fp[k - 1] : -1) + 1;
66
+ // Furthest points (y) on the diagonal above k.
67
+ const y2 = fp[k + 1] !== undefined ? fp[k + 1] : -1;
68
+ // The way we should go to get further.
69
+ const dir = y1 > y2 ? -1 : 1;
70
+ // Clone previous changes array (if any).
71
+ if (es[k + dir]) {
72
+ es[k] = es[k + dir].slice(0);
73
+ }
74
+ // Create changes array.
75
+ if (!es[k]) {
76
+ es[k] = [];
77
+ }
78
+ // Push the action.
79
+ es[k].push(y1 > y2 ? _insert : _delete);
80
+ // Set the beginning coordinates.
81
+ let y = Math.max(y1, y2);
82
+ let x = y - k;
83
+ // Traverse the diagonal as long as the values match.
84
+ while (x < m && y < n && cmp(a[x], b[y])) {
85
+ x++;
86
+ y++;
87
+ // Push no change action.
88
+ es[k].push('equal');
89
+ }
90
+ return y;
91
+ }
92
+ let p = 0;
93
+ let k;
94
+ // Traverse the graph until we reach the end of the longer string.
95
+ do {
96
+ // Updates furthest points and edit scripts for diagonals below delta.
97
+ for (k = -p; k < delta; k++) {
98
+ fp[k] = snake(k);
99
+ }
100
+ // Updates furthest points and edit scripts for diagonals above delta.
101
+ for (k = delta + p; k > delta; k--) {
102
+ fp[k] = snake(k);
103
+ }
104
+ // Updates furthest point and edit script for the delta diagonal.
105
+ // note that the delta diagonal is the one which goes through the sink (m, n).
106
+ fp[delta] = snake(delta);
107
+ p++;
108
+ } while (fp[delta] !== n);
109
+ // Return the final list of edit changes.
110
+ // We remove the first item that represents the action for the injected nulls.
111
+ return es[delta].slice(1);
112
+ }
113
+ // Store the API in static property to easily overwrite it in tests.
114
+ // Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
115
+ diff.fastDiff = fastDiff;