@ckeditor/ckeditor5-engine 35.0.1 → 35.2.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 (124) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/package.json +30 -24
  3. package/src/controller/datacontroller.js +467 -561
  4. package/src/controller/editingcontroller.js +168 -204
  5. package/src/conversion/conversion.js +541 -565
  6. package/src/conversion/conversionhelpers.js +24 -28
  7. package/src/conversion/downcastdispatcher.js +457 -686
  8. package/src/conversion/downcasthelpers.js +1583 -1965
  9. package/src/conversion/mapper.js +518 -707
  10. package/src/conversion/modelconsumable.js +240 -283
  11. package/src/conversion/upcastdispatcher.js +372 -718
  12. package/src/conversion/upcasthelpers.js +707 -818
  13. package/src/conversion/viewconsumable.js +524 -581
  14. package/src/dataprocessor/basichtmlwriter.js +12 -16
  15. package/src/dataprocessor/dataprocessor.js +5 -0
  16. package/src/dataprocessor/htmldataprocessor.js +100 -116
  17. package/src/dataprocessor/htmlwriter.js +1 -18
  18. package/src/dataprocessor/xmldataprocessor.js +116 -137
  19. package/src/dev-utils/model.js +260 -352
  20. package/src/dev-utils/operationreplayer.js +106 -126
  21. package/src/dev-utils/utils.js +34 -51
  22. package/src/dev-utils/view.js +632 -753
  23. package/src/index.js +0 -11
  24. package/src/model/batch.js +111 -127
  25. package/src/model/differ.js +988 -1233
  26. package/src/model/document.js +340 -449
  27. package/src/model/documentfragment.js +327 -364
  28. package/src/model/documentselection.js +996 -1189
  29. package/src/model/element.js +306 -410
  30. package/src/model/history.js +224 -262
  31. package/src/model/item.js +5 -0
  32. package/src/model/liveposition.js +84 -145
  33. package/src/model/liverange.js +108 -185
  34. package/src/model/markercollection.js +379 -480
  35. package/src/model/model.js +883 -1034
  36. package/src/model/node.js +419 -463
  37. package/src/model/nodelist.js +176 -201
  38. package/src/model/operation/attributeoperation.js +153 -182
  39. package/src/model/operation/detachoperation.js +64 -83
  40. package/src/model/operation/insertoperation.js +135 -166
  41. package/src/model/operation/markeroperation.js +114 -140
  42. package/src/model/operation/mergeoperation.js +163 -191
  43. package/src/model/operation/moveoperation.js +157 -187
  44. package/src/model/operation/nooperation.js +28 -38
  45. package/src/model/operation/operation.js +106 -125
  46. package/src/model/operation/operationfactory.js +30 -34
  47. package/src/model/operation/renameoperation.js +109 -135
  48. package/src/model/operation/rootattributeoperation.js +155 -188
  49. package/src/model/operation/splitoperation.js +196 -232
  50. package/src/model/operation/transform.js +1833 -2204
  51. package/src/model/operation/utils.js +140 -204
  52. package/src/model/position.js +980 -1053
  53. package/src/model/range.js +910 -1028
  54. package/src/model/rootelement.js +77 -97
  55. package/src/model/schema.js +1189 -1835
  56. package/src/model/selection.js +745 -862
  57. package/src/model/text.js +90 -114
  58. package/src/model/textproxy.js +204 -240
  59. package/src/model/treewalker.js +316 -397
  60. package/src/model/typecheckable.js +16 -0
  61. package/src/model/utils/autoparagraphing.js +32 -44
  62. package/src/model/utils/deletecontent.js +334 -418
  63. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  64. package/src/model/utils/getselectedcontent.js +96 -118
  65. package/src/model/utils/insertcontent.js +757 -773
  66. package/src/model/utils/insertobject.js +96 -119
  67. package/src/model/utils/modifyselection.js +120 -158
  68. package/src/model/utils/selection-post-fixer.js +153 -201
  69. package/src/model/writer.js +1305 -1474
  70. package/src/view/attributeelement.js +189 -225
  71. package/src/view/containerelement.js +75 -85
  72. package/src/view/document.js +172 -215
  73. package/src/view/documentfragment.js +200 -249
  74. package/src/view/documentselection.js +338 -367
  75. package/src/view/domconverter.js +1370 -1617
  76. package/src/view/downcastwriter.js +1747 -2076
  77. package/src/view/editableelement.js +81 -97
  78. package/src/view/element.js +739 -890
  79. package/src/view/elementdefinition.js +5 -0
  80. package/src/view/emptyelement.js +82 -92
  81. package/src/view/filler.js +35 -50
  82. package/src/view/item.js +5 -0
  83. package/src/view/matcher.js +260 -559
  84. package/src/view/node.js +274 -360
  85. package/src/view/observer/arrowkeysobserver.js +19 -28
  86. package/src/view/observer/bubblingemittermixin.js +120 -263
  87. package/src/view/observer/bubblingeventinfo.js +47 -55
  88. package/src/view/observer/clickobserver.js +7 -13
  89. package/src/view/observer/compositionobserver.js +14 -24
  90. package/src/view/observer/domeventdata.js +57 -67
  91. package/src/view/observer/domeventobserver.js +40 -64
  92. package/src/view/observer/fakeselectionobserver.js +81 -96
  93. package/src/view/observer/focusobserver.js +45 -61
  94. package/src/view/observer/inputobserver.js +7 -13
  95. package/src/view/observer/keyobserver.js +17 -27
  96. package/src/view/observer/mouseobserver.js +7 -14
  97. package/src/view/observer/mutationobserver.js +220 -315
  98. package/src/view/observer/observer.js +81 -102
  99. package/src/view/observer/selectionobserver.js +199 -246
  100. package/src/view/observer/tabobserver.js +23 -36
  101. package/src/view/placeholder.js +128 -173
  102. package/src/view/position.js +350 -401
  103. package/src/view/range.js +453 -513
  104. package/src/view/rawelement.js +85 -112
  105. package/src/view/renderer.js +874 -1018
  106. package/src/view/rooteditableelement.js +80 -90
  107. package/src/view/selection.js +608 -689
  108. package/src/view/styles/background.js +43 -44
  109. package/src/view/styles/border.js +220 -276
  110. package/src/view/styles/margin.js +8 -17
  111. package/src/view/styles/padding.js +8 -16
  112. package/src/view/styles/utils.js +127 -160
  113. package/src/view/stylesmap.js +728 -905
  114. package/src/view/text.js +102 -126
  115. package/src/view/textproxy.js +144 -170
  116. package/src/view/treewalker.js +383 -479
  117. package/src/view/typecheckable.js +19 -0
  118. package/src/view/uielement.js +166 -187
  119. package/src/view/upcastwriter.js +395 -449
  120. package/src/view/view.js +569 -664
  121. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  122. package/src/model/item.jsdoc +0 -14
  123. package/src/view/elementdefinition.jsdoc +0 -59
  124. package/src/view/item.jsdoc +0 -14
