@ckeditor/ckeditor5-utils 34.2.0 → 35.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/LICENSE.md +1 -1
  3. package/package.json +19 -8
  4. package/src/areconnectedthroughproperties.js +54 -71
  5. package/src/ckeditorerror.js +92 -114
  6. package/src/collection.js +594 -762
  7. package/src/comparearrays.js +22 -28
  8. package/src/config.js +193 -223
  9. package/src/count.js +8 -12
  10. package/src/diff.js +85 -110
  11. package/src/difftochanges.js +47 -57
  12. package/src/dom/createelement.js +17 -25
  13. package/src/dom/emittermixin.js +202 -263
  14. package/src/dom/getancestors.js +9 -13
  15. package/src/dom/getborderwidths.js +10 -13
  16. package/src/dom/getcommonancestor.js +9 -15
  17. package/src/dom/getdatafromelement.js +5 -9
  18. package/src/dom/getpositionedancestor.js +9 -14
  19. package/src/dom/global.js +15 -4
  20. package/src/dom/indexof.js +7 -11
  21. package/src/dom/insertat.js +2 -4
  22. package/src/dom/iscomment.js +2 -5
  23. package/src/dom/isnode.js +10 -12
  24. package/src/dom/isrange.js +2 -4
  25. package/src/dom/istext.js +2 -4
  26. package/src/dom/isvisible.js +2 -4
  27. package/src/dom/iswindow.js +11 -16
  28. package/src/dom/position.js +220 -410
  29. package/src/dom/rect.js +335 -414
  30. package/src/dom/remove.js +5 -8
  31. package/src/dom/resizeobserver.js +109 -342
  32. package/src/dom/scroll.js +151 -183
  33. package/src/dom/setdatainelement.js +5 -9
  34. package/src/dom/tounit.js +10 -12
  35. package/src/elementreplacer.js +30 -44
  36. package/src/emittermixin.js +368 -634
  37. package/src/env.js +109 -116
  38. package/src/eventinfo.js +12 -65
  39. package/src/fastdiff.js +96 -128
  40. package/src/first.js +8 -12
  41. package/src/focustracker.js +77 -133
  42. package/src/index.js +0 -9
  43. package/src/inserttopriorityarray.js +9 -30
  44. package/src/isiterable.js +2 -4
  45. package/src/keyboard.js +117 -196
  46. package/src/keystrokehandler.js +72 -88
  47. package/src/language.js +9 -15
  48. package/src/locale.js +61 -158
  49. package/src/mapsequal.js +12 -17
  50. package/src/mix.js +17 -16
  51. package/src/nth.js +8 -11
  52. package/src/objecttomap.js +7 -11
  53. package/src/observablemixin.js +474 -778
  54. package/src/priorities.js +20 -32
  55. package/src/spy.js +3 -6
  56. package/src/toarray.js +2 -13
  57. package/src/tomap.js +8 -10
  58. package/src/translation-service.js +57 -93
  59. package/src/uid.js +34 -38
  60. package/src/unicode.js +28 -43
  61. package/src/version.js +134 -143
package/src/dom/scroll.js CHANGED
@@ -2,107 +2,88 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/dom/scroll
8
7
  */
9
-
10
8
  import isRange from './isrange';
11
9
  import Rect from './rect';
12
10
  import isText from './istext';
13
-
14
- const utils = {};
15
-
16
11
  /**
17
12
  * Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
18
13
  * This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
19
14
  * the user. If the `target` is already visible, nothing will happen.
20
15
  *
16
+ * @param {Object} options
21
17
  * @param {HTMLElement|Range} options.target A target, which supposed to become visible to the user.
22
18
  * @param {Number} [options.viewportOffset] An offset from the edge of the viewport (in pixels)
23
19
  * the `target` will be moved by when the viewport is scrolled. It enhances the user experience
24
20
  * by keeping the `target` some distance from the edge of the viewport and thus making it easier to
25
21
  * read or edit by the user.
26
22
  */
