textwatcher.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TextWatcher from '../src/textwatcher';
  9. describe( 'TextWatcher', () => {
  10. let editor, model, doc;
  11. let watcher, matchedDataSpy, matchedSelectionSpy, unmatchedSpy, testCallbackStub;
  12. testUtils.createSinonSandbox();
  13. beforeEach( () => {
  14. return ModelTestEditor.create()
  15. .then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. doc = model.document;
  19. testCallbackStub = sinon.stub();
  20. matchedDataSpy = sinon.spy();
  21. matchedSelectionSpy = sinon.spy();
  22. unmatchedSpy = sinon.spy();
  23. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  24. setData( model, '<paragraph>foo []</paragraph>' );
  25. watcher = new TextWatcher( model, testCallbackStub, () => {} );
  26. watcher.on( 'matched:data', matchedDataSpy );
  27. watcher.on( 'matched:selection', matchedSelectionSpy );
  28. watcher.on( 'unmatched', unmatchedSpy );
  29. } );
  30. } );
  31. afterEach( () => {
  32. sinon.restore();
  33. if ( editor ) {
  34. return editor.destroy();
  35. }
  36. } );
  37. describe( 'testCallback', () => {
  38. it( 'should evaluate text before caret for data changes', () => {
  39. model.change( writer => {
  40. writer.insertText( '@', doc.selection.getFirstPosition() );
  41. } );
  42. sinon.assert.calledOnce( testCallbackStub );
  43. sinon.assert.calledWithExactly( testCallbackStub, 'foo @' );
  44. } );
  45. it( 'should not evaluate text for not collapsed selection', () => {
  46. model.change( writer => {
  47. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  48. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  49. } );
  50. sinon.assert.notCalled( testCallbackStub );
  51. } );
  52. it( 'should evaluate text for selection changes', () => {
  53. model.change( writer => {
  54. writer.setSelection( doc.getRoot().getChild( 0 ), 1 );
  55. } );
  56. sinon.assert.calledOnce( testCallbackStub );
  57. sinon.assert.calledWithExactly( testCallbackStub, 'f' );
  58. } );
  59. it( 'should evaluate text before caret up to <softBreak>', () => {
  60. model.schema.register( 'softBreak', {
  61. allowWhere: '$text',
  62. isInline: true
  63. } );
  64. model.change( writer => {
  65. writer.insertElement( 'softBreak', doc.selection.getFirstPosition() );
  66. writer.insertText( '@', doc.selection.getFirstPosition() );
  67. } );
  68. sinon.assert.calledOnce( testCallbackStub );
  69. sinon.assert.calledWithExactly( testCallbackStub, '@' );
  70. } );
  71. it( 'should not evaluate text for transparent batches', () => {
  72. model.enqueueChange( 'transparent', writer => {
  73. writer.insertText( '@', doc.selection.getFirstPosition() );
  74. } );
  75. sinon.assert.notCalled( testCallbackStub );
  76. } );
  77. } );
  78. describe( 'events', () => {
  79. it( 'should fire "matched:data" event when test callback returns true for model data changes', () => {
  80. testCallbackStub.returns( true );
  81. model.change( writer => {
  82. writer.insertText( '@', doc.selection.getFirstPosition() );
  83. } );
  84. sinon.assert.calledOnce( testCallbackStub );
  85. sinon.assert.calledOnce( matchedDataSpy );
  86. sinon.assert.notCalled( matchedSelectionSpy );
  87. sinon.assert.notCalled( unmatchedSpy );
  88. } );
  89. it( 'should fire "matched:selection" event when test callback returns true for model data changes', () => {
  90. testCallbackStub.returns( true );
  91. model.enqueueChange( 'transparent', writer => {
  92. writer.insertText( '@', doc.selection.getFirstPosition() );
  93. } );
  94. model.change( writer => {
  95. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  96. } );
  97. sinon.assert.calledOnce( testCallbackStub );
  98. sinon.assert.notCalled( matchedDataSpy );
  99. sinon.assert.calledOnce( matchedSelectionSpy );
  100. sinon.assert.notCalled( unmatchedSpy );
  101. } );
  102. it( 'should not fire "matched" event when test callback returns false', () => {
  103. testCallbackStub.returns( false );
  104. model.change( writer => {
  105. writer.insertText( '@', doc.selection.getFirstPosition() );
  106. } );
  107. sinon.assert.calledOnce( testCallbackStub );
  108. sinon.assert.notCalled( matchedDataSpy );
  109. sinon.assert.notCalled( matchedSelectionSpy );
  110. sinon.assert.notCalled( unmatchedSpy );
  111. } );
  112. it( 'should fire "unmatched" event when test callback returns false when it was previously matched', () => {
  113. testCallbackStub.returns( true );
  114. model.change( writer => {
  115. writer.insertText( '@', doc.selection.getFirstPosition() );
  116. } );
  117. sinon.assert.calledOnce( testCallbackStub );
  118. sinon.assert.calledOnce( matchedDataSpy );
  119. sinon.assert.notCalled( unmatchedSpy );
  120. testCallbackStub.returns( false );
  121. model.change( writer => {
  122. writer.insertText( '@', doc.selection.getFirstPosition() );
  123. } );
  124. sinon.assert.calledTwice( testCallbackStub );
  125. sinon.assert.calledOnce( matchedDataSpy );
  126. sinon.assert.calledOnce( unmatchedSpy );
  127. } );
  128. it( 'should fire "umatched" event when selection is expanded', () => {
  129. testCallbackStub.returns( true );
  130. model.change( writer => {
  131. writer.insertText( '@', doc.selection.getFirstPosition() );
  132. } );
  133. sinon.assert.calledOnce( testCallbackStub );
  134. sinon.assert.calledOnce( matchedDataSpy );
  135. sinon.assert.notCalled( matchedSelectionSpy );
  136. sinon.assert.notCalled( unmatchedSpy );
  137. model.change( writer => {
  138. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  139. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  140. } );
  141. sinon.assert.calledOnce( testCallbackStub );
  142. sinon.assert.calledOnce( matchedDataSpy );
  143. sinon.assert.notCalled( matchedSelectionSpy );
  144. sinon.assert.calledOnce( unmatchedSpy );
  145. } );
  146. } );
  147. } );