@ckeditor/ckeditor5-utils 35.0.0 → 35.0.1

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 +75 -0
  3. package/src/ckeditorerror.js +195 -0
  4. package/src/collection.js +619 -0
  5. package/src/comparearrays.js +45 -0
  6. package/src/config.js +216 -0
  7. package/src/count.js +22 -0
  8. package/src/diff.js +113 -0
  9. package/src/difftochanges.js +76 -0
  10. package/src/dom/createelement.js +41 -0
  11. package/src/dom/emittermixin.js +301 -0
  12. package/src/dom/getancestors.js +27 -0
  13. package/src/dom/getborderwidths.js +24 -0
  14. package/src/dom/getcommonancestor.js +25 -0
  15. package/src/dom/getdatafromelement.js +20 -0
  16. package/src/dom/getpositionedancestor.js +23 -0
  17. package/src/dom/global.js +23 -0
  18. package/src/dom/indexof.js +21 -0
  19. package/src/dom/insertat.js +17 -0
  20. package/src/dom/iscomment.js +17 -0
  21. package/src/dom/isnode.js +24 -0
  22. package/src/dom/isrange.js +16 -0
  23. package/src/dom/istext.js +16 -0
  24. package/src/dom/isvisible.js +23 -0
  25. package/src/dom/iswindow.js +25 -0
  26. package/src/dom/position.js +328 -0
  27. package/src/dom/rect.js +364 -0
  28. package/src/dom/remove.js +18 -0
  29. package/src/dom/resizeobserver.js +145 -0
  30. package/src/dom/scroll.js +270 -0
  31. package/src/dom/setdatainelement.js +20 -0
  32. package/src/dom/tounit.js +25 -0
  33. package/src/elementreplacer.js +43 -0
  34. package/src/emittermixin.js +471 -0
  35. package/src/env.js +168 -0
  36. package/src/eventinfo.js +26 -0
  37. package/src/fastdiff.js +229 -0
  38. package/src/first.js +20 -0
  39. package/src/focustracker.js +103 -0
  40. package/src/index.js +36 -0
  41. package/src/inserttopriorityarray.js +21 -0
  42. package/src/isiterable.js +16 -0
  43. package/src/keyboard.js +222 -0
  44. package/src/keystrokehandler.js +114 -0
  45. package/src/language.js +20 -0
  46. package/src/locale.js +79 -0
  47. package/src/mapsequal.js +27 -0
  48. package/src/mix.js +44 -0
  49. package/src/nth.js +28 -0
  50. package/src/objecttomap.js +25 -0
  51. package/src/observablemixin.js +605 -0
  52. package/src/priorities.js +32 -0
  53. package/src/spy.js +22 -0
  54. package/src/toarray.js +7 -0
  55. package/src/tomap.js +27 -0
  56. package/src/translation-service.js +180 -0
  57. package/src/uid.js +55 -0
  58. package/src/unicode.js +91 -0
  59. package/src/version.js +148 -0
