textwatcher.js 9.4 KB

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