textwatcher.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * @license Copyright (c) 2003-2020, 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( '#isEnabled', () => {
  38. it( 'should be enabled after initialization', () => {
  39. expect( watcher.isEnabled ).to.be.true;
  40. } );
  41. it( 'should be disabled after setting #isEnabled to false', () => {
  42. watcher.isEnabled = false;
  43. expect( watcher.isEnabled ).to.be.false;
  44. } );
  45. } );
  46. describe( 'testCallback', () => {
  47. it( 'should evaluate text before caret for data changes', () => {
  48. model.change( writer => {
  49. writer.insertText( '@', doc.selection.getFirstPosition() );
  50. } );
  51. sinon.assert.calledOnce( testCallbackStub );
  52. sinon.assert.calledWithExactly( testCallbackStub, 'foo @' );
  53. } );
  54. it( 'should not evaluate text for not collapsed selection', () => {
  55. model.change( writer => {
  56. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  57. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  58. } );
  59. sinon.assert.notCalled( testCallbackStub );
  60. } );
  61. it( 'should evaluate text for selection changes', () => {
  62. model.change( writer => {
  63. writer.setSelection( doc.getRoot().getChild( 0 ), 1 );
  64. } );
  65. sinon.assert.calledOnce( testCallbackStub );
  66. sinon.assert.calledWithExactly( testCallbackStub, 'f' );
  67. } );
  68. it( 'should evaluate text before caret up to <softBreak>', () => {
  69. model.schema.register( 'softBreak', {
  70. allowWhere: '$text',
  71. isInline: true
  72. } );
  73. model.change( writer => {
  74. writer.insertElement( 'softBreak', doc.selection.getFirstPosition() );
  75. writer.insertText( '@', doc.selection.getFirstPosition() );
  76. } );
  77. sinon.assert.calledOnce( testCallbackStub );
  78. sinon.assert.calledWithExactly( testCallbackStub, '@' );
  79. } );
  80. it( 'should not evaluate text for transparent batches', () => {
  81. model.enqueueChange( 'transparent', writer => {
  82. writer.insertText( '@', doc.selection.getFirstPosition() );
  83. } );
  84. sinon.assert.notCalled( testCallbackStub );
  85. } );
  86. it( 'should not evaluate text when watcher is disabled', () => {
  87. watcher.isEnabled = false;
  88. model.change( writer => {
  89. writer.insertText( '@', doc.selection.getFirstPosition() );
  90. } );
  91. sinon.assert.notCalled( testCallbackStub );
  92. } );
  93. it( 'should evaluate text when watcher is enabled again', () => {
  94. watcher.isEnabled = false;
  95. model.change( writer => {
  96. writer.insertText( '@', doc.selection.getFirstPosition() );
  97. } );
  98. sinon.assert.notCalled( testCallbackStub );
  99. watcher.isEnabled = true;
  100. model.change( writer => {
  101. writer.insertText( '@', doc.selection.getFirstPosition() );
  102. } );
  103. sinon.assert.calledOnce( testCallbackStub );
  104. sinon.assert.calledWithExactly( testCallbackStub, 'foo @@' );
  105. } );
  106. } );
  107. describe( 'events', () => {
  108. it( 'should fire "matched:data" event when test callback returns true for model data changes', () => {
  109. testCallbackStub.returns( true );
  110. model.change( writer => {
  111. writer.insertText( '@', doc.selection.getFirstPosition() );
  112. } );
  113. sinon.assert.calledOnce( testCallbackStub );
  114. sinon.assert.calledOnce( matchedDataSpy );
  115. sinon.assert.notCalled( matchedSelectionSpy );
  116. sinon.assert.notCalled( unmatchedSpy );
  117. } );
  118. it( 'should not fire "matched:data" event when watcher is disabled' +
  119. ' (even when test callback returns true for model data changes)', () => {
  120. watcher.isEnabled = false;
  121. testCallbackStub.returns( true );
  122. model.change( writer => {
  123. writer.insertText( '@', doc.selection.getFirstPosition() );
  124. } );
  125. sinon.assert.notCalled( testCallbackStub );
  126. sinon.assert.notCalled( matchedDataSpy );
  127. sinon.assert.notCalled( matchedSelectionSpy );
  128. sinon.assert.notCalled( unmatchedSpy );
  129. } );
  130. it( 'should fire "matched:selection" event when test callback returns true for model data changes', () => {
  131. testCallbackStub.returns( true );
  132. model.enqueueChange( 'transparent', writer => {
  133. writer.insertText( '@', doc.selection.getFirstPosition() );
  134. } );
  135. model.change( writer => {
  136. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  137. } );
  138. sinon.assert.calledOnce( testCallbackStub );
  139. sinon.assert.notCalled( matchedDataSpy );
  140. sinon.assert.calledOnce( matchedSelectionSpy );
  141. sinon.assert.notCalled( unmatchedSpy );
  142. } );
  143. it( 'should not fire "matched:selection" event when when watcher is disabled' +
  144. ' (even when test callback returns true for model data changes)', () => {
  145. watcher.isEnabled = false;
  146. testCallbackStub.returns( true );
  147. model.enqueueChange( 'transparent', writer => {
  148. writer.insertText( '@', doc.selection.getFirstPosition() );
  149. } );
  150. model.change( writer => {
  151. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  152. } );
  153. sinon.assert.notCalled( testCallbackStub );
  154. sinon.assert.notCalled( matchedDataSpy );
  155. sinon.assert.notCalled( matchedSelectionSpy );
  156. sinon.assert.notCalled( unmatchedSpy );
  157. } );
  158. it( 'should not fire "matched" event when test callback returns false', () => {
  159. testCallbackStub.returns( false );
  160. model.change( writer => {
  161. writer.insertText( '@', doc.selection.getFirstPosition() );
  162. } );
  163. sinon.assert.calledOnce( testCallbackStub );
  164. sinon.assert.notCalled( matchedDataSpy );
  165. sinon.assert.notCalled( matchedSelectionSpy );
  166. sinon.assert.notCalled( unmatchedSpy );
  167. } );
  168. it( 'should fire "unmatched" event when test callback returns false when it was previously matched', () => {
  169. testCallbackStub.returns( true );
  170. model.change( writer => {
  171. writer.insertText( '@', doc.selection.getFirstPosition() );
  172. } );
  173. sinon.assert.calledOnce( testCallbackStub );
  174. sinon.assert.calledOnce( matchedDataSpy );
  175. sinon.assert.notCalled( unmatchedSpy );
  176. testCallbackStub.returns( false );
  177. model.change( writer => {
  178. writer.insertText( '@', doc.selection.getFirstPosition() );
  179. } );
  180. sinon.assert.calledTwice( testCallbackStub );
  181. sinon.assert.calledOnce( matchedDataSpy );
  182. sinon.assert.calledOnce( unmatchedSpy );
  183. } );
  184. it( 'should fire "umatched" event when selection is expanded', () => {
  185. testCallbackStub.returns( true );
  186. model.change( writer => {
  187. writer.insertText( '@', doc.selection.getFirstPosition() );
  188. } );
  189. sinon.assert.calledOnce( testCallbackStub );
  190. sinon.assert.calledOnce( matchedDataSpy );
  191. sinon.assert.notCalled( matchedSelectionSpy );
  192. sinon.assert.notCalled( unmatchedSpy );
  193. model.change( writer => {
  194. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  195. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  196. } );
  197. sinon.assert.calledOnce( testCallbackStub );
  198. sinon.assert.calledOnce( matchedDataSpy );
  199. sinon.assert.notCalled( matchedSelectionSpy );
  200. sinon.assert.calledOnce( unmatchedSpy );
  201. } );
  202. it( 'should not fire "umatched" event when selection is expanded if watcher is disabled', () => {
  203. watcher.isEnabled = false;
  204. testCallbackStub.returns( true );
  205. model.change( writer => {
  206. writer.insertText( '@', doc.selection.getFirstPosition() );
  207. } );
  208. sinon.assert.notCalled( testCallbackStub );
  209. sinon.assert.notCalled( matchedDataSpy );
  210. sinon.assert.notCalled( matchedSelectionSpy );
  211. sinon.assert.notCalled( unmatchedSpy );
  212. model.change( writer => {
  213. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  214. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  215. } );
  216. sinon.assert.notCalled( testCallbackStub );
  217. sinon.assert.notCalled( matchedDataSpy );
  218. sinon.assert.notCalled( matchedSelectionSpy );
  219. sinon.assert.notCalled( unmatchedSpy );
  220. } );
  221. } );
  222. } );