@@ -2,17 +2,13 @@
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 engine/view/observer/selectionobserver
8
7
  */
9
-
10
8
  /* global setInterval, clearInterval */
11
-
12
9
  import Observer from './observer';
13
10
  import MutationObserver from './mutationobserver';
14
11
  import { debounce } from 'lodash-es';
15
-
16
12
  /**
17
13
  * Selection observer class observes selection changes in the document. If a selection changes on the document this
18
14
  * observer checks if there are any mutations and if the DOM selection is different from the
@@ -28,249 +24,206 @@ import { debounce } from 'lodash-es';
28
24
  * @extends module:engine/view/observer/observer~Observer
29
25
  */
30
26
  export default class SelectionObserver extends Observer {
31
- constructor( view ) {
32
- super( view );
33
-
34
- /**
35
- * Instance of the mutation observer. Selection observer calls
36
- * {@link module:engine/view/observer/mutationobserver~MutationObserver#flush} to ensure that the mutations will be handled
37
- * before the {@link module:engine/view/document~Document#event:selectionChange} event is fired.
38
- *
39
- * @readonly
40
- * @member {module:engine/view/observer/mutationobserver~MutationObserver}
41
- * module:engine/view/observer/selectionobserver~SelectionObserver#mutationObserver
42
- */
43
- this.mutationObserver = view.getObserver( MutationObserver );
44
-
45
- /**
46
- * Reference to the view {@link module:engine/view/documentselection~DocumentSelection} object used to compare
47
- * new selection with it.
48
- *
49
- * @readonly
50
- * @member {module:engine/view/documentselection~DocumentSelection}
51
- * module:engine/view/observer/selectionobserver~SelectionObserver#selection
52
- */
53
- this.selection = this.document.selection;
54
-
55
- /* eslint-disable max-len */
56
- /**
57
- * Reference to the {@link module:engine/view/view~View#domConverter}.
58
- *
59
- * @readonly
60
- * @member {module:engine/view/domconverter~DomConverter} module:engine/view/observer/selectionobserver~SelectionObserver#domConverter
61
- */
62
- /* eslint-enable max-len */
63
- this.domConverter = view.domConverter;
64
-
65
- /**
66
- * A set of documents which have added `selectionchange` listener to avoid adding a listener twice to the same
67
- * document.
68
- *
69
- * @private
70
- * @member {WeakSet.<Document>} module:engine/view/observer/selectionobserver~SelectionObserver#_documents
71
- */
72
- this._documents = new WeakSet();
73
-
74
- /**
75
- * Fires debounced event `selectionChangeDone`. It uses `lodash#debounce` method to delay function call.
76
- *
77
- * @private
78
- * @param {Object} data Selection change data.
79
- * @method #_fireSelectionChangeDoneDebounced
80
- */
81
- this._fireSelectionChangeDoneDebounced = debounce( data => this.document.fire( 'selectionChangeDone', data ), 200 );
82
-
83
- /**
84
- * When called, starts clearing the {@link #_loopbackCounter} counter in time intervals. When the number of selection
85
- * changes exceeds a certain limit within the interval of time, the observer will not fire `selectionChange` but warn about
86
- * possible infinite selection loop.
87
- *
88
- * @private
89
- * @member {Number} #_clearInfiniteLoopInterval
90
- */
91
- this._clearInfiniteLoopInterval = setInterval( () => this._clearInfiniteLoop(), 1000 );
92
-
93
- /**
94
- * Unlocks the `isSelecting` state of the view document in case the selection observer did not record this fact
95
- * correctly (for whatever reason). It is a safeguard (paranoid check), that returns document to the normal state
96
- * after a certain period of time (debounced, postponed by each selectionchange event).
97
- *
98
- * @private
99
- * @method #_documentIsSelectingInactivityTimeoutDebounced
100
- */
101
- this._documentIsSelectingInactivityTimeoutDebounced = debounce( () => ( this.document.isSelecting = false ), 5000 );
102
-
103
- /**
104
- * Private property to check if the code does not enter infinite loop.
105
- *
106
- * @private
107
- * @member {Number} module:engine/view/observer/selectionobserver~SelectionObserver#_loopbackCounter
108
- */
109
- this._loopbackCounter = 0;
110
- }
111
-
112
- /**
113
- * @inheritDoc
114
- */
115
- observe( domElement ) {
116
- const domDocument = domElement.ownerDocument;
117
-
118
- const startDocumentIsSelecting = () => {
119
- this.document.isSelecting = true;
120
-
121
- // Let's activate the safety timeout each time the document enters the "is selecting" state.
122
- this._documentIsSelectingInactivityTimeoutDebounced();
123
- };
124
-
125
- const endDocumentIsSelecting = () => {
126
- this.document.isSelecting = false;
127
-
128
- // The safety timeout can be canceled when the document leaves the "is selecting" state.
129
- this._documentIsSelectingInactivityTimeoutDebounced.cancel();
130
- };
131
-
132
- // The document has the "is selecting" state while the user keeps making (extending) the selection
133
- // (e.g. by holding the mouse button and moving the cursor). The state resets when they either released
134
- // the mouse button or interrupted the process by pressing or releasing any key.
135
- this.listenTo( domElement, 'selectstart', startDocumentIsSelecting, { priority: 'highest' } );
136
- this.listenTo( domElement, 'keydown', endDocumentIsSelecting, { priority: 'highest' } );
137
- this.listenTo( domElement, 'keyup', endDocumentIsSelecting, { priority: 'highest' } );
138
-
139
- // Add document-wide listeners only once. This method could be called for multiple editing roots.
140
- if ( this._documents.has( domDocument ) ) {
141
- return;
142
- }
143
-
144
- this.listenTo( domDocument, 'mouseup', endDocumentIsSelecting, { priority: 'highest' } );
145
- this.listenTo( domDocument, 'selectionchange', ( evt, domEvent ) => {
146
- this._handleSelectionChange( domEvent, domDocument );
147
-
148
- // Defer the safety timeout when the selection changes (e.g. the user keeps extending the selection
149
- // using their mouse).
150
- this._documentIsSelectingInactivityTimeoutDebounced();
151
- } );
152
-
153
- this._documents.add( domDocument );
154
- }
155
-
156
- /**
157
- * @inheritDoc
158
- */
159
- destroy() {
160
- super.destroy();
161
-
162
- clearInterval( this._clearInfiniteLoopInterval );
163
- this._fireSelectionChangeDoneDebounced.cancel();
164
- this._documentIsSelectingInactivityTimeoutDebounced.cancel();
165
- }
166
-
167
- /**
168
- * Selection change listener. {@link module:engine/view/observer/mutationobserver~MutationObserver#flush Flush} mutations, check if
169
- * a selection changes and fires {@link module:engine/view/document~Document#event:selectionChange} event on every change
170
- * and {@link module:engine/view/document~Document#event:selectionChangeDone} when a selection stop changing.
171
- *
172
- * @private
173
- * @param {Event} domEvent DOM event.
174
- * @param {Document} domDocument DOM document.
175
- */
176
- _handleSelectionChange( domEvent, domDocument ) {
177
- if ( !this.isEnabled ) {
178
- return;
179
- }
180
-
181
- const domSelection = domDocument.defaultView.getSelection();
182
-
183
- if ( this.checkShouldIgnoreEventFromTarget( domSelection.anchorNode ) ) {
184
- return;
185
- }
186
-
187
- // Ensure the mutation event will be before selection event on all browsers.
188
- this.mutationObserver.flush();
189
-
190
- // If there were mutations then the view will be re-rendered by the mutation observer and the selection
191
- // will be updated, so the selections will equal and the event will not be fired, as expected.
192
- const newViewSelection = this.domConverter.domSelectionToView( domSelection );
193
-
194
- // Do not convert selection change if the new view selection has no ranges in it.
195
- //
196
- // It means that the DOM selection is in some way incorrect. Ranges that were in the DOM selection could not be
197
- // converted to the view. This happens when the DOM selection was moved outside of the editable element.
198
- if ( newViewSelection.rangeCount == 0 ) {
199
- this.view.hasDomSelection = false;
200
-
201
- return;
202
- }
203
-
204
- this.view.hasDomSelection = true;
205
-
206
- if ( this.selection.isEqual( newViewSelection ) && this.domConverter.isDomSelectionCorrect( domSelection ) ) {
207
- return;
208
- }
209
-
210
- // Ensure we are not in the infinite loop (#400).
211
- // This counter is reset each second. 60 selection changes in 1 second is enough high number
212
- // to be very difficult (impossible) to achieve using just keyboard keys (during normal editor use).
213
- if ( ++this._loopbackCounter > 60 ) {
214
- // Selection change observer detected an infinite rendering loop.
215
- // Most probably you try to put the selection in the position which is not allowed
216
- // by the browser and browser fixes it automatically what causes `selectionchange` event on
217
- // which a loopback through a model tries to re-render the wrong selection and again.
218
- //
219
- // @if CK_DEBUG // console.warn( 'Selection change observer detected an infinite rendering loop.' );
220
-
221
- return;
222
- }
223
-
224
- if ( this.selection.isSimilar( newViewSelection ) ) {
225
- // If selection was equal and we are at this point of algorithm, it means that it was incorrect.
226
- // Just re-render it, no need to fire any events, etc.
227
- this.view.forceRender();
228
- } else {
229
- const data = {
230
- oldSelection: this.selection,
231
- newSelection: newViewSelection,
232
- domSelection
233
- };
234
-
235
- // Prepare data for new selection and fire appropriate events.
236
- this.document.fire( 'selectionChange', data );
237
-
238
- // Call `#_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
239
- // This function is debounced what means that `selectionChangeDone` event will be fired only when
240
- // defined int the function time will elapse since the last time the function was called.
241
- // So `selectionChangeDone` will be fired when selection will stop changing.
242
- this._fireSelectionChangeDoneDebounced( data );
243
- }
244
- }
245
-
246
- /**
247
- * Clears `SelectionObserver` internal properties connected with preventing infinite loop.
248
- *
249
- * @protected
250
- */
251
- _clearInfiniteLoop() {
252
- this._loopbackCounter = 0;
253
- }
27
+ constructor(view) {
28
+ super(view);
29
+ /**
30
+ * Instance of the mutation observer. Selection observer calls
31
+ * {@link module:engine/view/observer/mutationobserver~MutationObserver#flush} to ensure that the mutations will be handled
32
+ * before the {@link module:engine/view/document~Document#event:selectionChange} event is fired.
33
+ *
34
+ * @readonly
35
+ * @member {module:engine/view/observer/mutationobserver~MutationObserver}
36
+ * module:engine/view/observer/selectionobserver~SelectionObserver#mutationObserver
37
+ */
38
+ this.mutationObserver = view.getObserver(MutationObserver);
39
+ /**
40
+ * Reference to the view {@link module:engine/view/documentselection~DocumentSelection} object used to compare
41
+ * new selection with it.
42
+ *
43
+ * @readonly
44
+ * @member {module:engine/view/documentselection~DocumentSelection}
45
+ * module:engine/view/observer/selectionobserver~SelectionObserver#selection
46
+ */
47
+ this.selection = this.document.selection;
48
+ /* eslint-disable max-len */
49
+ /**
50
+ * Reference to the {@link module:engine/view/view~View#domConverter}.
51
+ *
52
+ * @readonly
53
+ * @member {module:engine/view/domconverter~DomConverter} module:engine/view/observer/selectionobserver~SelectionObserver#domConverter
54
+ */
55
+ /* eslint-enable max-len */
56
+ this.domConverter = view.domConverter;
57
+ /**
58
+ * A set of documents which have added `selectionchange` listener to avoid adding a listener twice to the same
59
+ * document.
60
+ *
61
+ * @private
62
+ * @member {WeakSet.<Document>} module:engine/view/observer/selectionobserver~SelectionObserver#_documents
63
+ */
64
+ this._documents = new WeakSet();
65
+ /**
66
+ * Fires debounced event `selectionChangeDone`. It uses `lodash#debounce` method to delay function call.
67
+ *
68
+ * @private
69
+ * @param {Object} data Selection change data.
70
+ * @method #_fireSelectionChangeDoneDebounced
71
+ */
72
+ this._fireSelectionChangeDoneDebounced = debounce(data => {
73
+ this.document.fire('selectionChangeDone', data);
74
+ }, 200);
75
+ /**
76
+ * When called, starts clearing the {@link #_loopbackCounter} counter in time intervals. When the number of selection
77
+ * changes exceeds a certain limit within the interval of time, the observer will not fire `selectionChange` but warn about
78
+ * possible infinite selection loop.
79
+ *
80
+ * @private
81
+ * @member {Number} #_clearInfiniteLoopInterval
82
+ */
83
+ this._clearInfiniteLoopInterval = setInterval(() => this._clearInfiniteLoop(), 1000);
84
+ /**
85
+ * Unlocks the `isSelecting` state of the view document in case the selection observer did not record this fact
86
+ * correctly (for whatever reason). It is a safeguard (paranoid check), that returns document to the normal state
87
+ * after a certain period of time (debounced, postponed by each selectionchange event).
88
+ *
89
+ * @private
90
+ * @method #_documentIsSelectingInactivityTimeoutDebounced
91
+ */
92
+ this._documentIsSelectingInactivityTimeoutDebounced = debounce(() => (this.document.isSelecting = false), 5000);
93
+ /**
94
+ * Private property to check if the code does not enter infinite loop.
95
+ *
96
+ * @private
97
+ * @member {Number} module:engine/view/observer/selectionobserver~SelectionObserver#_loopbackCounter
98
+ */
99
+ this._loopbackCounter = 0;
100
+ }
101
+ /**
102
+ * @inheritDoc
103
+ */
104
+ observe(domElement) {
105
+ const domDocument = domElement.ownerDocument;
106
+ const startDocumentIsSelecting = () => {
107
+ this.document.isSelecting = true;
108
+ // Let's activate the safety timeout each time the document enters the "is selecting" state.
109
+ this._documentIsSelectingInactivityTimeoutDebounced();
110
+ };
111
+ const endDocumentIsSelecting = () => {
112
+ if (!this.document.isSelecting) {
113
+ return;
114
+ }
115
+ // Make sure that model selection is up-to-date at the end of selecting process.
116
+ // Sometimes `selectionchange` events could arrive after the `mouseup` event and that selection could be already outdated.
117
+ this._handleSelectionChange(null, domDocument);
118
+ this.document.isSelecting = false;
119
+ // The safety timeout can be canceled when the document leaves the "is selecting" state.
120
+ this._documentIsSelectingInactivityTimeoutDebounced.cancel();
121
+ };
122
+ // The document has the "is selecting" state while the user keeps making (extending) the selection
123
+ // (e.g. by holding the mouse button and moving the cursor). The state resets when they either released
124
+ // the mouse button or interrupted the process by pressing or releasing any key.
125
+ this.listenTo(domElement, 'selectstart', startDocumentIsSelecting, { priority: 'highest' });
126
+ this.listenTo(domElement, 'keydown', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
127
+ this.listenTo(domElement, 'keyup', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
128
+ // Add document-wide listeners only once. This method could be called for multiple editing roots.
129
+ if (this._documents.has(domDocument)) {
130
+ return;
131
+ }
132
+ // This listener is using capture mode to make sure that selection is upcasted before any other
133
+ // handler would like to check it and update (for example table multi cell selection).
134
+ this.listenTo(domDocument, 'mouseup', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
135
+ this.listenTo(domDocument, 'selectionchange', (evt, domEvent) => {
136
+ this._handleSelectionChange(domEvent, domDocument);
137
+ // Defer the safety timeout when the selection changes (e.g. the user keeps extending the selection
138
+ // using their mouse).
139
+ this._documentIsSelectingInactivityTimeoutDebounced();
140
+ });
141
+ this._documents.add(domDocument);
142
+ }
143
+ /**
144
+ * @inheritDoc
145
+ */
146
+ destroy() {
147
+ super.destroy();
148
+ clearInterval(this._clearInfiniteLoopInterval);
149
+ this._fireSelectionChangeDoneDebounced.cancel();
150
+ this._documentIsSelectingInactivityTimeoutDebounced.cancel();
151
+ }
152
+ /**
153
+ * Selection change listener. {@link module:engine/view/observer/mutationobserver~MutationObserver#flush Flush} mutations, check if
154
+ * a selection changes and fires {@link module:engine/view/document~Document#event:selectionChange} event on every change
155
+ * and {@link module:engine/view/document~Document#event:selectionChangeDone} when a selection stop changing.
156
+ *
157
+ * @private
158
+ * @param {Event} domEvent DOM event.
159
+ * @param {Document} domDocument DOM document.
160
+ */
161
+ _handleSelectionChange(domEvent, domDocument) {
162
+ if (!this.isEnabled) {
163
+ return;
164
+ }
165
+ const domSelection = domDocument.defaultView.getSelection();
166
+ if (this.checkShouldIgnoreEventFromTarget(domSelection.anchorNode)) {
167
+ return;
168
+ }
169
+ // Ensure the mutation event will be before selection event on all browsers.
170
+ this.mutationObserver.flush();
171
+ // If there were mutations then the view will be re-rendered by the mutation observer and the selection
172
+ // will be updated, so the selections will equal and the event will not be fired, as expected.
173
+ const newViewSelection = this.domConverter.domSelectionToView(domSelection);
174
+ // Do not convert selection change if the new view selection has no ranges in it.
175
+ //
176
+ // It means that the DOM selection is in some way incorrect. Ranges that were in the DOM selection could not be
177
+ // converted to the view. This happens when the DOM selection was moved outside of the editable element.
178
+ if (newViewSelection.rangeCount == 0) {
179
+ this.view.hasDomSelection = false;
180
+ return;
181
+ }
182
+ this.view.hasDomSelection = true;
183
+ if (this.selection.isEqual(newViewSelection) && this.domConverter.isDomSelectionCorrect(domSelection)) {
184
+ return;
185
+ }
186
+ // Ensure we are not in the infinite loop (#400).
187
+ // This counter is reset each second. 60 selection changes in 1 second is enough high number
188
+ // to be very difficult (impossible) to achieve using just keyboard keys (during normal editor use).
189
+ if (++this._loopbackCounter > 60) {
190
+ // Selection change observer detected an infinite rendering loop.
191
+ // Most probably you try to put the selection in the position which is not allowed
192
+ // by the browser and browser fixes it automatically what causes `selectionchange` event on
193
+ // which a loopback through a model tries to re-render the wrong selection and again.
194
+ //
195
+ // @if CK_DEBUG // console.warn( 'Selection change observer detected an infinite rendering loop.' );
196
+ return;
197
+ }
198
+ if (this.selection.isSimilar(newViewSelection)) {
199
+ // If selection was equal and we are at this point of algorithm, it means that it was incorrect.
200
+ // Just re-render it, no need to fire any events, etc.
201
+ this.view.forceRender();
202
+ }
203
+ else {
204
+ const data = {
205
+ oldSelection: this.selection,
206
+ newSelection: newViewSelection,
207
+ domSelection: domSelection
208
+ };
209
+ // Prepare data for new selection and fire appropriate events.
210
+ this.document.fire('selectionChange', data);
211
+ // Call `#_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
212
+ // This function is debounced what means that `selectionChangeDone` event will be fired only when
213
+ // defined int the function time will elapse since the last time the function was called.
214
+ // So `selectionChangeDone` will be fired when selection will stop changing.
215
+ this._fireSelectionChangeDoneDebounced(data);
216
+ }
217
+ }
218
+ /**
219
+ * Clears `SelectionObserver` internal properties connected with preventing infinite loop.
220
+ *
221
+ * @protected
222
+ */
223
+ _clearInfiniteLoop() {
224
+ this._loopbackCounter = 0;
225
+ }
254
226
  }
255
-
256
- /**
257
- * Fired when a selection has changed. This event is fired only when the selection change was the only change that happened
258
- * in the document, and the old selection is different then the new selection.
259
- *
260
- * Introduced by {@link module:engine/view/observer/selectionobserver~SelectionObserver}.
261
- *
262
- * Note that because {@link module:engine/view/observer/selectionobserver~SelectionObserver} is attached by the
263
- * {@link module:engine/view/view~View} this event is available by default.
264
- *
265
- * @see module:engine/view/observer/selectionobserver~SelectionObserver
266
- * @event module:engine/view/document~Document#event:selectionChange
267
- * @param {Object} data
268
- * @param {module:engine/view/documentselection~DocumentSelection} data.oldSelection Old View selection which is
269
- * {@link module:engine/view/document~Document#selection}.
270
- * @param {module:engine/view/selection~Selection} data.newSelection New View selection which is converted DOM selection.
271
- * @param {Selection} data.domSelection Native DOM selection.
272
- */
273
-
274
227
  /**
275
228
  * Fired when selection stops changing.
276
229
  *
@@ -2,16 +2,12 @@
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 engine/view/observer/tabobserver
8
7
  */
9
-
10
8
  import Observer from './observer';
11
9
  import BubblingEventInfo from './bubblingeventinfo';
12
-
13
10
  import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
14
-
15
11
  /**
16
12
  * Tab observer introduces the {@link module:engine/view/document~Document#event:tab `Document#tab`} event.
17
13
  *
@@ -21,39 +17,30 @@ import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
21
17
  * @extends module:engine/view/observer/observer~Observer
22
18
  */
23
19
  export default class TabObserver extends Observer {
24
- /**
25
- * @inheritDoc
26
- */
27
- constructor( view ) {
28
- super( view );
29
-
30
- const doc = this.document;
31
-
32
- doc.on( 'keydown', ( evt, data ) => {
33
- if (
34
- !this.isEnabled ||
35
- data.keyCode != keyCodes.tab ||
36
- data.ctrlKey
37
- ) {
38
- return;
39
- }
40
-
41
- const event = new BubblingEventInfo( doc, 'tab', doc.selection.getFirstRange() );
42
-
43
- doc.fire( event, data );
44
-
45
- if ( event.stop.called ) {
46
- evt.stop();
47
- }
48
- } );
49
- }
50
-
51
- /**
52
- * @inheritDoc
53
- */
54
- observe() {}
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ constructor(view) {
24
+ super(view);
25
+ const doc = this.document;
26
+ doc.on('keydown', (evt, data) => {
27
+ if (!this.isEnabled ||
28
+ data.keyCode != keyCodes.tab ||
29
+ data.ctrlKey) {
30
+ return;
31
+ }
32
+ const event = new BubblingEventInfo(doc, 'tab', doc.selection.getFirstRange());
33
+ doc.fire(event, data);
34
+ if (event.stop.called) {
35
+ evt.stop();
36
+ }
37
+ });
38
+ }
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ observe() { }
55
43
  }
56
-
57
44
  /**
58
45
  * Event fired when the user presses a tab key.
59
46
  *