@@ -0,0 +1,328 @@
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
+ * @module utils/dom/position
7
+ */
8
+ import global from './global';
9
+ import Rect from './rect';
10
+ import getPositionedAncestor from './getpositionedancestor';
11
+ import getBorderWidths from './getborderwidths';
12
+ import { isFunction } from 'lodash-es';
13
+ // @if CK_DEBUG_POSITION // import { RectDrawer } from '@ckeditor/ckeditor5-minimap/src/utils';
14
+ /**
15
+ * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
16
+ * target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
17
+ * into consideration.
18
+ *
19
+ * // The element which is to be positioned.
20
+ * const element = document.body.querySelector( '#toolbar' );
21
+ *
22
+ * // A target to which the element is positioned relatively.
23
+ * const target = document.body.querySelector( '#container' );
24
+ *
25
+ * // Finding the optimal coordinates for the positioning.
26
+ * const { left, top, name } = getOptimalPosition( {
27
+ * element: element,
28
+ * target: target,
29
+ *
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
+ * } ),
45
+ *
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
+ * ],
58
+ *
59
+ * // Find a position such guarantees the element remains within visible boundaries of <body>.
60
+ * limiter: document.body,
61
+ *
62
+ * // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
63
+ * fitInViewport: true
64
+ * } );
65
+ *
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"
69
+ *
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;
74
+ *
75
+ * @param {module:utils/dom/position~Options} options The input data and configuration of the helper.
76
+ * @returns {module:utils/dom/position~Position}
77
+ */
78
+ export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
79
+ // If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
80
+ // https://github.com/ckeditor/ckeditor5-utils/issues/157
81
+ if (isFunction(target)) {
82
+ target = target();
83
+ }
84
+ // If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
85
+ // https://github.com/ckeditor/ckeditor5-ui/issues/260
86
+ if (isFunction(limiter)) {
87
+ limiter = limiter();
88
+ }
89
+ const positionedElementAncestor = getPositionedAncestor(element);
90
+ const elementRect = new Rect(element);
91
+ const targetRect = new Rect(target);
92
+ let bestPosition;
93
+ // @if CK_DEBUG_POSITION // RectDrawer.clear();
94
+ // @if CK_DEBUG_POSITION // RectDrawer.draw( targetRect, { outlineWidth: '5px' }, 'Target' );
95
+ const viewportRect = fitInViewport && getConstrainedViewportRect(viewportOffsetConfig) || null;
96
+ const positionOptions = { targetRect, elementRect, positionedElementAncestor, viewportRect };
97
+ // If there are no limits, just grab the very first position and be done with that drama.
98
+ if (!limiter && !fitInViewport) {
99
+ bestPosition = new PositionObject(positions[0], positionOptions);
100
+ }
101
+ else {
102
+ const limiterRect = limiter && new Rect(limiter).getVisible();
103
+ // @if CK_DEBUG_POSITION // if ( viewportRect ) {
104
+ // @if CK_DEBUG_POSITION // RectDrawer.draw( viewportRect, { outlineWidth: '5px' }, 'Viewport' );
105
+ // @if CK_DEBUG_POSITION // }
106
+ // @if CK_DEBUG_POSITION // if ( limiter ) {
107
+ // @if CK_DEBUG_POSITION // RectDrawer.draw( limiterRect, { outlineWidth: '5px', outlineColor: 'green' }, 'Visible limiter' );
108
+ // @if CK_DEBUG_POSITION // }
109
+ Object.assign(positionOptions, { limiterRect, viewportRect });
110
+ // If there's no best position found, i.e. when all intersections have no area because
111
+ // rects have no width or height, then just use the first available position.
112
+ bestPosition = getBestPosition(positions, positionOptions) || new PositionObject(positions[0], positionOptions);
113
+ }
114
+ return bestPosition;
115
+ }
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.
121
+ function getConstrainedViewportRect(viewportOffsetConfig) {
122
+ viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
123
+ const viewportRect = new Rect(global.window);
124
+ viewportRect.top += viewportOffsetConfig.top;
125
+ viewportRect.height -= viewportOffsetConfig.top;
126
+ viewportRect.bottom -= viewportOffsetConfig.bottom;
127
+ viewportRect.height -= viewportOffsetConfig.bottom;
128
+ return viewportRect;
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
+ //
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.
145
+ function getBestPosition(positions, options) {
146
+ const { elementRect } = options;
147
+ // This is when element is fully visible.
148
+ const elementRectArea = elementRect.getArea();
149
+ const positionInstances = positions
150
+ .map(positioningFunction => new PositionObject(positioningFunction, options))
151
+ // Some positioning functions may return `null` if they don't want to participate.
152
+ .filter(position => !!position.name);
153
+ let maxFitFactor = 0;
154
+ let bestPosition = null;
155
+ for (const position of positionInstances) {
156
+ const { limiterIntersectionArea, viewportIntersectionArea } = position;
157
+ // If a such position is found that element is fully contained by the limiter then, obviously,
158
+ // there will be no better one, so finishing.
159
+ if (limiterIntersectionArea === elementRectArea) {
160
+ return position;
161
+ }
162
+ // To maximize both viewport and limiter intersection areas we use distance on _viewportIntersectionArea
163
+ // and _limiterIntersectionArea plane (without sqrt because we are looking for max value).
164
+ const fitFactor = viewportIntersectionArea ** 2 + limiterIntersectionArea ** 2;
165
+ if (fitFactor > maxFitFactor) {
166
+ maxFitFactor = fitFactor;
167
+ bestPosition = position;
168
+ }
169
+ }
170
+ return bestPosition;
171
+ }
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.
183
+ function shiftRectToCompensatePositionedAncestor(rect, positionedElementAncestor) {
184
+ const ancestorPosition = getRectForAbsolutePositioning(new Rect(positionedElementAncestor));
185
+ const ancestorBorderWidths = getBorderWidths(positionedElementAncestor);
186
+ let moveX = 0;
187
+ let moveY = 0;
188
+ // (https://github.com/ckeditor/ckeditor5-ui-default/issues/126)
189
+ // If there's some positioned ancestor of the panel, then its `Rect` must be taken into
190
+ // consideration. `Rect` is always relative to the viewport while `position: absolute` works
191
+ // with respect to that positioned ancestor.
192
+ moveX -= ancestorPosition.left;
193
+ moveY -= ancestorPosition.top;
194
+ // (https://github.com/ckeditor/ckeditor5-utils/issues/139)
195
+ // If there's some positioned ancestor of the panel, not only its position must be taken into
196
+ // consideration (see above) but also its internal scrolls. Scroll have an impact here because `Rect`
197
+ // is relative to the viewport (it doesn't care about scrolling), while `position: absolute`
198
+ // must compensate that scrolling.
199
+ moveX += positionedElementAncestor.scrollLeft;
200
+ moveY += positionedElementAncestor.scrollTop;
201
+ // (https://github.com/ckeditor/ckeditor5-utils/issues/139)
202
+ // If there's some positioned ancestor of the panel, then its `Rect` includes its CSS `borderWidth`
203
+ // while `position: absolute` positioning does not consider it.
204
+ // E.g. `{ position: absolute, top: 0, left: 0 }` means upper left corner of the element,
205
+ // not upper-left corner of its border.
206
+ moveX -= ancestorBorderWidths.left;
207
+ moveY -= ancestorBorderWidths.top;
208
+ rect.moveBy(moveX, moveY);
209
+ }
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.
216
+ function getRectForAbsolutePositioning(rect) {
217
+ const { scrollX, scrollY } = global.window;
218
+ return rect.clone().moveBy(scrollX, scrollY);
219
+ }
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}
227
+ 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".
238
+ constructor(positioningFunction, options) {
239
+ const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect);
240
+ // Nameless position for a function that didn't participate.
241
+ if (!positioningFunctionOutput) {
242
+ return;
243
+ }
244
+ const { left, top, name, config } = positioningFunctionOutput;
245
+ this.name = name;
246
+ this.config = config;
247
+ this._positioningFunctionCorrdinates = { left, top };
248
+ this._options = options;
249
+ }
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}
255
+ get left() {
256
+ return this._absoluteRect.left;
257
+ }
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}
263
+ get top() {
264
+ return this._absoluteRect.top;
265
+ }
266
+ // An intersection area between positioned element and limiter within viewport constraints.
267
+ //
268
+ // @readonly
269
+ // @type {Number}
270
+ get limiterIntersectionArea() {
271
+ const limiterRect = this._options.limiterRect;
272
+ if (limiterRect) {
273
+ const viewportRect = this._options.viewportRect;
274
+ if (viewportRect) {
275
+ // Consider only the part of the limiter which is visible in the viewport. So the limiter is getting limited.
276
+ const limiterViewportIntersectRect = limiterRect.getIntersection(viewportRect);
277
+ if (limiterViewportIntersectRect) {
278
+ // If the limiter is within the viewport, then check the intersection between that part of the
279
+ // limiter and actual position.
280
+ return limiterViewportIntersectRect.getIntersectionArea(this._rect);
281
+ }
282
+ }
283
+ else {
284
+ return limiterRect.getIntersectionArea(this._rect);
285
+ }
286
+ }
287
+ return 0;
288
+ }
289
+ // An intersection area between positioned element and viewport.
290
+ //
291
+ // @readonly
292
+ // @type {Number}
293
+ get viewportIntersectionArea() {
294
+ const viewportRect = this._options.viewportRect;
295
+ if (viewportRect) {
296
+ return viewportRect.getIntersectionArea(this._rect);
297
+ }
298
+ return 0;
299
+ }
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}
306
+ get _rect() {
307
+ if (this._cachedRect) {
308
+ return this._cachedRect;
309
+ }
310
+ this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCorrdinates.left, this._positioningFunctionCorrdinates.top);
311
+ return this._cachedRect;
312
+ }
313
+ // An already absolutely positioned element rect. See ({@link #_rect}).
314
+ //
315
+ // @private
316
+ // @readonly
317
+ // @type {module:utils/dom/rect~Rect}
318
+ get _absoluteRect() {
319
+ if (this._cachedAbsoluteRect) {
320
+ return this._cachedAbsoluteRect;
321
+ }
322
+ this._cachedAbsoluteRect = getRectForAbsolutePositioning(this._rect);
323
+ if (this._options.positionedElementAncestor) {
324
+ shiftRectToCompensatePositionedAncestor(this._cachedAbsoluteRect, this._options.positionedElementAncestor);
325
+ }
326
+ return this._cachedAbsoluteRect;
327
+ }
328
+ }
@@ -0,0 +1,364 @@
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
+ * @module utils/dom/rect
7
+ */
8
+ import isRange from './isrange';
9
+ import isWindow from './iswindow';
10
+ import getBorderWidths from './getborderwidths';
11
+ import isText from './istext';
12
+ import { isElement } from 'lodash-es';
13
+ const rectProperties = ['top', 'right', 'bottom', 'left', 'width', 'height'];
14
+ /**
15
+ * A helper class representing a `ClientRect` object, e.g. value returned by
16
+ * the native `object.getBoundingClientRect()` method. Provides a set of methods
17
+ * to manipulate the rect and compare it against other rect instances.
18
+ */
19
+ export default class Rect {
20
+ /**
21
+ * Creates an instance of rect.
22
+ *
23
+ * // Rect of an HTMLElement.
24
+ * const rectA = new Rect( document.body );
25
+ *
26
+ * // Rect of a DOM Range.
27
+ * const rectB = new Rect( document.getSelection().getRangeAt( 0 ) );
28
+ *
29
+ * // Rect of a window (web browser viewport).
30
+ * const rectC = new Rect( window );
31
+ *
32
+ * // Rect out of an object.
33
+ * const rectD = new Rect( { top: 0, right: 10, bottom: 10, left: 0, width: 10, height: 10 } );
34
+ *
35
+ * // Rect out of another Rect instance.
36
+ * const rectE = new Rect( rectD );
37
+ *
38
+ * // Rect out of a ClientRect.
39
+ * const rectF = new Rect( document.body.getClientRects().item( 0 ) );
40
+ *
41
+ * **Note**: By default a rect of an HTML element includes its CSS borders and scrollbars (if any)
42
+ * ant the rect of a `window` includes scrollbars too. Use {@link #excludeScrollbarsAndBorders}
43
+ * to get the inner part of the rect.
44
+ *
45
+ * @param {module:utils/dom/rect~RectSource} source A source object to create the rect.
46
+ */
47
+ constructor(source) {
48
+ const isSourceRange = isRange(source);
49
+ Object.defineProperty(this, '_source', {
50
+ // If the source is a Rect instance, copy it's #_source.
51
+ value: source._source || source,
52
+ writable: true,
53
+ enumerable: false
54
+ });
55
+ if (isDomElement(source) || isSourceRange) {
56
+ // The `Rect` class depends on `getBoundingClientRect` and `getClientRects` DOM methods. If the source
57
+ // of a rect in an HTML element or a DOM range but it does not belong to any rendered DOM tree, these methods
58
+ // will fail to obtain the geometry and the rect instance makes little sense to the features using it.
59
+ // To get rid of this warning make sure the source passed to the constructor is a descendant of `window.document.body`.
60
+ // @if CK_DEBUG // const sourceNode = isSourceRange ? source.startContainer : source;
61
+ // @if CK_DEBUG // if ( !sourceNode.ownerDocument || !sourceNode.ownerDocument.body.contains( sourceNode ) ) {
62
+ // @if CK_DEBUG // console.warn(
63
+ // @if CK_DEBUG // 'rect-source-not-in-dom: The source of this rect does not belong to any rendered DOM tree.',
64
+ // @if CK_DEBUG // { source } );
65
+ // @if CK_DEBUG // }
66
+ if (isSourceRange) {
67
+ const rangeRects = Rect.getDomRangeRects(source);
68
+ copyRectProperties(this, Rect.getBoundingRect(rangeRects));
69
+ }
70
+ else {
71
+ copyRectProperties(this, source.getBoundingClientRect());
72
+ }
73
+ }
74
+ else if (isWindow(source)) {
75
+ const { innerWidth, innerHeight } = source;
76
+ copyRectProperties(this, {
77
+ top: 0,
78
+ right: innerWidth,
79
+ bottom: innerHeight,
80
+ left: 0,
81
+ width: innerWidth,
82
+ height: innerHeight
83
+ });
84
+ }
85
+ else {
86
+ copyRectProperties(this, source);
87
+ }
88
+ }
89
+ /**
90
+ * Returns a clone of the rect.
91
+ *
92
+ * @returns {module:utils/dom/rect~Rect} A cloned rect.
93
+ */
94
+ clone() {
95
+ return new Rect(this);
96
+ }
97
+ /**
98
+ * Moves the rect so that its upper–left corner lands in desired `[ x, y ]` location.
99
+ *
100
+ * @param {Number} x Desired horizontal location.
101
+ * @param {Number} y Desired vertical location.
102
+ * @returns {module:utils/dom/rect~Rect} A rect which has been moved.
103
+ */
104
+ moveTo(x, y) {
105
+ this.top = y;
106
+ this.right = x + this.width;
107
+ this.bottom = y + this.height;
108
+ this.left = x;
109
+ return this;
110
+ }
111
+ /**
112
+ * Moves the rect in–place by a dedicated offset.
113
+ *
114
+ * @param {Number} x A horizontal offset.
115
+ * @param {Number} y A vertical offset
116
+ * @returns {module:utils/dom/rect~Rect} A rect which has been moved.
117
+ */
118
+ moveBy(x, y) {
119
+ this.top += y;
120
+ this.right += x;
121
+ this.left += x;
122
+ this.bottom += y;
123
+ return this;
124
+ }
125
+ /**
126
+ * Returns a new rect a a result of intersection with another rect.
127
+ *
128
+ * @param {module:utils/dom/rect~Rect} anotherRect
129
+ * @returns {module:utils/dom/rect~Rect|null}
130
+ */
131
+ getIntersection(anotherRect) {
132
+ const rect = {
133
+ top: Math.max(this.top, anotherRect.top),
134
+ right: Math.min(this.right, anotherRect.right),
135
+ bottom: Math.min(this.bottom, anotherRect.bottom),
136
+ left: Math.max(this.left, anotherRect.left),
137
+ width: 0,
138
+ height: 0
139
+ };
140
+ rect.width = rect.right - rect.left;
141
+ rect.height = rect.bottom - rect.top;
142
+ if (rect.width < 0 || rect.height < 0) {
143
+ return null;
144
+ }
145
+ else {
146
+ return new Rect(rect);
147
+ }
148
+ }
149
+ /**
150
+ * Returns the area of intersection with another rect.
151
+ *
152
+ * @param {module:utils/dom/rect~Rect} anotherRect
153
+ * @returns {Number} Area of intersection.
154
+ */
155
+ getIntersectionArea(anotherRect) {
156
+ const rect = this.getIntersection(anotherRect);
157
+ if (rect) {
158
+ return rect.getArea();
159
+ }
160
+ else {
161
+ return 0;
162
+ }
163
+ }
164
+ /**
165
+ * Returns the area of the rect.
166
+ *
167
+ * @returns {Number}
168
+ */
169
+ getArea() {
170
+ return this.width * this.height;
171
+ }
172
+ /**
173
+ * Returns a new rect, a part of the original rect, which is actually visible to the user,
174
+ * e.g. an original rect cropped by parent element rects which have `overflow` set in CSS
175
+ * other than `"visible"`.
176
+ *
177
+ * If there's no such visible rect, which is when the rect is limited by one or many of
178
+ * the ancestors, `null` is returned.
179
+ *
180
+ * @returns {module:utils/dom/rect~Rect|null} A visible rect instance or `null`, if there's none.
181
+ */
182
+ getVisible() {
183
+ const source = this._source;
184
+ let visibleRect = this.clone();
185
+ // There's no ancestor to crop <body> with the overflow.
186
+ if (!isBody(source)) {
187
+ let parent = source.parentNode || source.commonAncestorContainer;
188
+ // Check the ancestors all the way up to the <body>.
189
+ while (parent && !isBody(parent)) {
190
+ const parentRect = new Rect(parent);
191
+ const intersectionRect = visibleRect.getIntersection(parentRect);
192
+ if (intersectionRect) {
193
+ if (intersectionRect.getArea() < visibleRect.getArea()) {
194
+ // Reduce the visible rect to the intersection.
195
+ visibleRect = intersectionRect;
196
+ }
197
+ }
198
+ else {
199
+ // There's no intersection, the rect is completely invisible.
200
+ return null;
201
+ }
202
+ parent = parent.parentNode;
203
+ }
204
+ }
205
+ return visibleRect;
206
+ }
207
+ /**
208
+ * Checks if all property values ({@link #top}, {@link #left}, {@link #right},
209
+ * {@link #bottom}, {@link #width} and {@link #height}) are the equal in both rect
210
+ * instances.
211
+ *
212
+ * @param {module:utils/dom/rect~Rect} anotherRect A rect instance to compare with.
213
+ * @returns {Boolean} `true` when Rects are equal. `false` otherwise.
214
+ */
215
+ isEqual(anotherRect) {
216
+ for (const prop of rectProperties) {
217
+ if (this[prop] !== anotherRect[prop]) {
218
+ return false;
219
+ }
220
+ }
221
+ return true;
222
+ }
223
+ /**
224
+ * Checks whether a rect fully contains another rect instance.
225
+ *
226
+ * @param {module:utils/dom/rect~Rect} anotherRect
227
+ * @returns {Boolean} `true` if contains, `false` otherwise.
228
+ */
229
+ contains(anotherRect) {
230
+ const intersectRect = this.getIntersection(anotherRect);
231
+ return !!(intersectRect && intersectRect.isEqual(anotherRect));
232
+ }
233
+ /**
234
+ * Excludes scrollbars and CSS borders from the rect.
235
+ *
236
+ * * Borders are removed when {@link #_source} is an HTML element.
237
+ * * Scrollbars are excluded from HTML elements and the `window`.
238
+ *
239
+ * @returns {module:utils/dom/rect~Rect} A rect which has been updated.
240
+ */
241
+ excludeScrollbarsAndBorders() {
242
+ const source = this._source;
243
+ let scrollBarWidth, scrollBarHeight, direction;
244
+ if (isWindow(source)) {
245
+ scrollBarWidth = source.innerWidth - source.document.documentElement.clientWidth;
246
+ scrollBarHeight = source.innerHeight - source.document.documentElement.clientHeight;
247
+ direction = source.getComputedStyle(source.document.documentElement).direction;
248
+ }
249
+ else {
250
+ const borderWidths = getBorderWidths(source);
251
+ scrollBarWidth = source.offsetWidth - source.clientWidth - borderWidths.left - borderWidths.right;
252
+ scrollBarHeight = source.offsetHeight - source.clientHeight - borderWidths.top - borderWidths.bottom;
253
+ direction = source.ownerDocument.defaultView.getComputedStyle(source).direction;
254
+ this.left += borderWidths.left;
255
+ this.top += borderWidths.top;
256
+ this.right -= borderWidths.right;
257
+ this.bottom -= borderWidths.bottom;
258
+ this.width = this.right - this.left;
259
+ this.height = this.bottom - this.top;
260
+ }
261
+ this.width -= scrollBarWidth;
262
+ if (direction === 'ltr') {
263
+ this.right -= scrollBarWidth;
264
+ }
265
+ else {
266
+ this.left += scrollBarWidth;
267
+ }
268
+ this.height -= scrollBarHeight;
269
+ this.bottom -= scrollBarHeight;
270
+ return this;
271
+ }
272
+ /**
273
+ * Returns an array of rects of the given native DOM Range.
274
+ *
275
+ * @param {Range} range A native DOM range.
276
+ * @returns {Array.<module:utils/dom/rect~Rect>} DOM Range rects.
277
+ */
278
+ static getDomRangeRects(range) {
279
+ const rects = [];
280
+ // Safari does not iterate over ClientRectList using for...of loop.
281
+ const clientRects = Array.from(range.getClientRects());
282
+ if (clientRects.length) {
283
+ for (const rect of clientRects) {
284
+ rects.push(new Rect(rect));
285
+ }
286
+ }
287
+ // If there's no client rects for the Range, use parent container's bounding rect
288
+ // instead and adjust rect's width to simulate the actual geometry of such range.
289
+ // https://github.com/ckeditor/ckeditor5-utils/issues/153
290
+ // https://github.com/ckeditor/ckeditor5-ui/issues/317
291
+ else {
292
+ let startContainer = range.startContainer;
293
+ if (isText(startContainer)) {
294
+ startContainer = startContainer.parentNode;
295
+ }
296
+ const rect = new Rect(startContainer.getBoundingClientRect());
297
+ rect.right = rect.left;
298
+ rect.width = 0;
299
+ rects.push(rect);
300
+ }
301
+ return rects;
302
+ }
303
+ /**
304
+ * Returns a bounding rectangle that contains all the given `rects`.
305
+ *
306
+ * @param {Iterable.<module:utils/dom/rect~Rect>} rects A list of rectangles that should be contained in the result rectangle.
307
+ * @returns {module:utils/dom/rect~Rect|null} Bounding rectangle or `null` if no `rects` were given.
308
+ */
309
+ static getBoundingRect(rects) {
310
+ const boundingRectData = {
311
+ left: Number.POSITIVE_INFINITY,
312
+ top: Number.POSITIVE_INFINITY,
313
+ right: Number.NEGATIVE_INFINITY,
314
+ bottom: Number.NEGATIVE_INFINITY,
315
+ width: 0,
316
+ height: 0
317
+ };
318
+ let rectangleCount = 0;
319
+ for (const rect of rects) {
320
+ rectangleCount++;
321
+ boundingRectData.left = Math.min(boundingRectData.left, rect.left);
322
+ boundingRectData.top = Math.min(boundingRectData.top, rect.top);
323
+ boundingRectData.right = Math.max(boundingRectData.right, rect.right);
324
+ boundingRectData.bottom = Math.max(boundingRectData.bottom, rect.bottom);
325
+ }
326
+ if (rectangleCount == 0) {
327
+ return null;
328
+ }
329
+ boundingRectData.width = boundingRectData.right - boundingRectData.left;
330
+ boundingRectData.height = boundingRectData.bottom - boundingRectData.top;
331
+ return new Rect(boundingRectData);
332
+ }
333
+ }
334
+ // Acquires all the rect properties from the passed source.
335
+ //
336
+ // @private
337
+ // @param {module:utils/dom/rect~Rect} rect
338
+ // @param {module:utils/dom/rect~RectLike} source
339
+ function copyRectProperties(rect, source) {
340
+ for (const p of rectProperties) {
341
+ rect[p] = source[p];
342
+ }
343
+ }
344
+ // Checks if provided object is a <body> HTML element.
345
+ //
346
+ // @private
347
+ // @param {*} value
348
+ // @returns {Boolean}
349
+ function isBody(value) {
350
+ if (!isDomElement(value)) {
351
+ return false;
352
+ }
353
+ return value === value.ownerDocument.body;
354
+ }
355
+ // Checks if provided object is a DOM Element.
356
+ //
357
+ // It's just a wrapper for lodash `isElement` but with type guard.
358
+ //
359
+ // @private
360
+ // @param {*} value
361
+ // @returns {Boolean}
362
+ function isDomElement(value) {
363
+ return isElement(value);
364
+ }