27
- export function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
28
- const targetWindow = getWindow( target );
29
- let currentWindow = targetWindow;
30
- let currentFrame = null;
31
-
32
- // Iterate over all windows, starting from target's parent window up to window#top.
33
- while ( currentWindow ) {
34
- let firstAncestorToScroll;
35
-
36
- // Let's scroll target's ancestors first to reveal it. Then, once the ancestor scrolls
37
- // settled down, the algorithm can eventually scroll the viewport of the current window.
38
- //
39
- // Note: If the current window is target's **original** window (e.g. the first one),
40
- // start scrolling the closest parent of the target. If not, scroll the closest parent
41
- // of an iframe that resides in the current window.
42
- if ( currentWindow == targetWindow ) {
43
- firstAncestorToScroll = getParentElement( target );
44
- } else {
45
- firstAncestorToScroll = getParentElement( currentFrame );
46
- }
47
-
48
- // Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
49
- scrollAncestorsToShowRect( firstAncestorToScroll, () => {
50
- // Note: If the target does not belong to the current window **directly**,
51
- // i.e. it resides in an iframe belonging to the window, obtain the target's rect
52
- // in the coordinates of the current window. By default, a Rect returns geometry
53
- // relative to the current window's viewport. To make it work in a parent window,
54
- // it must be shifted.
55
- return getRectRelativeToWindow( target, currentWindow );
56
- } );
57
-
58
- // Obtain the rect of the target after it has been scrolled within its ancestors.
59
- // It's time to scroll the viewport.
60
- const targetRect = getRectRelativeToWindow( target, currentWindow );
61
-
62
- scrollWindowToShowRect( currentWindow, targetRect, viewportOffset );
63
-
64
- if ( currentWindow.parent != currentWindow ) {
65
- // Keep the reference to the <iframe> element the "previous current window" was
66
- // rendered within. It will be useful to re–calculate the rect of the target
67
- // in the parent window's relative geometry. The target's rect must be shifted
68
- // by it's iframe's position.
69
- currentFrame = currentWindow.frameElement;
70
- currentWindow = currentWindow.parent;
71
-
72
- // If the current window has some parent but frameElement is inaccessible, then they have
73
- // different domains/ports and, due to security reasons, accessing and scrolling
74
- // the parent window won't be possible.
75
- // See https://github.com/ckeditor/ckeditor5/issues/930.
76
- if ( !currentFrame ) {
77
- return;
78
- }
79
- } else {
80
- currentWindow = null;
81
- }
82
- }
23
+ export function scrollViewportToShowTarget({ target, viewportOffset = 0 }) {
24
+ const targetWindow = getWindow(target);
25
+ let currentWindow = targetWindow;
26
+ let currentFrame = null;
27
+ // Iterate over all windows, starting from target's parent window up to window#top.
28
+ while (currentWindow) {
29
+ let firstAncestorToScroll;
30
+ // Let's scroll target's ancestors first to reveal it. Then, once the ancestor scrolls
31
+ // settled down, the algorithm can eventually scroll the viewport of the current window.
32
+ //
33
+ // Note: If the current window is target's **original** window (e.g. the first one),
34
+ // start scrolling the closest parent of the target. If not, scroll the closest parent
35
+ // of an iframe that resides in the current window.
36
+ if (currentWindow == targetWindow) {
37
+ firstAncestorToScroll = getParentElement(target);
38
+ }
39
+ else {
40
+ firstAncestorToScroll = getParentElement(currentFrame);
41
+ }
42
+ // Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
43
+ scrollAncestorsToShowRect(firstAncestorToScroll, () => {
44
+ // Note: If the target does not belong to the current window **directly**,
45
+ // i.e. it resides in an iframe belonging to the window, obtain the target's rect
46
+ // in the coordinates of the current window. By default, a Rect returns geometry
47
+ // relative to the current window's viewport. To make it work in a parent window,
48
+ // it must be shifted.
49
+ return getRectRelativeToWindow(target, currentWindow);
50
+ });
51
+ // Obtain the rect of the target after it has been scrolled within its ancestors.
52
+ // It's time to scroll the viewport.
53
+ const targetRect = getRectRelativeToWindow(target, currentWindow);
54
+ scrollWindowToShowRect(currentWindow, targetRect, viewportOffset);
55
+ if (currentWindow.parent != currentWindow) {
56
+ // Keep the reference to the <iframe> element the "previous current window" was
57
+ // rendered within. It will be useful to re–calculate the rect of the target
58
+ // in the parent window's relative geometry. The target's rect must be shifted
59
+ // by it's iframe's position.
60
+ currentFrame = currentWindow.frameElement;
61
+ currentWindow = currentWindow.parent;
62
+ // If the current window has some parent but frameElement is inaccessible, then they have
63
+ // different domains/ports and, due to security reasons, accessing and scrolling
64
+ // the parent window won't be possible.
65
+ // See https://github.com/ckeditor/ckeditor5/issues/930.
66
+ if (!currentFrame) {
67
+ return;
68
+ }
69
+ }
70
+ else {
71
+ currentWindow = null;
72
+ }
73
+ }
83
74
  }
