textwatcher.js 8.8 KB

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