textwatcher.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. describe( '"matched:data"', () => {
  109. it( 'should be fired 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 be fired with additional data when test callback returns true for model data changes', () => {
  120. const additionalData = { abc: 'xyz' };
  121. testCallbackStub.returns( additionalData );
  122. model.change( writer => {
  123. writer.insertText( '@', doc.selection.getFirstPosition() );
  124. } );
  125. sinon.assert.calledOnce( testCallbackStub );
  126. sinon.assert.calledOnce( matchedDataSpy );
  127. sinon.assert.notCalled( matchedSelectionSpy );
  128. sinon.assert.notCalled( unmatchedSpy );
  129. expect( matchedDataSpy.firstCall.args[ 1 ] ).to.deep.include( additionalData );
  130. } );
  131. } );
  132. it( 'should not fire "matched:data" event when watcher is disabled' +
  133. ' (even when test callback returns true for model data changes)', () => {
  134. watcher.isEnabled = false;
  135. testCallbackStub.returns( true );
  136. model.change( writer => {
  137. writer.insertText( '@', doc.selection.getFirstPosition() );
  138. } );
  139. sinon.assert.notCalled( testCallbackStub );
  140. sinon.assert.notCalled( matchedDataSpy );
  141. sinon.assert.notCalled( matchedSelectionSpy );
  142. sinon.assert.notCalled( unmatchedSpy );
  143. } );
  144. it( 'should fire "matched:selection" event when test callback returns true for model data changes', () => {
  145. testCallbackStub.returns( true );
  146. model.enqueueChange( 'transparent', writer => {
  147. writer.insertText( '@', doc.selection.getFirstPosition() );
  148. } );
  149. model.change( writer => {
  150. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  151. } );
  152. sinon.assert.calledOnce( testCallbackStub );
  153. sinon.assert.notCalled( matchedDataSpy );
  154. sinon.assert.calledOnce( matchedSelectionSpy );
  155. sinon.assert.notCalled( unmatchedSpy );
  156. } );
  157. it( 'should not fire "matched:selection" event when when watcher is disabled' +
  158. ' (even when test callback returns true for model data changes)', () => {
  159. watcher.isEnabled = false;
  160. testCallbackStub.returns( true );
  161. model.enqueueChange( 'transparent', writer => {
  162. writer.insertText( '@', doc.selection.getFirstPosition() );
  163. } );
  164. model.change( writer => {
  165. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  166. } );
  167. sinon.assert.notCalled( testCallbackStub );
  168. sinon.assert.notCalled( matchedDataSpy );
  169. sinon.assert.notCalled( matchedSelectionSpy );
  170. sinon.assert.notCalled( unmatchedSpy );
  171. } );
  172. it( 'should not fire "matched" event when test callback returns false', () => {
  173. testCallbackStub.returns( false );
  174. model.change( writer => {
  175. writer.insertText( '@', doc.selection.getFirstPosition() );
  176. } );
  177. sinon.assert.calledOnce( testCallbackStub );
  178. sinon.assert.notCalled( matchedDataSpy );
  179. sinon.assert.notCalled( matchedSelectionSpy );
  180. sinon.assert.notCalled( unmatchedSpy );
  181. } );
  182. it( 'should fire "unmatched" event when test callback returns false when it was previously matched', () => {
  183. testCallbackStub.returns( true );
  184. model.change( writer => {
  185. writer.insertText( '@', doc.selection.getFirstPosition() );
  186. } );
  187. sinon.assert.calledOnce( testCallbackStub );
  188. sinon.assert.calledOnce( matchedDataSpy );
  189. sinon.assert.notCalled( unmatchedSpy );
  190. testCallbackStub.returns( false );
  191. model.change( writer => {
  192. writer.insertText( '@', doc.selection.getFirstPosition() );
  193. } );
  194. sinon.assert.calledTwice( testCallbackStub );
  195. sinon.assert.calledOnce( matchedDataSpy );
  196. sinon.assert.calledOnce( unmatchedSpy );
  197. } );
  198. it( 'should fire "umatched" event when selection is expanded', () => {
  199. testCallbackStub.returns( true );
  200. model.change( writer => {
  201. writer.insertText( '@', doc.selection.getFirstPosition() );
  202. } );
  203. sinon.assert.calledOnce( testCallbackStub );
  204. sinon.assert.calledOnce( matchedDataSpy );
  205. sinon.assert.notCalled( matchedSelectionSpy );
  206. sinon.assert.notCalled( unmatchedSpy );
  207. model.change( writer => {
  208. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  209. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  210. } );
  211. sinon.assert.calledOnce( testCallbackStub );
  212. sinon.assert.calledOnce( matchedDataSpy );
  213. sinon.assert.notCalled( matchedSelectionSpy );
  214. sinon.assert.calledOnce( unmatchedSpy );
  215. } );
  216. it( 'should not fire "umatched" event when selection is expanded if watcher is disabled', () => {
  217. watcher.isEnabled = false;
  218. testCallbackStub.returns( true );
  219. model.change( writer => {
  220. writer.insertText( '@', doc.selection.getFirstPosition() );
  221. } );
  222. sinon.assert.notCalled( testCallbackStub );
  223. sinon.assert.notCalled( matchedDataSpy );
  224. sinon.assert.notCalled( matchedSelectionSpy );
  225. sinon.assert.notCalled( unmatchedSpy );
  226. model.change( writer => {
  227. const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
  228. writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  229. } );
  230. sinon.assert.notCalled( testCallbackStub );
  231. sinon.assert.notCalled( matchedDataSpy );
  232. sinon.assert.notCalled( matchedSelectionSpy );
  233. sinon.assert.notCalled( unmatchedSpy );
  234. } );
  235. } );
  236. } );