84
-
85
75
  /**
86
76
  * Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
87
77
  * e.g. if they have `overflow: scroll` CSS style.
88
78
  *
89
79
  * @param {HTMLElement|Range} target A target, which supposed to become visible to the user.
90
80
  */
91
- export function scrollAncestorsToShowTarget( target ) {
92
- const targetParent = getParentElement( target );
93
-
94
- scrollAncestorsToShowRect( targetParent, () => {
95
- return new Rect( target );
96
- } );
81
+ export function scrollAncestorsToShowTarget(target) {
82
+ const targetParent = getParentElement(target);
83
+ scrollAncestorsToShowRect(targetParent, () => {
84
+ return new Rect(target);
85
+ });
97
86
  }
98
-
99
- // TODO: Using a property value shorthand in the top of the file
100
- // causes JSDoc to throw errors. See https://github.com/cksource/docs-builder/issues/75.
101
- Object.assign( utils, {
102
- scrollViewportToShowTarget,
103
- scrollAncestorsToShowTarget
104
- } );
105
-
106
87
  // Makes a given rect visible within its parent window.
107
88
  //
108
89
  // Note: Avoid the situation where the caret is still in the viewport, but totally
@@ -144,134 +125,125 @@ Object.assign( utils, {
144
125
  // @param {Window} window A window which is scrolled to reveal the rect.
145
126
  // @param {module:utils/dom/rect~Rect} rect A rect which is to be revealed.
146
127
  // @param {Number} viewportOffset See scrollViewportToShowTarget.
147
- function scrollWindowToShowRect( window, rect, viewportOffset ) {
148
- const targetShiftedDownRect = rect.clone().moveBy( 0, viewportOffset );
149
- const targetShiftedUpRect = rect.clone().moveBy( 0, -viewportOffset );
150
- const viewportRect = new Rect( window ).excludeScrollbarsAndBorders();
151
-
152
- const rects = [ targetShiftedUpRect, targetShiftedDownRect ];
153
-
154
- if ( !rects.every( rect => viewportRect.contains( rect ) ) ) {
155
- let { scrollX, scrollY } = window;
156
-
157
- if ( isAbove( targetShiftedUpRect, viewportRect ) ) {
158
- scrollY -= viewportRect.top - rect.top + viewportOffset;
159
- } else if ( isBelow( targetShiftedDownRect, viewportRect ) ) {
160
- scrollY += rect.bottom - viewportRect.bottom + viewportOffset;
161
- }
162
-
163
- // TODO: Web browsers scroll natively to place the target in the middle
164
- // of the viewport. It's not a very popular case, though.
165
- if ( isLeftOf( rect, viewportRect ) ) {
166
- scrollX -= viewportRect.left - rect.left + viewportOffset;
167
- } else if ( isRightOf( rect, viewportRect ) ) {
168
- scrollX += rect.right - viewportRect.right + viewportOffset;
169
- }
170
-
171
- window.scrollTo( scrollX, scrollY );
172
- }
128
+ function scrollWindowToShowRect(window, rect, viewportOffset) {
129
+ const targetShiftedDownRect = rect.clone().moveBy(0, viewportOffset);
130
+ const targetShiftedUpRect = rect.clone().moveBy(0, -viewportOffset);
131
+ const viewportRect = new Rect(window).excludeScrollbarsAndBorders();
132
+ const rects = [targetShiftedUpRect, targetShiftedDownRect];
133
+ if (!rects.every(rect => viewportRect.contains(rect))) {
134
+ let { scrollX, scrollY } = window;
135
+ if (isAbove(targetShiftedUpRect, viewportRect)) {
136
+ scrollY -= viewportRect.top - rect.top + viewportOffset;
137
+ }
138
+ else if (isBelow(targetShiftedDownRect, viewportRect)) {
139
+ scrollY += rect.bottom - viewportRect.bottom + viewportOffset;
140
+ }
141
+ // TODO: Web browsers scroll natively to place the target in the middle
142
+ // of the viewport. It's not a very popular case, though.
143
+ if (isLeftOf(rect, viewportRect)) {
144
+ scrollX -= viewportRect.left - rect.left + viewportOffset;
145
+ }
146
+ else if (isRightOf(rect, viewportRect)) {
147
+ scrollX += rect.right - viewportRect.right + viewportOffset;
148
+ }
149
+ window.scrollTo(scrollX, scrollY);
150
+ }
173
151
  }
174
-
175
152
  // Recursively scrolls element ancestors to visually reveal a rect.
176
153
  //
177
154
  // @private
178
155
  // @param {HTMLElement} A parent The first ancestors to start scrolling.
179
156
  // @param {Function} getRect A function which returns the Rect, which is to be revealed.
180
- function scrollAncestorsToShowRect( parent, getRect ) {
181
- const parentWindow = getWindow( parent );
182
- let parentRect, targetRect;
183
-
184
- while ( parent != parentWindow.document.body ) {
185
- targetRect = getRect();
186
- parentRect = new Rect( parent ).excludeScrollbarsAndBorders();
187
-
188
- if ( !parentRect.contains( targetRect ) ) {
189
- if ( isAbove( targetRect, parentRect ) ) {
190
- parent.scrollTop -= parentRect.top - targetRect.top;
191
- } else if ( isBelow( targetRect, parentRect ) ) {
192
- parent.scrollTop += targetRect.bottom - parentRect.bottom;
193
- }
194
-
195
- if ( isLeftOf( targetRect, parentRect ) ) {
196
- parent.scrollLeft -= parentRect.left - targetRect.left;
197
- } else if ( isRightOf( targetRect, parentRect ) ) {
198
- parent.scrollLeft += targetRect.right - parentRect.right;
199
- }
200
- }
201
-
202
- parent = parent.parentNode;
203
- }
157
+ function scrollAncestorsToShowRect(parent, getRect) {
158
+ const parentWindow = getWindow(parent);
159
+ let parentRect, targetRect;
160
+ while (parent != parentWindow.document.body) {
161
+ targetRect = getRect();
162
+ parentRect = new Rect(parent).excludeScrollbarsAndBorders();
163
+ if (!parentRect.contains(targetRect)) {
164
+ if (isAbove(targetRect, parentRect)) {
165
+ parent.scrollTop -= parentRect.top - targetRect.top;
166
+ }
167
+ else if (isBelow(targetRect, parentRect)) {
168
+ parent.scrollTop += targetRect.bottom - parentRect.bottom;
169
+ }
170
+ if (isLeftOf(targetRect, parentRect)) {
171
+ parent.scrollLeft -= parentRect.left - targetRect.left;
172
+ }
173
+ else if (isRightOf(targetRect, parentRect)) {
174
+ parent.scrollLeft += targetRect.right - parentRect.right;
175
+ }
176
+ }
177
+ parent = parent.parentNode;
178
+ }
204
179
  }
205
-
206
180
  // Determines if a given `Rect` extends beyond the bottom edge of the second `Rect`.
207
181
  //
208
182
  // @private
209
183
  // @param {module:utils/dom/rect~Rect} firstRect
210
184
  // @param {module:utils/dom/rect~Rect} secondRect
211
- function isBelow( firstRect, secondRect ) {
212
- return firstRect.bottom > secondRect.bottom;
185
+ // @returns {Boolean}
186
+ function isBelow(firstRect, secondRect) {
187
+ return firstRect.bottom > secondRect.bottom;
213
188
  }
214
-
215
189
  // Determines if a given `Rect` extends beyond the top edge of the second `Rect`.
216
190
  //
217
191
  // @private
218
192
  // @param {module:utils/dom/rect~Rect} firstRect
219
193
  // @param {module:utils/dom/rect~Rect} secondRect
220
- function isAbove( firstRect, secondRect ) {
221
- return firstRect.top < secondRect.top;
194
+ // @returns {Boolean}
195
+ function isAbove(firstRect, secondRect) {
196
+ return firstRect.top < secondRect.top;
222
197
  }
223
-
224
198
  // Determines if a given `Rect` extends beyond the left edge of the second `Rect`.
225
199
  //
226
200
  // @private
227
201
  // @param {module:utils/dom/rect~Rect} firstRect
228
202
  // @param {module:utils/dom/rect~Rect} secondRect
229
- function isLeftOf( firstRect, secondRect ) {
230
- return firstRect.left < secondRect.left;
203
+ // @returns {Boolean}
204
+ function isLeftOf(firstRect, secondRect) {
205
+ return firstRect.left < secondRect.left;
231
206
  }
232
-
233
207
  // Determines if a given `Rect` extends beyond the right edge of the second `Rect`.
234
208
  //
235
209
  // @private
236
210
  // @param {module:utils/dom/rect~Rect} firstRect
237
211
  // @param {module:utils/dom/rect~Rect} secondRect
238
- function isRightOf( firstRect, secondRect ) {
239
- return firstRect.right > secondRect.right;
212
+ // @returns {Boolean}
213
+ function isRightOf(firstRect, secondRect) {
214
+ return firstRect.right > secondRect.right;
240
215
  }
241
-
242
216
  // Returns the closest window of an element or range.
243
217
  //
244
218
  // @private
245
- // @param {HTMLElement|Range} firstRect
219
+ // @param {HTMLElement|Range} elementOrRange
246
220
  // @returns {Window}
247
- function getWindow( elementOrRange ) {
248
- if ( isRange( elementOrRange ) ) {
249
- return elementOrRange.startContainer.ownerDocument.defaultView;
250
- } else {
251
- return elementOrRange.ownerDocument.defaultView;
252
- }
221
+ function getWindow(elementOrRange) {
222
+ if (isRange(elementOrRange)) {
223
+ return elementOrRange.startContainer.ownerDocument.defaultView;
224
+ }
225
+ else {
226
+ return elementOrRange.ownerDocument.defaultView;
227
+ }
253
228
  }
254
-
255
229
  // Returns the closest parent of an element or DOM range.
256
230
  //
257
231
  // @private
258
- // @param {HTMLElement|Range} firstRect
232
+ // @param {HTMLElement|Range} elementOrRange
259
233
  // @returns {HTMLelement}
260
- function getParentElement( elementOrRange ) {
261
- if ( isRange( elementOrRange ) ) {
262
- let parent = elementOrRange.commonAncestorContainer;
263
-
264
- // If a Range is attached to the Text, use the closest element ancestor.
265
- if ( isText( parent ) ) {
266
- parent = parent.parentNode;
267
- }
268
-
269
- return parent;
270
- } else {
271
- return elementOrRange.parentNode;
272
- }
234
+ function getParentElement(elementOrRange) {
235
+ if (isRange(elementOrRange)) {
236
+ let parent = elementOrRange.commonAncestorContainer;
237
+ // If a Range is attached to the Text, use the closest element ancestor.
238
+ if (isText(parent)) {
239
+ parent = parent.parentNode;
240
+ }
241
+ return parent;
242
+ }
243
+ else {
244
+ return elementOrRange.parentNode;
245
+ }
273
246
  }
274
-
275
247
  // Returns the rect of an element or range residing in an iframe.
276
248
  // The result rect is relative to the geometry of the passed window instance.
277
249
  //
@@ -279,24 +251,20 @@ function getParentElement( elementOrRange ) {
279
251
  // @param {HTMLElement|Range} target Element or range which rect should be returned.
280
252
  // @param {Window} relativeWindow A window the rect should be relative to.
281
253
  // @returns {module:utils/dom/rect~Rect}
282
- function getRectRelativeToWindow( target, relativeWindow ) {
283
- const targetWindow = getWindow( target );
284
- const rect = new Rect( target );
285
-
286
- if ( targetWindow === relativeWindow ) {
287
- return rect;
288
- } else {
289
- let currentWindow = targetWindow;
290
-
291
- while ( currentWindow != relativeWindow ) {
292
- const frame = currentWindow.frameElement;
293
- const frameRect = new Rect( frame ).excludeScrollbarsAndBorders();
294
-
295
- rect.moveBy( frameRect.left, frameRect.top );
296
-
297
- currentWindow = currentWindow.parent;
298
- }
299
- }
300
-
301
- return rect;
254
+ function getRectRelativeToWindow(target, relativeWindow) {
255
+ const targetWindow = getWindow(target);
256
+ const rect = new Rect(target);
257
+ if (targetWindow === relativeWindow) {
258
+ return rect;
259
+ }
260
+ else {
261
+ let currentWindow = targetWindow;
262
+ while (currentWindow != relativeWindow) {
263
+ const frame = currentWindow.frameElement;
264
+ const frameRect = new Rect(frame).excludeScrollbarsAndBorders();
265
+ rect.moveBy(frameRect.left, frameRect.top);
266
+ currentWindow = currentWindow.parent;
267
+ }
268
+ }
269
+ return rect;
302
270
  }
@@ -2,23 +2,19 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/dom/setdatainelement
8
7
  */
9
-
10
8
  /* globals HTMLTextAreaElement */
11
-
12
9
  /**
13
10
  * Sets data in a given element.
14
11
  *
15
12
  * @param {HTMLElement} el The element in which the data will be set.
16
13
  * @param {String} data The data string.
17
14
  */
18
- export default function setDataInElement( el, data ) {
19
- if ( el instanceof HTMLTextAreaElement ) {
20
- el.value = data;
21
- }
22
-
23
- el.innerHTML = data;
15
+ export default function setDataInElement(el, data) {
16
+ if (el instanceof HTMLTextAreaElement) {
17
+ el.value = data;
18
+ }
19
+ el.innerHTML = data;
24
20
  }
package/src/dom/tounit.js CHANGED
@@ -2,11 +2,9 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/dom/tounit
8
7
  */
9
-
10
8
  /**
11
9
  * Returns a helper function, which adds a desired trailing
12
10
  * `unit` to the passed value.
@@ -14,14 +12,14 @@
14
12
  * @param {String} unit An unit like "px" or "em".
15
13
  * @returns {module:utils/dom/tounit~helper}
16
14
  */
17
- export default function toUnit( unit ) {
18
- /**
19
- * A function, which adds a pre–defined trailing `unit`
20
- * to the passed `value`.
21
- *
22
- * @function helper
23
- * @param {*} value A value to be given the unit.
24
- * @returns {String} A value with the trailing unit.
25
- */
26
- return value => value + unit;
15
+ export default function toUnit(unit) {
16
+ /**
17
+ * A function, which adds a pre–defined trailing `unit`
18
+ * to the passed `value`.
19
+ *
20
+ * @function helper
21
+ * @param {String|Number} value A value to be given the unit.
22
+ * @returns {String} A value with the trailing unit.
23
+ */
24
+ return value => value + unit;
27
25
  }
@@ -2,56 +2,42 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/elementreplacer
8
7
  */
9
-
10
8
  /**
11
9
  * Utility class allowing to hide existing HTML elements or replace them with given ones in a way that doesn't remove
12
10
  * the original elements from the DOM.
13
11
  */
14
12
  export default class ElementReplacer {
15
- constructor() {
16
- /**
17
- * The elements replaced by {@link #replace} and their replacements.
18
- *
19
- * @private
20
- * @member {Array.<Object>}
21
- */
22
- this._replacedElements = [];
23
- }
24
-
25
- /**
26
- * Hides the `element` and, if specified, inserts the the given element next to it.
27
- *
28
- * The effect of this method can be reverted by {@link #restore}.
29
- *
30
- * @param {HTMLElement} element The element to replace.
31
- * @param {HTMLElement} [newElement] The replacement element. If not passed, then the `element` will just be hidden.
32
- */
33
- replace( element, newElement ) {
34
- this._replacedElements.push( { element, newElement } );
35
-
36
- element.style.display = 'none';
37
-
38
- if ( newElement ) {
39
- element.parentNode.insertBefore( newElement, element.nextSibling );
40
- }
41
- }
42
-
43
- /**
44
- * Restores what {@link #replace} did.
45
- */
46
- restore() {
47
- this._replacedElements.forEach( ( { element, newElement } ) => {
48
- element.style.display = '';
49
-
50
- if ( newElement ) {
51
- newElement.remove();
52
- }
53
- } );
54
-
55
- this._replacedElements = [];
56
- }
13
+ constructor() {
14
+ this._replacedElements = [];
15
+ }
16
+ /**
17
+ * Hides the `element` and, if specified, inserts the the given element next to it.
18
+ *
19
+ * The effect of this method can be reverted by {@link #restore}.
20
+ *
21
+ * @param {HTMLElement} element The element to replace.
22
+ * @param {HTMLElement} [newElement] The replacement element. If not passed, then the `element` will just be hidden.
23
+ */
24
+ replace(element, newElement) {
25
+ this._replacedElements.push({ element, newElement });
26
+ element.style.display = 'none';
27
+ if (newElement) {
28
+ element.parentNode.insertBefore(newElement, element.nextSibling);
29
+ }
30
+ }
31
+ /**
32
+ * Restores what {@link #replace} did.
33
+ */
34
+ restore() {
35
+ this._replacedElements.forEach(({ element, newElement }) => {
36
+ element.style.display = '';
37
+ if (newElement) {
38
+ newElement.remove();
39
+ }
40
+ });
41
+ this._replacedElements = [];
42
+ }
57
43
  }