33 * For licensing, see LICENSE.md.
44 */
55
6- /* globals setTimeout, document */
6+ /* globals setTimeout, setInterval, clearInterval, document */
77
88import ViewRange from '../../../src/view/range' ;
99import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils' ;
1010import ViewSelection from '../../../src/view/selection' ;
1111import ViewDocument from '../../../src/view/document' ;
1212import SelectionObserver from '../../../src/view/observer/selectionobserver' ;
1313import MutationObserver from '../../../src/view/observer/mutationobserver' ;
14-
14+ import FocusObserver from '../../../src/view/observer/focusobserver' ;
1515import log from '@ckeditor/ckeditor5-utils/src/log' ;
16-
1716import { parse } from '../../../src/dev-utils/view' ;
1817
1918testUtils . createSinonSandbox ( ) ;
2019
2120describe ( 'SelectionObserver' , ( ) => {
22- let viewDocument , viewRoot , mutationObserver , selectionObserver , domRoot ;
21+ let viewDocument , viewRoot , mutationObserver , selectionObserver , domRoot , domMain , domDocument ;
2322
2423 beforeEach ( ( done ) => {
25- domRoot = document . createElement ( 'div' ) ;
26- domRoot . innerHTML = `<div contenteditable="true" id="main"></div><div contenteditable="true" id="additional"></div>` ;
27- document . body . appendChild ( domRoot ) ;
24+ domDocument = document ;
25+ domRoot = domDocument . createElement ( 'div' ) ;
26+ domRoot . innerHTML = `<div contenteditable="true"></div><div contenteditable="true" id="additional"></div>` ;
27+ domMain = domRoot . childNodes [ 0 ] ;
28+ domDocument . body . appendChild ( domRoot ) ;
2829
2930 viewDocument = new ViewDocument ( ) ;
30- viewDocument . createRoot ( document . getElementById ( 'main' ) ) ;
31+ viewDocument . createRoot ( domMain ) ;
3132
3233 mutationObserver = viewDocument . getObserver ( MutationObserver ) ;
3334 selectionObserver = viewDocument . getObserver ( SelectionObserver ) ;
@@ -41,7 +42,7 @@ describe( 'SelectionObserver', () => {
4142 viewDocument . render ( ) ;
4243
4344 viewDocument . selection . removeAllRanges ( ) ;
44- document . getSelection ( ) . removeAllRanges ( ) ;
45+ domDocument . getSelection ( ) . removeAllRanges ( ) ;
4546
4647 viewDocument . isFocused = true ;
4748
@@ -59,7 +60,7 @@ describe( 'SelectionObserver', () => {
5960
6061 it ( 'should fire selectionChange when it is the only change' , ( done ) => {
6162 viewDocument . on ( 'selectionChange' , ( evt , data ) => {
62- expect ( data ) . to . have . property ( 'domSelection' ) . that . equals ( document . getSelection ( ) ) ;
63+ expect ( data ) . to . have . property ( 'domSelection' ) . that . equals ( domDocument . getSelection ( ) ) ;
6364
6465 expect ( data ) . to . have . property ( 'oldSelection' ) . that . is . instanceof ( ViewSelection ) ;
6566 expect ( data . oldSelection . rangeCount ) . to . equal ( 0 ) ;
@@ -83,7 +84,7 @@ describe( 'SelectionObserver', () => {
8384
8485 it ( 'should add only one listener to one document' , ( done ) => {
8586 // Add second roots to ensure that listener is added once.
86- viewDocument . createRoot ( document . getElementById ( 'additional' ) , 'additional' ) ;
87+ viewDocument . createRoot ( domDocument . getElementById ( 'additional' ) , 'additional' ) ;
8788
8889 viewDocument . on ( 'selectionChange' , ( ) => {
8990 done ( ) ;
@@ -138,6 +139,8 @@ describe( 'SelectionObserver', () => {
138139 it ( 'should warn and not enter infinite loop' , ( done ) => {
139140 // Reset infinite loop counters so other tests won't mess up with this test.
140141 selectionObserver . _clearInfiniteLoop ( ) ;
142+ clearInterval ( selectionObserver . _clearInfiniteLoopInterval ) ;
143+ selectionObserver . _clearInfiniteLoopInterval = setInterval ( ( ) => selectionObserver . _clearInfiniteLoop ( ) , 2000 ) ;
141144
142145 let counter = 100 ;
143146
@@ -232,6 +235,9 @@ describe( 'SelectionObserver', () => {
232235
233236 viewDocument . on ( 'selectionChangeDone' , spy ) ;
234237
238+ // Disable focus observer to not re-render view on each focus.
239+ viewDocument . getObserver ( FocusObserver ) . disable ( ) ;
240+
235241 // Change selection.
236242 changeDomSelection ( ) ;
237243
@@ -250,7 +256,7 @@ describe( 'SelectionObserver', () => {
250256 const data = spy . firstCall . args [ 1 ] ;
251257
252258 expect ( spy . calledOnce ) . to . true ;
253- expect ( data ) . to . have . property ( 'domSelection' ) . to . equal ( document . getSelection ( ) ) ;
259+ expect ( data ) . to . have . property ( 'domSelection' ) . to . equal ( domDocument . getSelection ( ) ) ;
254260
255261 expect ( data ) . to . have . property ( 'oldSelection' ) . to . instanceof ( ViewSelection ) ;
256262 expect ( data . oldSelection . rangeCount ) . to . equal ( 0 ) ;
@@ -295,13 +301,13 @@ describe( 'SelectionObserver', () => {
295301 } , 110 ) ;
296302 } , 100 ) ;
297303 } ) ;
298- } ) ;
299304
300- function changeDomSelection ( ) {
301- const domSelection = document . getSelection ( ) ;
302- const domFoo = document . getElementById ( 'main' ) . childNodes [ 0 ] . childNodes [ 0 ] ;
303- const offset = domSelection . anchorOffset ;
305+ function changeDomSelection ( ) {
306+ const domSelection = domDocument . getSelection ( ) ;
307+ const domFoo = domMain . childNodes [ 0 ] . childNodes [ 0 ] ;
308+ const offset = domSelection . anchorOffset ;
304309
305- domSelection . removeAllRanges ( ) ;
306- domSelection . collapse ( domFoo , offset == 2 ? 3 : 2 ) ;
307- }
310+ domSelection . removeAllRanges ( ) ;
311+ domSelection . collapse ( domFoo , offset == 2 ? 3 : 2 ) ;
312+ }
313+ } ) ;
0 commit comments