@ckeditor/ckeditor5-utils 35.3.2 → 35.4.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 (59) hide show
  1. package/package.json +5 -5
  2. package/src/areconnectedthroughproperties.js +5 -7
  3. package/src/ckeditorerror.js +51 -70
  4. package/src/collection.js +106 -148
  5. package/src/comparearrays.js +10 -8
  6. package/src/config.js +29 -83
  7. package/src/count.js +5 -3
  8. package/src/diff.js +7 -5
  9. package/src/difftochanges.js +17 -14
  10. package/src/dom/createelement.js +11 -9
  11. package/src/dom/emittermixin.js +43 -84
  12. package/src/dom/getancestors.js +2 -2
  13. package/src/dom/getborderwidths.js +2 -2
  14. package/src/dom/getcommonancestor.js +3 -3
  15. package/src/dom/getdatafromelement.js +2 -2
  16. package/src/dom/getpositionedancestor.js +1 -2
  17. package/src/dom/global.js +8 -10
  18. package/src/dom/indexof.js +2 -2
  19. package/src/dom/insertat.js +3 -3
  20. package/src/dom/iscomment.js +0 -3
  21. package/src/dom/isnode.js +0 -3
  22. package/src/dom/isrange.js +0 -3
  23. package/src/dom/istext.js +0 -3
  24. package/src/dom/isvisible.js +0 -3
  25. package/src/dom/iswindow.js +0 -3
  26. package/src/dom/position.js +110 -133
  27. package/src/dom/rect.js +42 -52
  28. package/src/dom/remove.js +1 -1
  29. package/src/dom/resizeobserver.js +10 -35
  30. package/src/dom/scroll.js +85 -91
  31. package/src/dom/setdatainelement.js +2 -2
  32. package/src/dom/tounit.js +1 -10
  33. package/src/elementreplacer.js +2 -2
  34. package/src/emittermixin.js +48 -48
  35. package/src/env.js +14 -75
  36. package/src/eventinfo.js +2 -2
  37. package/src/fastdiff.js +115 -96
  38. package/src/first.js +0 -3
  39. package/src/focustracker.js +10 -18
  40. package/src/index.js +17 -0
  41. package/src/inserttopriorityarray.js +2 -2
  42. package/src/isiterable.js +2 -2
  43. package/src/keyboard.js +20 -21
  44. package/src/keystrokehandler.js +26 -24
  45. package/src/language.js +1 -2
  46. package/src/locale.js +11 -14
  47. package/src/mapsequal.js +3 -3
  48. package/src/mix.js +15 -13
  49. package/src/nth.js +0 -4
  50. package/src/objecttomap.js +6 -4
  51. package/src/observablemixin.js +126 -150
  52. package/src/priorities.js +0 -9
  53. package/src/splicearray.js +12 -11
  54. package/src/spy.js +1 -1
  55. package/src/tomap.js +7 -5
  56. package/src/translation-service.js +70 -52
  57. package/src/uid.js +5 -3
  58. package/src/unicode.js +9 -15
  59. package/src/version.js +32 -26
package/src/dom/global.js CHANGED
@@ -2,23 +2,21 @@
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
- /* globals window, document */
6
- /**
7
- * @module utils/dom/global
8
- */
9
5
  /**
10
6
  * A helper (module) giving an access to the global DOM objects such as `window` and
11
7
  * `document`. Accessing these objects using this helper allows easy and bulletproof
12
8
  * testing, i.e. stubbing native properties:
13
9
  *
14
- * import global from 'ckeditor5/utils/dom/global.js';
10
+ * ```ts
11
+ * import { global } from 'ckeditor5/utils';
15
12
  *
16
- * // This stub will work for any code using global module.
17
- * testUtils.sinon.stub( global, 'window', {
18
- * innerWidth: 10000
19
- * } );
13
+ * // This stub will work for any code using global module.
14
+ * testUtils.sinon.stub( global, 'window', {
15
+ * innerWidth: 10000
16
+ * } );
20
17
  *
21
- * console.log( global.window.innerWidth );
18
+ * console.log( global.window.innerWidth );
19
+ * ```
22
20
  */
23
21
  let global;
24
22
  // In some environments window and document API might not be available.
@@ -8,8 +8,8 @@
8
8
  /**
9
9
  * Returns index of the node in the parent element.
10
10
  *
11
- * @param {Node} node Node which index is tested.
12
- * @returns {Number} Index of the node in the parent element. Returns 0 if node has no parent.
11
+ * @param node Node which index is tested.
12
+ * @returns Index of the node in the parent element. Returns 0 if node has no parent.
13
13
  */
