selectionobserver.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import ViewRange from '/ckeditor5/engine/treeview/range.js';
  8. import ViewSelection from '/ckeditor5/engine/treeview/selection.js';
  9. import TreeView from '/ckeditor5/engine/treeview/treeview.js';
  10. import SelectionObserver from '/ckeditor5/engine/treeview/observer/selectionobserver.js';
  11. import MutationObserver from '/ckeditor5/engine/treeview/observer/mutationobserver.js';
  12. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  13. import { parse } from '/tests/engine/_utils/view.js';
  14. describe( 'SelectionObserver', () => {
  15. let treeView, viewRoot, mutationObserver, selectionObserver, listenter;
  16. before( () => {
  17. listenter = Object.create( EmitterMixin );
  18. treeView = new TreeView();
  19. treeView.createRoot( document.getElementById( 'main' ) );
  20. mutationObserver = treeView.addObserver( MutationObserver );
  21. selectionObserver = treeView.addObserver( SelectionObserver );
  22. viewRoot = treeView.getRoot();
  23. viewRoot.appendChildren( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  24. treeView.render();
  25. } );
  26. beforeEach( ( done ) => {
  27. treeView.selection.removeAllRanges();
  28. document.getSelection().removeAllRanges();
  29. treeView.getObserver( SelectionObserver ).enable();
  30. // Ensure selectionchange will not be fired.
  31. setTimeout( () => done(), 100 );
  32. } );
  33. afterEach( () => {
  34. listenter.stopListening();
  35. } );
  36. it( 'should fire selectionChange when it is the only change', ( done ) => {
  37. listenter.listenTo( treeView, 'selectionChange', ( evt, data ) => {
  38. expect( data ).to.have.property( 'domSelection' ).that.equals( document.getSelection() );
  39. expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
  40. expect( data.oldSelection.rangeCount ).to.equal( 0 );
  41. expect( data ).to.have.property( 'newSelection' ).that.is.instanceof( ViewSelection );
  42. expect( data.newSelection.rangeCount ).to.equal( 1 );
  43. const newViewRange = data.newSelection.getFirstRange();
  44. const viewFoo = treeView.getRoot().getChild( 0 ).getChild( 0 );
  45. expect( newViewRange.start.parent ).to.equal( viewFoo );
  46. expect( newViewRange.start.offset ).to.equal( 1 );
  47. expect( newViewRange.end.parent ).to.equal( viewFoo );
  48. expect( newViewRange.end.offset ).to.equal( 2 );
  49. done();
  50. } );
  51. changeDomSelection();
  52. } );
  53. it( 'should add only one listener to one document', ( done ) => {
  54. // Add second roots to ensure that listener is added once.
  55. treeView.createRoot( document.getElementById( 'additional' ), 'additional' );
  56. listenter.listenTo( treeView, 'selectionChange', () => {
  57. done();
  58. } );
  59. changeDomSelection();
  60. } );
  61. it( 'should not fire selectionChange on render', ( done ) => {
  62. listenter.listenTo( treeView, 'selectionChange', () => {
  63. throw 'selectionChange on render';
  64. } );
  65. setTimeout( () => done(), 70 );
  66. const viewBar = treeView.getRoot().getChild( 1 ).getChild( 0 );
  67. treeView.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 1, viewBar, 2 ) );
  68. treeView.render();
  69. } );
  70. it( 'should not fired if observer is disabled', ( done ) => {
  71. treeView.getObserver( SelectionObserver ).disable();
  72. listenter.listenTo( treeView, 'selectionChange', () => {
  73. throw 'selectionChange on render';
  74. } );
  75. setTimeout( () => done(), 70 );
  76. changeDomSelection();
  77. } );
  78. it( 'should call render after selection change which reset selection if it was not changed', ( done ) => {
  79. const viewBar = treeView.getRoot().getChild( 1 ).getChild( 0 );
  80. treeView.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 0, viewBar, 1 ) );
  81. listenter.listenTo( treeView, 'selectionChange', () => {
  82. setTimeout( () => {
  83. const domSelection = document.getSelection();
  84. expect( domSelection.rangeCount ).to.equal( 1 );
  85. const domRange = domSelection.getRangeAt( 0 );
  86. const domBar = document.getElementById( 'main' ).childNodes[ 1 ].childNodes[ 0 ];
  87. expect( domRange.startContainer ).to.equal( domBar );
  88. expect( domRange.startOffset ).to.equal( 0 );
  89. expect( domRange.endContainer ).to.equal( domBar );
  90. expect( domRange.endOffset ).to.equal( 1 );
  91. done();
  92. } );
  93. } );
  94. changeDomSelection();
  95. } );
  96. } );
  97. function changeDomSelection() {
  98. const domSelection = document.getSelection();
  99. domSelection.removeAllRanges();
  100. const domFoo = document.getElementById( 'main' ).childNodes[ 0 ].childNodes[ 0 ];
  101. const domRange = new Range();
  102. domRange.setStart( domFoo, 1 );
  103. domRange.setEnd( domFoo, 2 );
  104. domSelection.addRange( domRange );
  105. }