@ckeditor/ckeditor5-utils 38.1.0 → 38.1.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.
- package/package.json +1 -1
- package/src/areconnectedthroughproperties.d.ts +11 -11
- package/src/areconnectedthroughproperties.js +73 -73
- package/src/ckeditorerror.d.ts +123 -123
- package/src/ckeditorerror.js +176 -176
- package/src/collection.d.ts +427 -427
- package/src/collection.js +575 -575
- package/src/comparearrays.d.ts +30 -30
- package/src/comparearrays.js +47 -47
- package/src/config.d.ts +163 -163
- package/src/config.js +162 -162
- package/src/count.d.ts +18 -18
- package/src/count.js +24 -24
- package/src/delay.d.ts +19 -19
- package/src/delay.js +26 -26
- package/src/diff.d.ts +31 -31
- package/src/diff.js +115 -115
- package/src/difftochanges.d.ts +59 -59
- package/src/difftochanges.js +79 -79
- package/src/dom/createelement.d.ts +57 -57
- package/src/dom/createelement.js +40 -40
- package/src/dom/emittermixin.d.ts +142 -142
- package/src/dom/emittermixin.js +239 -239
- package/src/dom/findclosestscrollableancestor.d.ts +11 -11
- package/src/dom/findclosestscrollableancestor.js +31 -31
- package/src/dom/getancestors.d.ts +17 -17
- package/src/dom/getancestors.js +27 -27
- package/src/dom/getborderwidths.d.ts +24 -24
- package/src/dom/getborderwidths.js +24 -24
- package/src/dom/getcommonancestor.d.ts +12 -12
- package/src/dom/getcommonancestor.js +25 -25
- package/src/dom/getdatafromelement.d.ts +14 -14
- package/src/dom/getdatafromelement.js +20 -20
- package/src/dom/getpositionedancestor.d.ts +10 -10
- package/src/dom/getpositionedancestor.js +22 -22
- package/src/dom/global.d.ts +32 -32
- package/src/dom/global.js +35 -35
- package/src/dom/indexof.d.ts +14 -14
- package/src/dom/indexof.js +21 -21
- package/src/dom/insertat.d.ts +15 -15
- package/src/dom/insertat.js +17 -17
- package/src/dom/iscomment.d.ts +11 -11
- package/src/dom/iscomment.js +14 -14
- package/src/dom/isnode.d.ts +11 -11
- package/src/dom/isnode.js +21 -21
- package/src/dom/isrange.d.ts +11 -11
- package/src/dom/isrange.js +13 -13
- package/src/dom/istext.d.ts +11 -11
- package/src/dom/istext.js +13 -13
- package/src/dom/isvalidattributename.d.ts +10 -10
- package/src/dom/isvalidattributename.js +22 -22
- package/src/dom/isvisible.d.ts +18 -18
- package/src/dom/isvisible.js +20 -20
- package/src/dom/iswindow.d.ts +11 -11
- package/src/dom/iswindow.js +22 -22
- package/src/dom/position.d.ts +208 -208
- package/src/dom/position.js +305 -305
- package/src/dom/rect.d.ts +191 -191
- package/src/dom/rect.js +388 -388
- package/src/dom/remove.d.ts +13 -13
- package/src/dom/remove.js +18 -18
- package/src/dom/resizeobserver.d.ts +74 -74
- package/src/dom/resizeobserver.js +126 -126
- package/src/dom/scroll.d.ts +71 -71
- package/src/dom/scroll.js +378 -378
- package/src/dom/setdatainelement.d.ts +14 -14
- package/src/dom/setdatainelement.js +20 -20
- package/src/dom/tounit.d.ts +22 -22
- package/src/dom/tounit.js +16 -16
- package/src/elementreplacer.d.ts +31 -31
- package/src/elementreplacer.js +43 -43
- package/src/emittermixin.d.ts +312 -312
- package/src/emittermixin.js +453 -453
- package/src/env.d.ts +117 -117
- package/src/env.js +122 -122
- package/src/eventinfo.d.ts +58 -58
- package/src/eventinfo.js +26 -26
- package/src/fastdiff.d.ts +112 -112
- package/src/fastdiff.js +248 -248
- package/src/first.d.ts +11 -11
- package/src/first.js +17 -17
- package/src/focustracker.d.ts +73 -73
- package/src/focustracker.js +93 -93
- package/src/index.d.ts +60 -60
- package/src/index.js +59 -59
- package/src/inserttopriorityarray.d.ts +30 -30
- package/src/inserttopriorityarray.js +21 -21
- package/src/isiterable.d.ts +14 -14
- package/src/isiterable.js +16 -16
- package/src/keyboard.d.ts +126 -126
- package/src/keyboard.js +221 -221
- package/src/keystrokehandler.d.ts +87 -87
- package/src/keystrokehandler.js +122 -122
- package/src/language.d.ts +17 -17
- package/src/language.js +19 -19
- package/src/locale.d.ts +120 -120
- package/src/locale.js +76 -76
- package/src/mapsequal.d.ts +15 -15
- package/src/mapsequal.js +27 -27
- package/src/mix.d.ts +85 -85
- package/src/mix.js +50 -50
- package/src/nth.d.ts +16 -16
- package/src/nth.js +24 -24
- package/src/objecttomap.d.ts +23 -23
- package/src/objecttomap.js +27 -27
- package/src/observablemixin.d.ts +558 -558
- package/src/observablemixin.js +580 -580
- package/src/priorities.d.ts +33 -33
- package/src/priorities.js +23 -23
- package/src/splicearray.d.ts +26 -26
- package/src/splicearray.js +40 -40
- package/src/spy.d.ts +21 -21
- package/src/spy.js +22 -22
- package/src/toarray.d.ts +25 -25
- package/src/toarray.js +7 -7
- package/src/tomap.d.ts +19 -19
- package/src/tomap.js +29 -29
- package/src/translation-service.d.ts +168 -168
- package/src/translation-service.js +198 -198
- package/src/uid.d.ts +15 -15
- package/src/uid.js +57 -57
- package/src/unicode.d.ts +54 -54
- package/src/unicode.js +85 -85
- package/src/verifylicense.d.ts +15 -15
- package/src/verifylicense.js +87 -87
- package/src/version.d.ts +10 -10
- package/src/version.js +156 -156
package/src/dom/rect.js
CHANGED
|
@@ -1,388 +1,388 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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
|
-
const rectProperties = ['top', 'right', 'bottom', 'left', 'width', 'height'];
|
|
13
|
-
/**
|
|
14
|
-
* A helper class representing a `ClientRect` object, e.g. value returned by
|
|
15
|
-
* the native `object.getBoundingClientRect()` method. Provides a set of methods
|
|
16
|
-
* to manipulate the rect and compare it against other rect instances.
|
|
17
|
-
*/
|
|
18
|
-
export default class Rect {
|
|
19
|
-
/**
|
|
20
|
-
* Creates an instance of rect.
|
|
21
|
-
*
|
|
22
|
-
* ```ts
|
|
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
|
-
*
|
|
42
|
-
* **Note**: By default a rect of an HTML element includes its CSS borders and scrollbars (if any)
|
|
43
|
-
* ant the rect of a `window` includes scrollbars too. Use {@link #excludeScrollbarsAndBorders}
|
|
44
|
-
* to get the inner part of the rect.
|
|
45
|
-
*
|
|
46
|
-
* @param source A source object to create the rect.
|
|
47
|
-
*/
|
|
48
|
-
constructor(source) {
|
|
49
|
-
const isSourceRange = isRange(source);
|
|
50
|
-
Object.defineProperty(this, '_source', {
|
|
51
|
-
// If the source is a Rect instance, copy it's #_source.
|
|
52
|
-
value: source._source || source,
|
|
53
|
-
writable: true,
|
|
54
|
-
enumerable: false
|
|
55
|
-
});
|
|
56
|
-
if (isDomElement(source) || isSourceRange) {
|
|
57
|
-
// The `Rect` class depends on `getBoundingClientRect` and `getClientRects` DOM methods. If the source
|
|
58
|
-
// of a rect in an HTML element or a DOM range but it does not belong to any rendered DOM tree, these methods
|
|
59
|
-
// will fail to obtain the geometry and the rect instance makes little sense to the features using it.
|
|
60
|
-
// To get rid of this warning make sure the source passed to the constructor is a descendant of `window.document.body`.
|
|
61
|
-
// @if CK_DEBUG // const sourceNode = isSourceRange ? source.startContainer : source;
|
|
62
|
-
// @if CK_DEBUG // if ( !sourceNode.ownerDocument || !sourceNode.ownerDocument.body.contains( sourceNode ) ) {
|
|
63
|
-
// @if CK_DEBUG // console.warn(
|
|
64
|
-
// @if CK_DEBUG // 'rect-source-not-in-dom: The source of this rect does not belong to any rendered DOM tree.',
|
|
65
|
-
// @if CK_DEBUG // { source } );
|
|
66
|
-
// @if CK_DEBUG // }
|
|
67
|
-
if (isSourceRange) {
|
|
68
|
-
const rangeRects = Rect.getDomRangeRects(source);
|
|
69
|
-
copyRectProperties(this, Rect.getBoundingRect(rangeRects));
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
copyRectProperties(this, source.getBoundingClientRect());
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
else if (isWindow(source)) {
|
|
76
|
-
const { innerWidth, innerHeight } = source;
|
|
77
|
-
copyRectProperties(this, {
|
|
78
|
-
top: 0,
|
|
79
|
-
right: innerWidth,
|
|
80
|
-
bottom: innerHeight,
|
|
81
|
-
left: 0,
|
|
82
|
-
width: innerWidth,
|
|
83
|
-
height: innerHeight
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
copyRectProperties(this, source);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Returns a clone of the rect.
|
|
92
|
-
*
|
|
93
|
-
* @returns A cloned rect.
|
|
94
|
-
*/
|
|
95
|
-
clone() {
|
|
96
|
-
return new Rect(this);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Moves the rect so that its upper–left corner lands in desired `[ x, y ]` location.
|
|
100
|
-
*
|
|
101
|
-
* @param x Desired horizontal location.
|
|
102
|
-
* @param y Desired vertical location.
|
|
103
|
-
* @returns A rect which has been moved.
|
|
104
|
-
*/
|
|
105
|
-
moveTo(x, y) {
|
|
106
|
-
this.top = y;
|
|
107
|
-
this.right = x + this.width;
|
|
108
|
-
this.bottom = y + this.height;
|
|
109
|
-
this.left = x;
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Moves the rect in–place by a dedicated offset.
|
|
114
|
-
*
|
|
115
|
-
* @param x A horizontal offset.
|
|
116
|
-
* @param y A vertical offset
|
|
117
|
-
* @returns A rect which has been moved.
|
|
118
|
-
*/
|
|
119
|
-
moveBy(x, y) {
|
|
120
|
-
this.top += y;
|
|
121
|
-
this.right += x;
|
|
122
|
-
this.left += x;
|
|
123
|
-
this.bottom += y;
|
|
124
|
-
return this;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Returns a new rect a a result of intersection with another rect.
|
|
128
|
-
*/
|
|
129
|
-
getIntersection(anotherRect) {
|
|
130
|
-
const rect = {
|
|
131
|
-
top: Math.max(this.top, anotherRect.top),
|
|
132
|
-
right: Math.min(this.right, anotherRect.right),
|
|
133
|
-
bottom: Math.min(this.bottom, anotherRect.bottom),
|
|
134
|
-
left: Math.max(this.left, anotherRect.left),
|
|
135
|
-
width: 0,
|
|
136
|
-
height: 0
|
|
137
|
-
};
|
|
138
|
-
rect.width = rect.right - rect.left;
|
|
139
|
-
rect.height = rect.bottom - rect.top;
|
|
140
|
-
if (rect.width < 0 || rect.height < 0) {
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
return new Rect(rect);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Returns the area of intersection with another rect.
|
|
149
|
-
*
|
|
150
|
-
* @returns Area of intersection.
|
|
151
|
-
*/
|
|
152
|
-
getIntersectionArea(anotherRect) {
|
|
153
|
-
const rect = this.getIntersection(anotherRect);
|
|
154
|
-
if (rect) {
|
|
155
|
-
return rect.getArea();
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
return 0;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Returns the area of the rect.
|
|
163
|
-
*/
|
|
164
|
-
getArea() {
|
|
165
|
-
return this.width * this.height;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Returns a new rect, a part of the original rect, which is actually visible to the user,
|
|
169
|
-
* e.g. an original rect cropped by parent element rects which have `overflow` set in CSS
|
|
170
|
-
* other than `"visible"`.
|
|
171
|
-
*
|
|
172
|
-
* If there's no such visible rect, which is when the rect is limited by one or many of
|
|
173
|
-
* the ancestors, `null` is returned.
|
|
174
|
-
*
|
|
175
|
-
* **Note**: This method does not consider the boundaries of the viewport (window).
|
|
176
|
-
* To get a rect cropped by all ancestors and the viewport, use an intersection such as:
|
|
177
|
-
*
|
|
178
|
-
* ```ts
|
|
179
|
-
* const visibleInViewportRect = new Rect( window ).getIntersection( new Rect( source ).getVisible() );
|
|
180
|
-
* ```
|
|
181
|
-
*
|
|
182
|
-
* @returns A visible rect instance or `null`, if there's none.
|
|
183
|
-
*/
|
|
184
|
-
getVisible() {
|
|
185
|
-
const source = this._source;
|
|
186
|
-
let visibleRect = this.clone();
|
|
187
|
-
// There's no ancestor to crop <body> with the overflow.
|
|
188
|
-
if (isBody(source)) {
|
|
189
|
-
return visibleRect;
|
|
190
|
-
}
|
|
191
|
-
let child = source;
|
|
192
|
-
let parent = source.parentNode || source.commonAncestorContainer;
|
|
193
|
-
let absolutelyPositionedChildElement;
|
|
194
|
-
// Check the ancestors all the way up to the <body>.
|
|
195
|
-
while (parent && !isBody(parent)) {
|
|
196
|
-
if (child instanceof HTMLElement && getElementPosition(child) === 'absolute') {
|
|
197
|
-
absolutelyPositionedChildElement = child;
|
|
198
|
-
}
|
|
199
|
-
// The child will be cropped only if it has `position: absolute` and the parent has `position: relative` + some overflow.
|
|
200
|
-
// Otherwise there's no chance of visual clipping and the parent can be skipped
|
|
201
|
-
// https://github.com/ckeditor/ckeditor5/issues/14107.
|
|
202
|
-
if (absolutelyPositionedChildElement &&
|
|
203
|
-
(getElementPosition(parent) !== 'relative' || getElementOverflow(parent) === 'visible')) {
|
|
204
|
-
child = parent;
|
|
205
|
-
parent = parent.parentNode;
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
const parentRect = new Rect(parent);
|
|
209
|
-
const intersectionRect = visibleRect.getIntersection(parentRect);
|
|
210
|
-
if (intersectionRect) {
|
|
211
|
-
if (intersectionRect.getArea() < visibleRect.getArea()) {
|
|
212
|
-
// Reduce the visible rect to the intersection.
|
|
213
|
-
visibleRect = intersectionRect;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
// There's no intersection, the rect is completely invisible.
|
|
218
|
-
return null;
|
|
219
|
-
}
|
|
220
|
-
child = parent;
|
|
221
|
-
parent = parent.parentNode;
|
|
222
|
-
}
|
|
223
|
-
return visibleRect;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Checks if all property values ({@link #top}, {@link #left}, {@link #right},
|
|
227
|
-
* {@link #bottom}, {@link #width} and {@link #height}) are the equal in both rect
|
|
228
|
-
* instances.
|
|
229
|
-
*
|
|
230
|
-
* @param anotherRect A rect instance to compare with.
|
|
231
|
-
* @returns `true` when Rects are equal. `false` otherwise.
|
|
232
|
-
*/
|
|
233
|
-
isEqual(anotherRect) {
|
|
234
|
-
for (const prop of rectProperties) {
|
|
235
|
-
if (this[prop] !== anotherRect[prop]) {
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return true;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Checks whether a rect fully contains another rect instance.
|
|
243
|
-
*
|
|
244
|
-
* @param anotherRect
|
|
245
|
-
* @returns `true` if contains, `false` otherwise.
|
|
246
|
-
*/
|
|
247
|
-
contains(anotherRect) {
|
|
248
|
-
const intersectRect = this.getIntersection(anotherRect);
|
|
249
|
-
return !!(intersectRect && intersectRect.isEqual(anotherRect));
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Excludes scrollbars and CSS borders from the rect.
|
|
253
|
-
*
|
|
254
|
-
* * Borders are removed when {@link #_source} is an HTML element.
|
|
255
|
-
* * Scrollbars are excluded from HTML elements and the `window`.
|
|
256
|
-
*
|
|
257
|
-
* @returns A rect which has been updated.
|
|
258
|
-
*/
|
|
259
|
-
excludeScrollbarsAndBorders() {
|
|
260
|
-
const source = this._source;
|
|
261
|
-
let scrollBarWidth, scrollBarHeight, direction;
|
|
262
|
-
if (isWindow(source)) {
|
|
263
|
-
scrollBarWidth = source.innerWidth - source.document.documentElement.clientWidth;
|
|
264
|
-
scrollBarHeight = source.innerHeight - source.document.documentElement.clientHeight;
|
|
265
|
-
direction = source.getComputedStyle(source.document.documentElement).direction;
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
const borderWidths = getBorderWidths(source);
|
|
269
|
-
scrollBarWidth = source.offsetWidth - source.clientWidth - borderWidths.left - borderWidths.right;
|
|
270
|
-
scrollBarHeight = source.offsetHeight - source.clientHeight - borderWidths.top - borderWidths.bottom;
|
|
271
|
-
direction = source.ownerDocument.defaultView.getComputedStyle(source).direction;
|
|
272
|
-
this.left += borderWidths.left;
|
|
273
|
-
this.top += borderWidths.top;
|
|
274
|
-
this.right -= borderWidths.right;
|
|
275
|
-
this.bottom -= borderWidths.bottom;
|
|
276
|
-
this.width = this.right - this.left;
|
|
277
|
-
this.height = this.bottom - this.top;
|
|
278
|
-
}
|
|
279
|
-
this.width -= scrollBarWidth;
|
|
280
|
-
if (direction === 'ltr') {
|
|
281
|
-
this.right -= scrollBarWidth;
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
this.left += scrollBarWidth;
|
|
285
|
-
}
|
|
286
|
-
this.height -= scrollBarHeight;
|
|
287
|
-
this.bottom -= scrollBarHeight;
|
|
288
|
-
return this;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Returns an array of rects of the given native DOM Range.
|
|
292
|
-
*
|
|
293
|
-
* @param range A native DOM range.
|
|
294
|
-
* @returns DOM Range rects.
|
|
295
|
-
*/
|
|
296
|
-
static getDomRangeRects(range) {
|
|
297
|
-
const rects = [];
|
|
298
|
-
// Safari does not iterate over ClientRectList using for...of loop.
|
|
299
|
-
const clientRects = Array.from(range.getClientRects());
|
|
300
|
-
if (clientRects.length) {
|
|
301
|
-
for (const rect of clientRects) {
|
|
302
|
-
rects.push(new Rect(rect));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
// If there's no client rects for the Range, use parent container's bounding rect
|
|
306
|
-
// instead and adjust rect's width to simulate the actual geometry of such range.
|
|
307
|
-
// https://github.com/ckeditor/ckeditor5-utils/issues/153
|
|
308
|
-
// https://github.com/ckeditor/ckeditor5-ui/issues/317
|
|
309
|
-
else {
|
|
310
|
-
let startContainer = range.startContainer;
|
|
311
|
-
if (isText(startContainer)) {
|
|
312
|
-
startContainer = startContainer.parentNode;
|
|
313
|
-
}
|
|
314
|
-
const rect = new Rect(startContainer.getBoundingClientRect());
|
|
315
|
-
rect.right = rect.left;
|
|
316
|
-
rect.width = 0;
|
|
317
|
-
rects.push(rect);
|
|
318
|
-
}
|
|
319
|
-
return rects;
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Returns a bounding rectangle that contains all the given `rects`.
|
|
323
|
-
*
|
|
324
|
-
* @param rects A list of rectangles that should be contained in the result rectangle.
|
|
325
|
-
* @returns Bounding rectangle or `null` if no `rects` were given.
|
|
326
|
-
*/
|
|
327
|
-
static getBoundingRect(rects) {
|
|
328
|
-
const boundingRectData = {
|
|
329
|
-
left: Number.POSITIVE_INFINITY,
|
|
330
|
-
top: Number.POSITIVE_INFINITY,
|
|
331
|
-
right: Number.NEGATIVE_INFINITY,
|
|
332
|
-
bottom: Number.NEGATIVE_INFINITY,
|
|
333
|
-
width: 0,
|
|
334
|
-
height: 0
|
|
335
|
-
};
|
|
336
|
-
let rectangleCount = 0;
|
|
337
|
-
for (const rect of rects) {
|
|
338
|
-
rectangleCount++;
|
|
339
|
-
boundingRectData.left = Math.min(boundingRectData.left, rect.left);
|
|
340
|
-
boundingRectData.top = Math.min(boundingRectData.top, rect.top);
|
|
341
|
-
boundingRectData.right = Math.max(boundingRectData.right, rect.right);
|
|
342
|
-
boundingRectData.bottom = Math.max(boundingRectData.bottom, rect.bottom);
|
|
343
|
-
}
|
|
344
|
-
if (rectangleCount == 0) {
|
|
345
|
-
return null;
|
|
346
|
-
}
|
|
347
|
-
boundingRectData.width = boundingRectData.right - boundingRectData.left;
|
|
348
|
-
boundingRectData.height = boundingRectData.bottom - boundingRectData.top;
|
|
349
|
-
return new Rect(boundingRectData);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Acquires all the rect properties from the passed source.
|
|
354
|
-
*/
|
|
355
|
-
function copyRectProperties(rect, source) {
|
|
356
|
-
for (const p of rectProperties) {
|
|
357
|
-
rect[p] = source[p];
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Checks if provided object is a <body> HTML element.
|
|
362
|
-
*/
|
|
363
|
-
function isBody(value) {
|
|
364
|
-
if (!isDomElement(value)) {
|
|
365
|
-
return false;
|
|
366
|
-
}
|
|
367
|
-
return value === value.ownerDocument.body;
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Checks if provided object "looks like" a DOM Element and has API required by `Rect` class.
|
|
371
|
-
*/
|
|
372
|
-
function isDomElement(value) {
|
|
373
|
-
// Note: earlier we used `isElement()` from lodash library, however that function is less performant because
|
|
374
|
-
// it makes complicated checks to make sure that given value is a DOM element.
|
|
375
|
-
return value !== null && typeof value === 'object' && value.nodeType === 1 && typeof value.getBoundingClientRect === 'function';
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* Returns the value of the `position` style of an `HTMLElement`.
|
|
379
|
-
*/
|
|
380
|
-
function getElementPosition(element) {
|
|
381
|
-
return element.ownerDocument.defaultView.getComputedStyle(element).position;
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Returns the value of the `overflow` style of an `HTMLElement`.
|
|
385
|
-
*/
|
|
386
|
-
function getElementOverflow(element) {
|
|
387
|
-
return element.ownerDocument.defaultView.getComputedStyle(element).overflow;
|
|
388
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
const rectProperties = ['top', 'right', 'bottom', 'left', 'width', 'height'];
|
|
13
|
+
/**
|
|
14
|
+
* A helper class representing a `ClientRect` object, e.g. value returned by
|
|
15
|
+
* the native `object.getBoundingClientRect()` method. Provides a set of methods
|
|
16
|
+
* to manipulate the rect and compare it against other rect instances.
|
|
17
|
+
*/
|
|
18
|
+
export default class Rect {
|
|
19
|
+
/**
|
|
20
|
+
* Creates an instance of rect.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
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
|
+
*
|
|
42
|
+
* **Note**: By default a rect of an HTML element includes its CSS borders and scrollbars (if any)
|
|
43
|
+
* ant the rect of a `window` includes scrollbars too. Use {@link #excludeScrollbarsAndBorders}
|
|
44
|
+
* to get the inner part of the rect.
|
|
45
|
+
*
|
|
46
|
+
* @param source A source object to create the rect.
|
|
47
|
+
*/
|
|
48
|
+
constructor(source) {
|
|
49
|
+
const isSourceRange = isRange(source);
|
|
50
|
+
Object.defineProperty(this, '_source', {
|
|
51
|
+
// If the source is a Rect instance, copy it's #_source.
|
|
52
|
+
value: source._source || source,
|
|
53
|
+
writable: true,
|
|
54
|
+
enumerable: false
|
|
55
|
+
});
|
|
56
|
+
if (isDomElement(source) || isSourceRange) {
|
|
57
|
+
// The `Rect` class depends on `getBoundingClientRect` and `getClientRects` DOM methods. If the source
|
|
58
|
+
// of a rect in an HTML element or a DOM range but it does not belong to any rendered DOM tree, these methods
|
|
59
|
+
// will fail to obtain the geometry and the rect instance makes little sense to the features using it.
|
|
60
|
+
// To get rid of this warning make sure the source passed to the constructor is a descendant of `window.document.body`.
|
|
61
|
+
// @if CK_DEBUG // const sourceNode = isSourceRange ? source.startContainer : source;
|
|
62
|
+
// @if CK_DEBUG // if ( !sourceNode.ownerDocument || !sourceNode.ownerDocument.body.contains( sourceNode ) ) {
|
|
63
|
+
// @if CK_DEBUG // console.warn(
|
|
64
|
+
// @if CK_DEBUG // 'rect-source-not-in-dom: The source of this rect does not belong to any rendered DOM tree.',
|
|
65
|
+
// @if CK_DEBUG // { source } );
|
|
66
|
+
// @if CK_DEBUG // }
|
|
67
|
+
if (isSourceRange) {
|
|
68
|
+
const rangeRects = Rect.getDomRangeRects(source);
|
|
69
|
+
copyRectProperties(this, Rect.getBoundingRect(rangeRects));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
copyRectProperties(this, source.getBoundingClientRect());
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (isWindow(source)) {
|
|
76
|
+
const { innerWidth, innerHeight } = source;
|
|
77
|
+
copyRectProperties(this, {
|
|
78
|
+
top: 0,
|
|
79
|
+
right: innerWidth,
|
|
80
|
+
bottom: innerHeight,
|
|
81
|
+
left: 0,
|
|
82
|
+
width: innerWidth,
|
|
83
|
+
height: innerHeight
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
copyRectProperties(this, source);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns a clone of the rect.
|
|
92
|
+
*
|
|
93
|
+
* @returns A cloned rect.
|
|
94
|
+
*/
|
|
95
|
+
clone() {
|
|
96
|
+
return new Rect(this);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Moves the rect so that its upper–left corner lands in desired `[ x, y ]` location.
|
|
100
|
+
*
|
|
101
|
+
* @param x Desired horizontal location.
|
|
102
|
+
* @param y Desired vertical location.
|
|
103
|
+
* @returns A rect which has been moved.
|
|
104
|
+
*/
|
|
105
|
+
moveTo(x, y) {
|
|
106
|
+
this.top = y;
|
|
107
|
+
this.right = x + this.width;
|
|
108
|
+
this.bottom = y + this.height;
|
|
109
|
+
this.left = x;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Moves the rect in–place by a dedicated offset.
|
|
114
|
+
*
|
|
115
|
+
* @param x A horizontal offset.
|
|
116
|
+
* @param y A vertical offset
|
|
117
|
+
* @returns A rect which has been moved.
|
|
118
|
+
*/
|
|
119
|
+
moveBy(x, y) {
|
|
120
|
+
this.top += y;
|
|
121
|
+
this.right += x;
|
|
122
|
+
this.left += x;
|
|
123
|
+
this.bottom += y;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns a new rect a a result of intersection with another rect.
|
|
128
|
+
*/
|
|
129
|
+
getIntersection(anotherRect) {
|
|
130
|
+
const rect = {
|
|
131
|
+
top: Math.max(this.top, anotherRect.top),
|
|
132
|
+
right: Math.min(this.right, anotherRect.right),
|
|
133
|
+
bottom: Math.min(this.bottom, anotherRect.bottom),
|
|
134
|
+
left: Math.max(this.left, anotherRect.left),
|
|
135
|
+
width: 0,
|
|
136
|
+
height: 0
|
|
137
|
+
};
|
|
138
|
+
rect.width = rect.right - rect.left;
|
|
139
|
+
rect.height = rect.bottom - rect.top;
|
|
140
|
+
if (rect.width < 0 || rect.height < 0) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return new Rect(rect);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Returns the area of intersection with another rect.
|
|
149
|
+
*
|
|
150
|
+
* @returns Area of intersection.
|
|
151
|
+
*/
|
|
152
|
+
getIntersectionArea(anotherRect) {
|
|
153
|
+
const rect = this.getIntersection(anotherRect);
|
|
154
|
+
if (rect) {
|
|
155
|
+
return rect.getArea();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Returns the area of the rect.
|
|
163
|
+
*/
|
|
164
|
+
getArea() {
|
|
165
|
+
return this.width * this.height;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Returns a new rect, a part of the original rect, which is actually visible to the user,
|
|
169
|
+
* e.g. an original rect cropped by parent element rects which have `overflow` set in CSS
|
|
170
|
+
* other than `"visible"`.
|
|
171
|
+
*
|
|
172
|
+
* If there's no such visible rect, which is when the rect is limited by one or many of
|
|
173
|
+
* the ancestors, `null` is returned.
|
|
174
|
+
*
|
|
175
|
+
* **Note**: This method does not consider the boundaries of the viewport (window).
|
|
176
|
+
* To get a rect cropped by all ancestors and the viewport, use an intersection such as:
|
|
177
|
+
*
|
|
178
|
+
* ```ts
|
|
179
|
+
* const visibleInViewportRect = new Rect( window ).getIntersection( new Rect( source ).getVisible() );
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @returns A visible rect instance or `null`, if there's none.
|
|
183
|
+
*/
|
|
184
|
+
getVisible() {
|
|
185
|
+
const source = this._source;
|
|
186
|
+
let visibleRect = this.clone();
|
|
187
|
+
// There's no ancestor to crop <body> with the overflow.
|
|
188
|
+
if (isBody(source)) {
|
|
189
|
+
return visibleRect;
|
|
190
|
+
}
|
|
191
|
+
let child = source;
|
|
192
|
+
let parent = source.parentNode || source.commonAncestorContainer;
|
|
193
|
+
let absolutelyPositionedChildElement;
|
|
194
|
+
// Check the ancestors all the way up to the <body>.
|
|
195
|
+
while (parent && !isBody(parent)) {
|
|
196
|
+
if (child instanceof HTMLElement && getElementPosition(child) === 'absolute') {
|
|
197
|
+
absolutelyPositionedChildElement = child;
|
|
198
|
+
}
|
|
199
|
+
// The child will be cropped only if it has `position: absolute` and the parent has `position: relative` + some overflow.
|
|
200
|
+
// Otherwise there's no chance of visual clipping and the parent can be skipped
|
|
201
|
+
// https://github.com/ckeditor/ckeditor5/issues/14107.
|
|
202
|
+
if (absolutelyPositionedChildElement &&
|
|
203
|
+
(getElementPosition(parent) !== 'relative' || getElementOverflow(parent) === 'visible')) {
|
|
204
|
+
child = parent;
|
|
205
|
+
parent = parent.parentNode;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const parentRect = new Rect(parent);
|
|
209
|
+
const intersectionRect = visibleRect.getIntersection(parentRect);
|
|
210
|
+
if (intersectionRect) {
|
|
211
|
+
if (intersectionRect.getArea() < visibleRect.getArea()) {
|
|
212
|
+
// Reduce the visible rect to the intersection.
|
|
213
|
+
visibleRect = intersectionRect;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// There's no intersection, the rect is completely invisible.
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
child = parent;
|
|
221
|
+
parent = parent.parentNode;
|
|
222
|
+
}
|
|
223
|
+
return visibleRect;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Checks if all property values ({@link #top}, {@link #left}, {@link #right},
|
|
227
|
+
* {@link #bottom}, {@link #width} and {@link #height}) are the equal in both rect
|
|
228
|
+
* instances.
|
|
229
|
+
*
|
|
230
|
+
* @param anotherRect A rect instance to compare with.
|
|
231
|
+
* @returns `true` when Rects are equal. `false` otherwise.
|
|
232
|
+
*/
|
|
233
|
+
isEqual(anotherRect) {
|
|
234
|
+
for (const prop of rectProperties) {
|
|
235
|
+
if (this[prop] !== anotherRect[prop]) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Checks whether a rect fully contains another rect instance.
|
|
243
|
+
*
|
|
244
|
+
* @param anotherRect
|
|
245
|
+
* @returns `true` if contains, `false` otherwise.
|
|
246
|
+
*/
|
|
247
|
+
contains(anotherRect) {
|
|
248
|
+
const intersectRect = this.getIntersection(anotherRect);
|
|
249
|
+
return !!(intersectRect && intersectRect.isEqual(anotherRect));
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Excludes scrollbars and CSS borders from the rect.
|
|
253
|
+
*
|
|
254
|
+
* * Borders are removed when {@link #_source} is an HTML element.
|
|
255
|
+
* * Scrollbars are excluded from HTML elements and the `window`.
|
|
256
|
+
*
|
|
257
|
+
* @returns A rect which has been updated.
|
|
258
|
+
*/
|
|
259
|
+
excludeScrollbarsAndBorders() {
|
|
260
|
+
const source = this._source;
|
|
261
|
+
let scrollBarWidth, scrollBarHeight, direction;
|
|
262
|
+
if (isWindow(source)) {
|
|
263
|
+
scrollBarWidth = source.innerWidth - source.document.documentElement.clientWidth;
|
|
264
|
+
scrollBarHeight = source.innerHeight - source.document.documentElement.clientHeight;
|
|
265
|
+
direction = source.getComputedStyle(source.document.documentElement).direction;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
const borderWidths = getBorderWidths(source);
|
|
269
|
+
scrollBarWidth = source.offsetWidth - source.clientWidth - borderWidths.left - borderWidths.right;
|
|
270
|
+
scrollBarHeight = source.offsetHeight - source.clientHeight - borderWidths.top - borderWidths.bottom;
|
|
271
|
+
direction = source.ownerDocument.defaultView.getComputedStyle(source).direction;
|
|
272
|
+
this.left += borderWidths.left;
|
|
273
|
+
this.top += borderWidths.top;
|
|
274
|
+
this.right -= borderWidths.right;
|
|
275
|
+
this.bottom -= borderWidths.bottom;
|
|
276
|
+
this.width = this.right - this.left;
|
|
277
|
+
this.height = this.bottom - this.top;
|
|
278
|
+
}
|
|
279
|
+
this.width -= scrollBarWidth;
|
|
280
|
+
if (direction === 'ltr') {
|
|
281
|
+
this.right -= scrollBarWidth;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
this.left += scrollBarWidth;
|
|
285
|
+
}
|
|
286
|
+
this.height -= scrollBarHeight;
|
|
287
|
+
this.bottom -= scrollBarHeight;
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Returns an array of rects of the given native DOM Range.
|
|
292
|
+
*
|
|
293
|
+
* @param range A native DOM range.
|
|
294
|
+
* @returns DOM Range rects.
|
|
295
|
+
*/
|
|
296
|
+
static getDomRangeRects(range) {
|
|
297
|
+
const rects = [];
|
|
298
|
+
// Safari does not iterate over ClientRectList using for...of loop.
|
|
299
|
+
const clientRects = Array.from(range.getClientRects());
|
|
300
|
+
if (clientRects.length) {
|
|
301
|
+
for (const rect of clientRects) {
|
|
302
|
+
rects.push(new Rect(rect));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// If there's no client rects for the Range, use parent container's bounding rect
|
|
306
|
+
// instead and adjust rect's width to simulate the actual geometry of such range.
|
|
307
|
+
// https://github.com/ckeditor/ckeditor5-utils/issues/153
|
|
308
|
+
// https://github.com/ckeditor/ckeditor5-ui/issues/317
|
|
309
|
+
else {
|
|
310
|
+
let startContainer = range.startContainer;
|
|
311
|
+
if (isText(startContainer)) {
|
|
312
|
+
startContainer = startContainer.parentNode;
|
|
313
|
+
}
|
|
314
|
+
const rect = new Rect(startContainer.getBoundingClientRect());
|
|
315
|
+
rect.right = rect.left;
|
|
316
|
+
rect.width = 0;
|
|
317
|
+
rects.push(rect);
|
|
318
|
+
}
|
|
319
|
+
return rects;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Returns a bounding rectangle that contains all the given `rects`.
|
|
323
|
+
*
|
|
324
|
+
* @param rects A list of rectangles that should be contained in the result rectangle.
|
|
325
|
+
* @returns Bounding rectangle or `null` if no `rects` were given.
|
|
326
|
+
*/
|
|
327
|
+
static getBoundingRect(rects) {
|
|
328
|
+
const boundingRectData = {
|
|
329
|
+
left: Number.POSITIVE_INFINITY,
|
|
330
|
+
top: Number.POSITIVE_INFINITY,
|
|
331
|
+
right: Number.NEGATIVE_INFINITY,
|
|
332
|
+
bottom: Number.NEGATIVE_INFINITY,
|
|
333
|
+
width: 0,
|
|
334
|
+
height: 0
|
|
335
|
+
};
|
|
336
|
+
let rectangleCount = 0;
|
|
337
|
+
for (const rect of rects) {
|
|
338
|
+
rectangleCount++;
|
|
339
|
+
boundingRectData.left = Math.min(boundingRectData.left, rect.left);
|
|
340
|
+
boundingRectData.top = Math.min(boundingRectData.top, rect.top);
|
|
341
|
+
boundingRectData.right = Math.max(boundingRectData.right, rect.right);
|
|
342
|
+
boundingRectData.bottom = Math.max(boundingRectData.bottom, rect.bottom);
|
|
343
|
+
}
|
|
344
|
+
if (rectangleCount == 0) {
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
boundingRectData.width = boundingRectData.right - boundingRectData.left;
|
|
348
|
+
boundingRectData.height = boundingRectData.bottom - boundingRectData.top;
|
|
349
|
+
return new Rect(boundingRectData);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Acquires all the rect properties from the passed source.
|
|
354
|
+
*/
|
|
355
|
+
function copyRectProperties(rect, source) {
|
|
356
|
+
for (const p of rectProperties) {
|
|
357
|
+
rect[p] = source[p];
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Checks if provided object is a <body> HTML element.
|
|
362
|
+
*/
|
|
363
|
+
function isBody(value) {
|
|
364
|
+
if (!isDomElement(value)) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
return value === value.ownerDocument.body;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Checks if provided object "looks like" a DOM Element and has API required by `Rect` class.
|
|
371
|
+
*/
|
|
372
|
+
function isDomElement(value) {
|
|
373
|
+
// Note: earlier we used `isElement()` from lodash library, however that function is less performant because
|
|
374
|
+
// it makes complicated checks to make sure that given value is a DOM element.
|
|
375
|
+
return value !== null && typeof value === 'object' && value.nodeType === 1 && typeof value.getBoundingClientRect === 'function';
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Returns the value of the `position` style of an `HTMLElement`.
|
|
379
|
+
*/
|
|
380
|
+
function getElementPosition(element) {
|
|
381
|
+
return element.ownerDocument.defaultView.getComputedStyle(element).position;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Returns the value of the `overflow` style of an `HTMLElement`.
|
|
385
|
+
*/
|
|
386
|
+
function getElementOverflow(element) {
|
|
387
|
+
return element.ownerDocument.defaultView.getComputedStyle(element).overflow;
|
|
388
|
+
}
|