14
14
  export default function indexOf(node) {
15
15
  let index = 0;
@@ -8,9 +8,9 @@
8
8
  /**
9
9
  * Inserts node to the parent at given index.
10
10
  *
11
- * @param {Element} parentElement Parent element.
12
- * @param {Number} index Insertions index.
13
- * @param {Node} nodeToInsert Node to insert.
11
+ * @param parentElement Parent element.
12
+ * @param index Insertions index.
13
+ * @param nodeToInsert Node to insert.
14
14
  */
15
15
  export default function insertAt(parentElement, index, nodeToInsert) {
16
16
  parentElement.insertBefore(nodeToInsert, parentElement.childNodes[index] || null);
@@ -8,9 +8,6 @@
8
8
  */
9
9
  /**
10
10
  * Checks whether the object is a native DOM Comment node.
11
- *
12
- * @param {*} obj
13
- * @returns {Boolean}
14
11
  */
15
12
  export default function isComment(obj) {
16
13
  return obj && obj.nodeType === Node.COMMENT_NODE;
package/src/dom/isnode.js CHANGED
@@ -7,9 +7,6 @@
7
7
  */
8
8
  /**
9
9
  * Checks if the object is a native DOM Node.
10
- *
11
- * @param {*} obj
12
- * @returns {Boolean}
13
10
  */
14
11
  export default function isNode(obj) {
15
12
  if (obj) {
@@ -7,9 +7,6 @@
7
7
  */
8
8
  /**
9
9
  * Checks if the object is a native DOM Range.
10
- *
11
- * @param {*} obj
12
- * @returns {Boolean}
13
10
  */
14
11
  export default function isRange(obj) {
15
12
  return Object.prototype.toString.apply(obj) == '[object Range]';
package/src/dom/istext.js CHANGED
@@ -7,9 +7,6 @@
7
7
  */
8
8
  /**
9
9
  * Checks if the object is a native DOM Text node.
10
- *
11
- * @param {*} obj
12
- * @returns {Boolean}
13
10
  */
14
11
  export default function isText(obj) {
15
12
  return Object.prototype.toString.call(obj) == '[object Text]';
@@ -14,9 +14,6 @@
14
14
  *
15
15
  * **Note**: This helper does not check whether the element is hidden by cropping, overflow, etc..
16
16
  * To check that, use {@link module:utils/dom/rect~Rect} instead.
17
- *
18
- * @param {HTMLElement|null|undefined} element
19
- * @returns {Boolean}
20
17
  */
21
18
  export default function isVisible(element) {
22
19
  return !!(element && element.getClientRects && element.getClientRects().length);
@@ -7,9 +7,6 @@
7
7
  */
8
8
  /**
9
9
  * Checks if the object is a native DOM Window.
10
- *
11
- * @param {*} obj
12
- * @returns {Boolean}
13
10
  */
14
11
  export default function isWindow(obj) {
15
12
  const stringifiedObject = Object.prototype.toString.apply(obj);
@@ -10,70 +10,71 @@ import Rect from './rect';
10
10
  import getPositionedAncestor from './getpositionedancestor';
11
11
  import getBorderWidths from './getborderwidths';
12
12
  import { isFunction } from 'lodash-es';
13
- // @if CK_DEBUG_POSITION // import { RectDrawer } from '@ckeditor/ckeditor5-minimap/src/utils';
13
+ // @if CK_DEBUG_POSITION // const { RectDrawer } = require( '@ckeditor/ckeditor5-minimap/src/utils' );
14
14
  /**
15
15
  * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
16
16
  * target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
17
17
  * into consideration.
18
18
  *
19
- * // The element which is to be positioned.
20
- * const element = document.body.querySelector( '#toolbar' );
19
+ * ```ts
20
+ * // The element which is to be positioned.
21
+ * const element = document.body.querySelector( '#toolbar' );
21
22
  *
22
- * // A target to which the element is positioned relatively.
23
- * const target = document.body.querySelector( '#container' );
23
+ * // A target to which the element is positioned relatively.
24
+ * const target = document.body.querySelector( '#container' );
24
25
  *
25
- * // Finding the optimal coordinates for the positioning.
26
- * const { left, top, name } = getOptimalPosition( {
27
- * element: element,
28
- * target: target,
26
+ * // Finding the optimal coordinates for the positioning.
27
+ * const { left, top, name } = getOptimalPosition( {
28
+ * element: element,
29
+ * target: target,
29
30
  *
30
- * // The algorithm will chose among these positions to meet the requirements such
31
- * // as "limiter" element or "fitInViewport", set below. The positions are considered
32
- * // in the order of the array.
33
- * positions: [
34
- * //
35
- * // [ Target ]
36
- * // +-----------------+
37
- * // | Element |
38
- * // +-----------------+
39
- * //
40
- * targetRect => ( {
41
- * top: targetRect.bottom,
42
- * left: targetRect.left,
43
- * name: 'mySouthEastPosition'
44
- * } ),
31
+ * // The algorithm will chose among these positions to meet the requirements such
32
+ * // as "limiter" element or "fitInViewport", set below. The positions are considered
33
+ * // in the order of the array.
34
+ * positions: [
35
+ * //
36
+ * // [ Target ]
37
+ * // +-----------------+
38
+ * // | Element |
39
+ * // +-----------------+
40
+ * //
41
+ * targetRect => ( {
42
+ * top: targetRect.bottom,
43
+ * left: targetRect.left,
44
+ * name: 'mySouthEastPosition'
45
+ * } ),
45
46
  *
46
- * //
47
- * // +-----------------+
48
- * // | Element |
49
- * // +-----------------+
50
- * // [ Target ]
51
- * //
52
- * ( targetRect, elementRect ) => ( {
53
- * top: targetRect.top - elementRect.height,
54
- * left: targetRect.left,
55
- * name: 'myNorthEastPosition'
56
- * } )
57
- * ],
47
+ * //
48
+ * // +-----------------+
49
+ * // | Element |
50
+ * // +-----------------+
51
+ * // [ Target ]
52
+ * //
53
+ * ( targetRect, elementRect ) => ( {
54
+ * top: targetRect.top - elementRect.height,
55
+ * left: targetRect.left,
56
+ * name: 'myNorthEastPosition'
57
+ * } )
58
+ * ],
58
59
  *
59
- * // Find a position such guarantees the element remains within visible boundaries of <body>.
60
- * limiter: document.body,
60
+ * // Find a position such guarantees the element remains within visible boundaries of <body>.
61
+ * limiter: document.body,
61
62
  *
62
- * // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
63
- * fitInViewport: true
64
- * } );
63
+ * // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
64
+ * fitInViewport: true
65
+ * } );
65
66
  *
66
- * // The best position which fits into document.body and the viewport. May be useful
67
- * // to set proper class on the `element`.
68
- * console.log( name ); // -> "myNorthEastPosition"
67
+ * // The best position which fits into document.body and the viewport. May be useful
68
+ * // to set proper class on the `element`.
69
+ * console.log( name ); // -> "myNorthEastPosition"
69
70
  *
70
- * // Using the absolute coordinates which has been found to position the element
71
- * // as in the diagram depicting the "myNorthEastPosition" position.
72
- * element.style.top = top;
73
- * element.style.left = left;
71
+ * // Using the absolute coordinates which has been found to position the element
72
+ * // as in the diagram depicting the "myNorthEastPosition" position.
73
+ * element.style.top = top;
74
+ * element.style.left = left;
75
+ * ```
74
76
  *
75
- * @param {module:utils/dom/position~Options} options The input data and configuration of the helper.
76
- * @returns {module:utils/dom/position~Position}
77
+ * @param options The input data and configuration of the helper.
77
78
  */
78
79
  export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
79
80
  // If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
@@ -113,11 +114,9 @@ export function getOptimalPosition({ element, target, positions, limiter, fitInV
113
114
  }
114
115
  return bestPosition;
115
116
  }
116
- // Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
117
- //
118
- // @private
119
- // @param {Object} An object containing viewportOffset config.
120
- // @returns {module:utils/dom/rect~Rect} A shrunken rect of the viewport.
117
+ /**
118
+ * Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
119
+ */
121
120
  function getConstrainedViewportRect(viewportOffsetConfig) {
122
121
  viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
123
122
  const viewportRect = new Rect(global.window);
@@ -127,21 +126,10 @@ function getConstrainedViewportRect(viewportOffsetConfig) {
127
126
  viewportRect.height -= viewportOffsetConfig.bottom;
128
127
  return viewportRect;
129
128
  }
130
- // For a given array of positioning functions, returns such that provides the best
131
- // fit of the `elementRect` into the `limiterRect` and `viewportRect`.
132
- //
133
- // @private
134
- //
135
- // @param {module:utils/dom/position~Options#positions} positions Functions returning
136
- // {@link module:utils/dom/position~Position}to be checked, in the order of preference.
137
- // @param {Object} options
138
- // @param {module:utils/dom/rect~Rect} options.elementRect The positioned element rect.
139
- // @param {module:utils/dom/rect~Rect} options.targetRect The target element rect.
140
- // @param {module:utils/dom/rect~Rect} options.viewportRect The viewport rect.
141
- // @param {module:utils/dom/rect~Rect} [options.limiterRect] The limiter rect.
142
- // @param {HTMLElement|null} [options.positionedElementAncestor] Nearest element ancestor element which CSS position is not "static".
143
- //
144
- // @returns {module:utils/dom/position~Position|null} An array containing the name of the position and it's rect.
129
+ /**
130
+ * For a given array of positioning functions, returns such that provides the best
131
+ * fit of the `elementRect` into the `limiterRect` and `viewportRect`.
132
+ */
145
133
  function getBestPosition(positions, options) {
146
134
  const { elementRect } = options;
147
135
  // This is when element is fully visible.
@@ -169,17 +157,14 @@ function getBestPosition(positions, options) {
169
157
  }
170
158
  return bestPosition;
171
159
  }
172
- // For a given absolute Rect coordinates object and a positioned element ancestor, it updates its
173
- // coordinates that make up for the position and the scroll of the ancestor.
174
- //
175
- // This is necessary because while Rects (and DOMRects) are relative to the browser's viewport, their coordinates
176
- // are used in real–life to position elements with `position: absolute`, which are scoped by any positioned
177
- // (and scrollable) ancestors.
178
- //
179
- // @private
180
- //
181
- // @param {module:utils/dom/rect~Rect} rect A rect with absolute rect coordinates.
182
- // @param {HTMLElement} positionedElementAncestor An ancestor element that should be considered.
160
+ /**
161
+ * For a given absolute Rect coordinates object and a positioned element ancestor, it updates its
162
+ * coordinates that make up for the position and the scroll of the ancestor.
163
+ *
164
+ * This is necessary because while Rects (and DOMRects) are relative to the browser's viewport, their coordinates
165
+ * are used in real–life to position elements with `position: absolute`, which are scoped by any positioned
166
+ * (and scrollable) ancestors.
167
+ */
183
168
  function shiftRectToCompensatePositionedAncestor(rect, positionedElementAncestor) {
184
169
  const ancestorPosition = getRectForAbsolutePositioning(new Rect(positionedElementAncestor));
185
170
  const ancestorBorderWidths = getBorderWidths(positionedElementAncestor);
@@ -207,34 +192,34 @@ function shiftRectToCompensatePositionedAncestor(rect, positionedElementAncestor
207
192
  moveY -= ancestorBorderWidths.top;
208
193
  rect.moveBy(moveX, moveY);
209
194
  }
210
- // DOMRect (also Rect) works in a scroll–independent geometry but `position: absolute` doesn't.
211
- // This function converts Rect to `position: absolute` coordinates.
212
- //
213
- // @private
214
- // @param {module:utils/dom/rect~Rect} rect A rect to be converted.
215
- // @returns {module:utils/dom/rect~Rect} Object containing `left` and `top` properties, in absolute coordinates.
195
+ /**
196
+ * DOMRect (also Rect) works in a scroll–independent geometry but `position: absolute` doesn't.
197
+ * This function converts Rect to `position: absolute` coordinates.
198
+ */
216
199
  function getRectForAbsolutePositioning(rect) {
217
200
  const { scrollX, scrollY } = global.window;
218
201
  return rect.clone().moveBy(scrollX, scrollY);
219
202
  }
220
- // A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
221
- //
222
- // {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
223
- // translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
224
- // in DOM, they will make it display it in the right place in the viewport.
225
- // @private
226
- // @implements {Position}
203
+ /**
204
+ * A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
205
+ *
206
+ * {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
207
+ * translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
208
+ * in DOM, they will make it display it in the right place in the viewport.
209
+ */
227
210
  class PositionObject {
228
- // Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
229
- //
230
- // @param {module:utils/dom/position~PositioningFunction} positioningFunction function The function that defines the expected
231
- // coordinates the positioned element should move to.
232
- // @param {Object} [options] options object.
233
- // @param {module:utils/dom/rect~Rect} options.elementRect The positioned element rect.
234
- // @param {module:utils/dom/rect~Rect} options.targetRect The target element rect.
235
- // @param {module:utils/dom/rect~Rect|null} options.viewportRect The viewport rect.
236
- // @param {module:utils/dom/rect~Rect} [options.limiterRect] The limiter rect.
237
- // @param {HTMLElement|null} [options.positionedElementAncestor] Nearest element ancestor element which CSS position is not "static".
211
+ /**
212
+ * Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
213
+ *
214
+ * @param positioningFunction function The function that defines the expected
215
+ * coordinates the positioned element should move to.
216
+ * @param options options object.
217
+ * @param options.elementRect The positioned element rect.
218
+ * @param options.targetRect The target element rect.
219
+ * @param options.viewportRect The viewport rect.
220
+ * @param options.limiterRect The limiter rect.
221
+ * @param options.positionedElementAncestor Nearest element ancestor element which CSS position is not "static".
222
+ */
238
223
  constructor(positioningFunction, options) {
239
224
  const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect);
240
225
  // Nameless position for a function that didn't participate.
@@ -247,26 +232,23 @@ class PositionObject {
247
232
  this._positioningFunctionCorrdinates = { left, top };
248
233
  this._options = options;
249
234
  }
250
- // The left value in pixels in the CSS `position: absolute` coordinate system.
251
- // Set it on the positioned element in DOM to move it to the position.
252
- //
253
- // @readonly
254
- // @type {Number}
235
+ /**
236
+ * The left value in pixels in the CSS `position: absolute` coordinate system.
237
+ * Set it on the positioned element in DOM to move it to the position.
238
+ */
255
239
  get left() {
256
240
  return this._absoluteRect.left;
257
241
  }
258
- // The top value in pixels in the CSS `position: absolute` coordinate system.
259
- // Set it on the positioned element in DOM to move it to the position.
260
- //
261
- // @readonly
262
- // @type {Number}
242
+ /**
243
+ * The top value in pixels in the CSS `position: absolute` coordinate system.
244
+ * Set it on the positioned element in DOM to move it to the position.
245
+ */
263
246
  get top() {
264
247
  return this._absoluteRect.top;
265
248
  }
266
- // An intersection area between positioned element and limiter within viewport constraints.
267
- //
268
- // @readonly
269
- // @type {Number}
249
+ /**
250
+ * An intersection area between positioned element and limiter within viewport constraints.
251
+ */
270
252
  get limiterIntersectionArea() {
271
253
  const limiterRect = this._options.limiterRect;
272
254
  if (limiterRect) {
@@ -286,10 +268,9 @@ class PositionObject {
286
268
  }
287
269
  return 0;
288
270
  }
289
- // An intersection area between positioned element and viewport.
290
- //
291
- // @readonly
292
- // @type {Number}
271
+ /**
272
+ * An intersection area between positioned element and viewport.
273
+ */
293
274
  get viewportIntersectionArea() {
294
275
  const viewportRect = this._options.viewportRect;
295
276
  if (viewportRect) {
@@ -297,12 +278,10 @@ class PositionObject {
297
278
  }
298
279
  return 0;
299
280
  }
300
- // An already positioned element rect. A clone of the element rect passed to the constructor
301
- // but placed in the viewport according to the positioning function.
302
- //
303
- // @private
304
- // @readonly
305
- // @type {module:utils/dom/rect~Rect}
281
+ /**
282
+ * An already positioned element rect. A clone of the element rect passed to the constructor
283
+ * but placed in the viewport according to the positioning function.
284
+ */
306
285
  get _rect() {
307
286
  if (this._cachedRect) {
308
287
  return this._cachedRect;
@@ -310,11 +289,9 @@ class PositionObject {
310
289
  this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCorrdinates.left, this._positioningFunctionCorrdinates.top);
311
290
  return this._cachedRect;
312
291
  }
313
- // An already absolutely positioned element rect. See ({@link #_rect}).
314
- //
315
- // @private
316
- // @readonly
317
- // @type {module:utils/dom/rect~Rect}
292
+ /**
293
+ * An already absolutely positioned element rect. See ({@link #_rect}).
294
+ */
318
295
  get _absoluteRect() {
319
296
  if (this._cachedAbsoluteRect) {
320
297
  return this._cachedAbsoluteRect;