selectionobserver.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals setTimeout, Range, document */
  6. /* bender-tags: view, browser-only */
  7. import ViewRange from 'ckeditor5/engine/view/range.js';
  8. import testUtils from 'tests/core/_utils/utils.js';
  9. import ViewSelection from 'ckeditor5/engine/view/selection.js';
  10. import ViewDocument from 'ckeditor5/engine/view/document.js';
  11. import SelectionObserver from 'ckeditor5/engine/view/observer/selectionobserver.js';
  12. import MutationObserver from 'ckeditor5/engine/view/observer/mutationobserver.js';
  13. import EmitterMixin from 'ckeditor5/utils/emittermixin.js';
  14. import log from 'ckeditor5/utils/log.js';
  15. import { parse } from 'ckeditor5/engine/dev-utils/view.js';
  16. testUtils.createSinonSandbox();
  17. describe( 'SelectionObserver', () => {
  18. let viewDocument, viewRoot, mutationObserver, selectionObserver, listener, domRoot;
  19. beforeEach( ( done ) => {
  20. domRoot = document.createElement( 'div' );
  21. domRoot.innerHTML = `<div contenteditable="true" id="main"></div><div contenteditable="true" id="additional"></div>`;
  22. document.body.appendChild( domRoot );
  23. listener = Object.create( EmitterMixin );
  24. viewDocument = new ViewDocument();
  25. viewDocument.createRoot( document.getElementById( 'main' ) );
  26. mutationObserver = viewDocument.getObserver( MutationObserver );
  27. selectionObserver = viewDocument.getObserver( SelectionObserver );
  28. viewRoot = viewDocument.getRoot();
  29. viewRoot.appendChildren( parse(
  30. '<container:p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</container:p>' +
  31. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  32. viewDocument.render();
  33. viewDocument.selection.removeAllRanges();
  34. document.getSelection().removeAllRanges();
  35. viewDocument.isFocused = true;
  36. selectionObserver.enable();
  37. // Ensure selectionchange will not be fired.
  38. setTimeout( () => done(), 100 );
  39. } );
  40. afterEach( () => {
  41. domRoot.parentElement.removeChild( domRoot );
  42. listener.stopListening();
  43. selectionObserver.disable();
  44. } );
  45. it( 'should fire selectionChange when it is the only change', ( done ) => {
  46. listener.listenTo( viewDocument, 'selectionChange', ( evt, data ) => {
  47. expect( data ).to.have.property( 'domSelection' ).that.equals( document.getSelection() );
  48. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  49. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  50. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  51. expect( data.newSelection.rangeCount ).to.equal( 1 );
  52. const newViewRange = data.newSelection.getFirstRange();
  53. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  54. expect( newViewRange.start.parent ).to.equal( viewFoo );
  55. expect( newViewRange.start.offset ).to.equal( 1 );
  56. expect( newViewRange.end.parent ).to.equal( viewFoo );
  57. expect( newViewRange.end.offset ).to.equal( 2 );
  58. done();
  59. } );
  60. changeDomSelection();
  61. } );
  62. it( 'should add only one listener to one document', ( done ) => {
  63. // Add second roots to ensure that listener is added once.
  64. viewDocument.createRoot( document.getElementById( 'additional' ), 'additional' );
  65. listener.listenTo( viewDocument, 'selectionChange', () => {
  66. done();
  67. } );
  68. changeDomSelection();
  69. } );
  70. it( 'should not fire selectionChange on render', ( done ) => {
  71. listener.listenTo( viewDocument, 'selectionChange', () => {
  72. throw 'selectionChange on render';
  73. } );
  74. setTimeout( done, 70 );
  75. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  76. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  77. viewDocument.render();
  78. } );
  79. it( 'should not fired if observer is disabled', ( done ) => {
  80. viewDocument.getObserver( SelectionObserver ).disable();
  81. listener.listenTo( viewDocument, 'selectionChange', () => {
  82. throw 'selectionChange on render';
  83. } );
  84. setTimeout( done, 70 );
  85. changeDomSelection();
  86. } );
  87. it( 'should not fired if there is no focus', ( done ) => {
  88. viewDocument.isFocused = false;
  89. listener.listenTo( viewDocument, 'selectionChange', () => {
  90. throw 'selectionChange on render';
  91. } );
  92. setTimeout( done, 70 );
  93. changeDomSelection();
  94. } );
  95. it( 'should warn and not enter infinite loop', ( done ) => {
  96. // Reset infinite loop counters so other tests won't mess up with this test.
  97. selectionObserver._clearInfiniteLoop();
  98. let counter = 100;
  99. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  100. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  101. listener.listenTo( viewDocument, 'selectionChange', () => {
  102. counter--;
  103. if ( counter > 0 ) {
  104. setTimeout( changeDomSelection );
  105. } else {
  106. throw 'Infinite loop!';
  107. }
  108. } );
  109. let warnedOnce = false;
  110. testUtils.sinon.stub( log, 'warn', ( msg ) => {
  111. if ( !warnedOnce ) {
  112. warnedOnce = true;
  113. setTimeout( () => {
  114. expect( msg ).to.match( /^selectionchange-infinite-loop/ );
  115. done();
  116. }, 200 );
  117. }
  118. } );
  119. changeDomSelection();
  120. } );
  121. it( 'should not be treated as an infinite loop if selection is changed only few times', ( done ) => {
  122. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  123. // Reset infinite loop counters so other tests won't mess up with this test.
  124. selectionObserver._clearInfiniteLoop();
  125. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  126. const spy = testUtils.sinon.spy( log, 'warn' );
  127. for ( let i = 0; i < 10; i++ ) {
  128. changeDomSelection();
  129. }
  130. setTimeout( () => {
  131. expect( spy.called ).to.be.false;
  132. done();
  133. }, 400 );
  134. } );
  135. } );
  136. function changeDomSelection() {
  137. const domSelection = document.getSelection();
  138. domSelection.removeAllRanges();
  139. const domFoo = document.getElementById( 'main' ).childNodes[ 0 ].childNodes[ 0 ];
  140. const domRange = new Range();
  141. domRange.setStart( domFoo, 1 );
  142. domRange.setEnd( domFoo, 2 );
  143. domSelection.addRange( domRange );
  144. }