8
0

selectionobserver.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, listenter;
  19. before( () => {
  20. listenter = Object.create( EmitterMixin );
  21. viewDocument = new ViewDocument();
  22. viewDocument.createRoot( document.getElementById( 'main' ) );
  23. mutationObserver = viewDocument.getObserver( MutationObserver );
  24. selectionObserver = viewDocument.getObserver( SelectionObserver );
  25. viewRoot = viewDocument.getRoot();
  26. viewRoot.appendChildren( parse(
  27. '<container:p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</container:p>' +
  28. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  29. viewDocument.render();
  30. } );
  31. beforeEach( ( done ) => {
  32. viewDocument.selection.removeAllRanges();
  33. document.getSelection().removeAllRanges();
  34. viewDocument.isFocused = true;
  35. viewDocument.getObserver( SelectionObserver ).enable();
  36. // Ensure selectionchange will not be fired.
  37. setTimeout( () => done(), 100 );
  38. } );
  39. afterEach( () => {
  40. listenter.stopListening();
  41. } );
  42. it( 'should fire selectionChange when it is the only change', ( done ) => {
  43. listenter.listenTo( viewDocument, 'selectionChange', ( evt, data ) => {
  44. expect( data ).to.have.property( 'domSelection' ).that.equals( document.getSelection() );
  45. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  46. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  47. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  48. expect( data.newSelection.rangeCount ).to.equal( 1 );
  49. const newViewRange = data.newSelection.getFirstRange();
  50. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  51. expect( newViewRange.start.parent ).to.equal( viewFoo );
  52. expect( newViewRange.start.offset ).to.equal( 1 );
  53. expect( newViewRange.end.parent ).to.equal( viewFoo );
  54. expect( newViewRange.end.offset ).to.equal( 2 );
  55. done();
  56. } );
  57. changeDomSelection();
  58. } );
  59. it( 'should add only one listener to one document', ( done ) => {
  60. // Add second roots to ensure that listener is added once.
  61. viewDocument.createRoot( document.getElementById( 'additional' ), 'additional' );
  62. listenter.listenTo( viewDocument, 'selectionChange', () => {
  63. done();
  64. } );
  65. changeDomSelection();
  66. } );
  67. it( 'should not fire selectionChange on render', ( done ) => {
  68. listenter.listenTo( viewDocument, 'selectionChange', () => {
  69. throw 'selectionChange on render';
  70. } );
  71. setTimeout( () => done(), 70 );
  72. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  73. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  74. viewDocument.render();
  75. } );
  76. it( 'should not fired if observer is disabled', ( done ) => {
  77. viewDocument.getObserver( SelectionObserver ).disable();
  78. listenter.listenTo( viewDocument, 'selectionChange', () => {
  79. throw 'selectionChange on render';
  80. } );
  81. setTimeout( () => done(), 70 );
  82. changeDomSelection();
  83. } );
  84. it( 'should not fired if there is no focus', ( done ) => {
  85. viewDocument.isFocused = false;
  86. listenter.listenTo( viewDocument, 'selectionChange', () => {
  87. throw 'selectionChange on render';
  88. } );
  89. setTimeout( () => done(), 70 );
  90. changeDomSelection();
  91. } );
  92. it( 'should warn and not enter infinite loop', ( done ) => {
  93. // Reset infinite loop counters so other tests won't mess up with this test.
  94. selectionObserver._clearInfiniteLoop();
  95. let counter = 100;
  96. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  97. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  98. listenter.listenTo( viewDocument, 'selectionChange', () => {
  99. counter--;
  100. if ( counter > 0 ) {
  101. setTimeout( changeDomSelection );
  102. } else {
  103. throw( 'Infinite loop!' );
  104. }
  105. } );
  106. let warnedOnce = false;
  107. testUtils.sinon.stub( log, 'warn', ( msg ) => {
  108. if ( !warnedOnce ) {
  109. warnedOnce = true;
  110. setTimeout( () => {
  111. expect( msg ).to.match( /^selectionchange-infinite-loop/ );
  112. done();
  113. }, 200 );
  114. }
  115. } );
  116. changeDomSelection();
  117. } );
  118. it( 'should not be treated as an infinite loop if selection is changed only few times', ( done ) => {
  119. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  120. // Reset infinite loop counters so other tests won't mess up with this test.
  121. selectionObserver._clearInfiniteLoop();
  122. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  123. const spy = testUtils.sinon.spy( log, 'warn' );
  124. for ( let i = 0; i < 10; i++ ) {
  125. changeDomSelection();
  126. }
  127. setTimeout( () => {
  128. expect( spy.called ).to.be.false;
  129. done();
  130. }, 400 );
  131. } );
  132. } );
  133. function changeDomSelection() {
  134. const domSelection = document.getSelection();
  135. domSelection.removeAllRanges();
  136. const domFoo = document.getElementById( 'main' ).childNodes[ 0 ].childNodes[ 0 ];
  137. const domRange = new Range();
  138. domRange.setStart( domFoo, 1 );
  139. domRange.setEnd( domFoo, 2 );
  140. domSelection.addRange( domRange );
  141. }