selectionobserver.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, document, console */
  6. import ViewRange from '../../../src/view/range';
  7. import DocumentSelection from '../../../src/view/documentselection';
  8. import ViewSelection from '../../../src/view/selection';
  9. import View from '../../../src/view/view';
  10. import SelectionObserver from '../../../src/view/observer/selectionobserver';
  11. import FocusObserver from '../../../src/view/observer/focusobserver';
  12. import createViewRoot from '../_utils/createroot';
  13. import { parse } from '../../../src/dev-utils/view';
  14. describe( 'SelectionObserver', () => {
  15. let view, viewDocument, viewRoot, selectionObserver, domRoot, domMain, domDocument;
  16. beforeEach( done => {
  17. domDocument = document;
  18. domRoot = domDocument.createElement( 'div' );
  19. domRoot.innerHTML = '<div contenteditable="true"></div><div contenteditable="true" id="additional"></div>';
  20. domMain = domRoot.childNodes[ 0 ];
  21. domDocument.body.appendChild( domRoot );
  22. view = new View();
  23. viewDocument = view.document;
  24. createViewRoot( viewDocument );
  25. view.attachDomRoot( domMain );
  26. selectionObserver = view.getObserver( SelectionObserver );
  27. viewRoot = viewDocument.getRoot();
  28. view.change( writer => {
  29. viewRoot._appendChild( parse(
  30. '<container:p>xxx<ui:span></ui:span></container:p>' +
  31. '<container:p>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</container:p>' ) );
  32. writer.setSelection( null );
  33. domDocument.getSelection().removeAllRanges();
  34. viewDocument.isFocused = true;
  35. domMain.focus();
  36. } );
  37. selectionObserver.enable();
  38. // Ensure selectionchange will not be fired.
  39. setTimeout( () => done(), 100 );
  40. } );
  41. afterEach( () => {
  42. sinon.restore();
  43. domRoot.parentElement.removeChild( domRoot );
  44. view.destroy();
  45. } );
  46. it( 'should fire selectionChange when it is the only change', done => {
  47. viewDocument.on( 'selectionChange', ( evt, data ) => {
  48. expect( data ).to.have.property( 'domSelection' ).that.equals( domDocument.getSelection() );
  49. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( DocumentSelection );
  50. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  51. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  52. expect( data.newSelection.rangeCount ).to.equal( 1 );
  53. const newViewRange = data.newSelection.getFirstRange();
  54. const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  55. expect( newViewRange.start.parent ).to.equal( viewFoo );
  56. expect( newViewRange.start.offset ).to.equal( 2 );
  57. expect( newViewRange.end.parent ).to.equal( viewFoo );
  58. expect( newViewRange.end.offset ).to.equal( 2 );
  59. done();
  60. } );
  61. changeDomSelection();
  62. } );
  63. it( 'should add only one listener to one document', done => {
  64. // Add second roots to ensure that listener is added once.
  65. createViewRoot( viewDocument, 'div', 'additional' );
  66. view.attachDomRoot( domDocument.getElementById( 'additional' ), 'additional' );
  67. viewDocument.on( 'selectionChange', () => {
  68. done();
  69. } );
  70. changeDomSelection();
  71. } );
  72. it( 'should not fire selectionChange on render', done => {
  73. viewDocument.on( 'selectionChange', () => {
  74. throw 'selectionChange on render';
  75. } );
  76. setTimeout( done, 70 );
  77. const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  78. view.change( writer => {
  79. writer.setSelection( ViewRange._createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  80. } );
  81. } );
  82. it( 'should not fired if observer is disabled', done => {
  83. view.getObserver( SelectionObserver ).disable();
  84. viewDocument.on( 'selectionChange', () => {
  85. throw 'selectionChange on render';
  86. } );
  87. setTimeout( done, 70 );
  88. changeDomSelection();
  89. } );
  90. it( 'should not fired if there is no focus', done => {
  91. viewDocument.isFocused = false;
  92. // changeDomSelection() may focus the editable element (happens on Chrome)
  93. // so cancel this because it sets the isFocused flag.
  94. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  95. viewDocument.on( 'selectionChange', () => {
  96. // Validate the correctness of the test. May help tracking issue with this test.
  97. expect( viewDocument.isFocused ).to.be.false;
  98. throw 'selectionChange on render';
  99. } );
  100. setTimeout( done, 70 );
  101. changeDomSelection();
  102. } );
  103. it( 'should fired if there is no focus but document is read-only', done => {
  104. const spy = sinon.spy();
  105. viewDocument.isFocused = false;
  106. viewDocument.isReadOnly = true;
  107. // changeDomSelection() may focus the editable element (happens on Chrome)
  108. // so cancel this because it sets the isFocused flag.
  109. viewDocument.on( 'focus', evt => evt.stop(), { priority: 'highest' } );
  110. viewDocument.on( 'selectionChange', spy );
  111. setTimeout( () => {
  112. sinon.assert.called( spy );
  113. done();
  114. }, 70 );
  115. changeDomSelection();
  116. } );
  117. it( 'should not enter infinite loop', () => {
  118. let counter = 70;
  119. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  120. view.change( writer => {
  121. writer.setSelection( viewFoo, 0 );
  122. } );
  123. const selectionChangeSpy = sinon.spy();
  124. const consoleWarnStub = sinon.stub( console, 'warn' );
  125. viewDocument.on( 'selectionChange', selectionChangeSpy );
  126. return new Promise( resolve => {
  127. viewDocument.on( 'selectionChangeDone', () => {
  128. expect( selectionChangeSpy.callCount ).to.equal( 60 );
  129. sinon.assert.called( consoleWarnStub );
  130. sinon.assert.calledWith(
  131. consoleWarnStub,
  132. 'Selection change observer detected an infinite rendering loop.'
  133. );
  134. resolve();
  135. } );
  136. while ( counter > 0 ) {
  137. changeDomSelection();
  138. counter--;
  139. }
  140. } );
  141. } );
  142. it( 'should not be treated as an infinite loop if selection is changed only few times', done => {
  143. const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
  144. viewDocument.selection._setTo( ViewRange._createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
  145. const consoleWarnSpy = sinon.spy( console, 'warn' );
  146. viewDocument.on( 'selectionChangeDone', () => {
  147. expect( consoleWarnSpy.called ).to.be.false;
  148. done();
  149. } );
  150. for ( let i = 0; i < 10; i++ ) {
  151. changeDomSelection();
  152. }
  153. } );
  154. it( 'should not be treated as an infinite loop if changes are not often', () => {
  155. const clock = sinon.useFakeTimers( {
  156. toFake: [ 'setInterval', 'clearInterval' ]
  157. } );
  158. const consoleWarnStub = sinon.stub( console, 'warn' );
  159. // We need to recreate SelectionObserver, so it will use mocked setInterval.
  160. selectionObserver.disable();
  161. selectionObserver.destroy();
  162. view._observers.delete( SelectionObserver );
  163. view.addObserver( SelectionObserver );
  164. return doChanges()
  165. .then( doChanges )
  166. .then( () => {
  167. sinon.assert.notCalled( consoleWarnStub );
  168. clock.restore();
  169. } );
  170. function doChanges() {
  171. return new Promise( resolve => {
  172. viewDocument.once( 'selectionChangeDone', () => {
  173. clock.tick( 1100 );
  174. resolve();
  175. } );
  176. for ( let i = 0; i < 50; i++ ) {
  177. changeDomSelection();
  178. }
  179. } );
  180. }
  181. } );
  182. it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
  183. const spy = sinon.spy();
  184. viewDocument.on( 'selectionChangeDone', spy );
  185. // Disable focus observer to not re-render view on each focus.
  186. view.getObserver( FocusObserver ).disable();
  187. // Change selection.
  188. changeDomSelection();
  189. // Wait 100ms.
  190. setTimeout( () => {
  191. // Check if spy was called.
  192. expect( spy.notCalled ).to.true;
  193. // Change selection one more time.
  194. changeDomSelection();
  195. // Wait 210ms (debounced function should be called).
  196. setTimeout( () => {
  197. const data = spy.firstCall.args[ 1 ];
  198. expect( spy.calledOnce ).to.true;
  199. expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
  200. expect( data ).to.have.property( 'oldSelection' ).to.instanceof( DocumentSelection );
  201. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  202. expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
  203. expect( data.newSelection.rangeCount ).to.equal( 1 );
  204. const newViewRange = data.newSelection.getFirstRange();
  205. const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
  206. expect( newViewRange.start.parent ).to.equal( viewFoo );
  207. expect( newViewRange.start.offset ).to.equal( 3 );
  208. expect( newViewRange.end.parent ).to.equal( viewFoo );
  209. expect( newViewRange.end.offset ).to.equal( 3 );
  210. done();
  211. }, 210 );
  212. }, 100 );
  213. } );
  214. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
  215. const spy = sinon.spy();
  216. viewDocument.on( 'selectionChangeDone', spy );
  217. // Change selection.
  218. changeDomSelection();
  219. // Wait 100ms.
  220. setTimeout( () => {
  221. // And destroy observer.
  222. selectionObserver.destroy();
  223. // Wait another 110ms.
  224. setTimeout( () => {
  225. // Check that event won't be called.
  226. expect( spy.notCalled ).to.true;
  227. done();
  228. }, 110 );
  229. }, 100 );
  230. } );
  231. it( 'should re-render view if selections are similar if DOM selection is in incorrect place', done => {
  232. const sel = domDocument.getSelection();
  233. const domParagraph = domMain.childNodes[ 0 ];
  234. const domText = domParagraph.childNodes[ 0 ];
  235. const domUI = domParagraph.childNodes[ 1 ];
  236. const viewRenderSpy = sinon.spy();
  237. // Add rendering on selectionChange event to check this feature.
  238. viewDocument.on( 'selectionChange', () => {
  239. // Manually set selection because no handlers are set for selectionChange event in this test.
  240. // Normally this is handled by view -> model -> view selection converters chain.
  241. const viewAnchor = view.domConverter.domPositionToView( sel.anchorNode, sel.anchorOffset );
  242. const viewFocus = view.domConverter.domPositionToView( sel.focusNode, sel.focusOffset );
  243. view.change( writer => {
  244. writer.setSelection( viewAnchor );
  245. writer.setSelectionFocus( viewFocus );
  246. } );
  247. } );
  248. viewDocument.once( 'selectionChange', () => {
  249. // 2. Selection change has been handled.
  250. selectionObserver.listenTo( domDocument, 'selectionchange', () => {
  251. // 4. Check if view was re-rendered.
  252. sinon.assert.calledOnce( viewRenderSpy );
  253. done();
  254. }, { priority: 'lowest' } );
  255. // 3. Now, collapse selection in similar position, but in UI element.
  256. // Current and new selection position are similar in view (but not equal!).
  257. // Also add a spy to `viewDocument#render` to see if view will be re-rendered.
  258. sel.collapse( domUI, 0 );
  259. view.on( 'render', viewRenderSpy );
  260. // Some browsers like Safari won't allow to put selection inside empty ui element.
  261. // In that situation selection should stay in correct place.
  262. if ( sel.anchorNode !== domUI ) {
  263. expect( sel.anchorNode ).to.equal( domText );
  264. expect( sel.anchorOffset ).to.equal( 3 );
  265. expect( sel.isCollapsed ).to.be.true;
  266. done();
  267. }
  268. }, { priority: 'lowest' } );
  269. // 1. Collapse in a text node, before ui element, and wait for async selectionchange to fire selection change handling.
  270. sel.collapse( domText, 3 );
  271. } );
  272. function changeDomSelection() {
  273. const domSelection = domDocument.getSelection();
  274. const domFoo = domMain.childNodes[ 1 ].childNodes[ 0 ];
  275. const offset = domSelection.anchorOffset;
  276. domSelection.collapse( domFoo, offset == 2 ? 3 : 2 );
  277. }
  278. } );