inputcommand.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import InputCommand from '../src/inputcommand';
  10. import ChangeBuffer from '../src/utils/changebuffer';
  11. import Input from '../src/input';
  12. describe( 'InputCommand', () => {
  13. let editor, model, doc, buffer, inputCommand;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. return ModelTestEditor.create()
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. doc = model.document;
  21. inputCommand = new InputCommand( editor, 20 );
  22. editor.commands.add( 'input', inputCommand );
  23. buffer = inputCommand.buffer;
  24. buffer.size = 0;
  25. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  26. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  27. } );
  28. } );
  29. afterEach( () => {
  30. return editor.destroy();
  31. } );
  32. describe( 'buffer', () => {
  33. it( 'has buffer getter', () => {
  34. expect( editor.commands.get( 'input' ).buffer ).to.be.an.instanceof( ChangeBuffer );
  35. } );
  36. it( 'has a buffer limit configured to default value of 20', () => {
  37. expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 20 );
  38. } );
  39. it( 'has a buffer configured to config.typing.undoStep', () => {
  40. return VirtualTestEditor
  41. .create( {
  42. plugins: [ Input ],
  43. typing: {
  44. undoStep: 5
  45. }
  46. } )
  47. .then( editor => {
  48. expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 5 );
  49. } );
  50. } );
  51. } );
  52. describe( 'execute()', () => {
  53. it( 'uses enqueueChange', () => {
  54. setData( model, '<paragraph>foo[]bar</paragraph>' );
  55. model.enqueueChange( () => {
  56. editor.execute( 'input', { text: 'x' } );
  57. // We expect that command is executed in enqueue changes block. Since we are already in
  58. // an enqueued block, the command execution will be postponed. Hence, no changes.
  59. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  60. } );
  61. // After all enqueued changes are done, the command execution is reflected.
  62. expect( getData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
  63. } );
  64. it( 'should lock and unlock buffer', () => {
  65. setData( model, '<paragraph>foo[]bar</paragraph>' );
  66. const spyLock = testUtils.sinon.spy( buffer, 'lock' );
  67. const spyUnlock = testUtils.sinon.spy( buffer, 'unlock' );
  68. editor.execute( 'input', {
  69. text: ''
  70. } );
  71. expect( spyLock.calledOnce ).to.be.true;
  72. expect( spyUnlock.calledOnce ).to.be.true;
  73. } );
  74. it( 'inserts text for collapsed range', () => {
  75. setData( model, '<paragraph>foo[]</paragraph>' );
  76. editor.execute( 'input', {
  77. text: 'bar',
  78. range: doc.selection.getFirstRange()
  79. } );
  80. expect( getData( model ) ).to.equal( '<paragraph>foobar[]</paragraph>' );
  81. expect( buffer.size ).to.equal( 3 );
  82. } );
  83. it( 'replaces text for range within single element on the beginning', () => {
  84. setData( model, '<paragraph>[fooba]r</paragraph>' );
  85. editor.execute( 'input', {
  86. text: 'rab',
  87. range: doc.selection.getFirstRange()
  88. } );
  89. expect( getData( model ) ).to.equal( '<paragraph>rab[]r</paragraph>' );
  90. expect( buffer.size ).to.equal( 3 );
  91. } );
  92. it( 'replaces text for range within single element in the middle', () => {
  93. setData( model, '<paragraph>fo[oba]r</paragraph>' );
  94. editor.execute( 'input', {
  95. text: 'bazz',
  96. range: doc.selection.getFirstRange()
  97. } );
  98. expect( getData( model ) ).to.equal( '<paragraph>fobazz[]r</paragraph>' );
  99. expect( buffer.size ).to.equal( 4 );
  100. } );
  101. it( 'replaces text for range within single element on the end', () => {
  102. setData( model, '<paragraph>fooba[r]</paragraph>' );
  103. editor.execute( 'input', {
  104. text: 'zzz',
  105. range: doc.selection.getFirstRange()
  106. } );
  107. expect( getData( model ) ).to.equal( '<paragraph>foobazzz[]</paragraph>' );
  108. expect( buffer.size ).to.equal( 3 );
  109. } );
  110. it( 'replaces text for range within multiple elements', () => {
  111. setData( model, '<heading1>F[OO</heading1><paragraph>b]ar</paragraph>' );
  112. editor.execute( 'input', {
  113. text: 'unny c',
  114. range: doc.selection.getFirstRange()
  115. } );
  116. expect( getData( model ) ).to.equal( '<heading1>Funny c[]ar</heading1>' );
  117. expect( buffer.size ).to.equal( 6 );
  118. } );
  119. it( 'uses current selection when range is not given', () => {
  120. setData( model, '<paragraph>foob[ar]</paragraph>' );
  121. editor.execute( 'input', {
  122. text: 'az'
  123. } );
  124. expect( getData( model ) ).to.equal( '<paragraph>foobaz[]</paragraph>' );
  125. expect( buffer.size ).to.equal( 2 );
  126. } );
  127. it( 'only removes content when empty text given', () => {
  128. setData( model, '<paragraph>[fo]obar</paragraph>' );
  129. editor.execute( 'input', {
  130. text: '',
  131. range: doc.selection.getFirstRange()
  132. } );
  133. expect( getData( model ) ).to.equal( '<paragraph>[]obar</paragraph>' );
  134. expect( buffer.size ).to.equal( 0 );
  135. } );
  136. it( 'should set selection according to passed resultRange (collapsed)', () => {
  137. setData( model, '<paragraph>[foo]bar</paragraph>' );
  138. editor.execute( 'input', {
  139. text: 'new',
  140. resultRange: editor.model.createRange( editor.model.createPositionFromPath( doc.getRoot(), [ 0, 5 ] ) )
  141. } );
  142. expect( getData( model ) ).to.equal( '<paragraph>newba[]r</paragraph>' );
  143. expect( buffer.size ).to.equal( 3 );
  144. } );
  145. it( 'should set selection according to passed resultRange (non-collapsed)', () => {
  146. setData( model, '<paragraph>[foo]bar</paragraph>' );
  147. editor.execute( 'input', {
  148. text: 'new',
  149. resultRange: editor.model.createRange(
  150. editor.model.createPositionFromPath( doc.getRoot(), [ 0, 3 ] ),
  151. editor.model.createPositionFromPath( doc.getRoot(), [ 0, 6 ] )
  152. )
  153. } );
  154. expect( getData( model ) ).to.equal( '<paragraph>new[bar]</paragraph>' );
  155. expect( buffer.size ).to.equal( 3 );
  156. } );
  157. it( 'only removes content when no text given (with default non-collapsed range)', () => {
  158. setData( model, '<paragraph>[fo]obar</paragraph>' );
  159. editor.execute( 'input' );
  160. expect( getData( model ) ).to.equal( '<paragraph>[]obar</paragraph>' );
  161. expect( buffer.size ).to.equal( 0 );
  162. } );
  163. it( 'does not change selection and content when no text given (with default collapsed range)', () => {
  164. setData( model, '<paragraph>fo[]obar</paragraph>' );
  165. editor.execute( 'input' );
  166. expect( getData( model ) ).to.equal( '<paragraph>fo[]obar</paragraph>' );
  167. expect( buffer.size ).to.equal( 0 );
  168. } );
  169. it( 'does not create insert delta when no text given', () => {
  170. setData( model, '<paragraph>foo[]bar</paragraph>' );
  171. const version = doc.version;
  172. editor.execute( 'input' );
  173. expect( doc.version ).to.equal( version );
  174. } );
  175. it( 'handles multi-range selection', () => {
  176. model.schema.register( 'object', {
  177. allowWhere: '$block',
  178. allowContentOf: '$block',
  179. isObject: true
  180. } );
  181. setData(
  182. model,
  183. '<paragraph>x</paragraph>' +
  184. '[<object>y</object>]' +
  185. '<paragraph>y</paragraph>' +
  186. '[<object>y</object>]' +
  187. '<paragraph>z</paragraph>'
  188. );
  189. // deleteContent() does not support multi-range selections yet, so we need to mock it here.
  190. // See https://github.com/ckeditor/ckeditor5/issues/6328.
  191. model.on( 'deleteContent', ( evt, args ) => {
  192. const [ selection ] = args;
  193. if ( selection.rangeCount != 2 ) {
  194. return;
  195. }
  196. evt.stop();
  197. model.change( writer => {
  198. let rangeSelection;
  199. for ( const range of Array.from( selection.getRanges() ) ) {
  200. rangeSelection = writer.createSelection( range );
  201. model.deleteContent( rangeSelection );
  202. }
  203. writer.setSelection( rangeSelection );
  204. } );
  205. }, { priority: 'high' } );
  206. editor.execute( 'input', {
  207. text: 'foo'
  208. } );
  209. expect( getData( model ) ).to.equal(
  210. '<paragraph>x</paragraph>' +
  211. '<paragraph></paragraph>' +
  212. '<paragraph>y</paragraph>' +
  213. '<paragraph>foo[]</paragraph>' +
  214. '<paragraph>z</paragraph>'
  215. );
  216. } );
  217. it( 'uses typing batch while removing and inserting the content', () => {
  218. expect( inputCommand._batches.has( getCurrentBatch() ), 'batch before typing' ).to.equal( false );
  219. model.on( 'deleteContent', () => {
  220. expect( inputCommand._batches.has( getCurrentBatch() ), 'batch when deleting content' ).to.equal( true );
  221. }, { priority: 'highest' } );
  222. model.on( 'insertContent', () => {
  223. expect( inputCommand._batches.has( getCurrentBatch() ), 'batch when inserting content' ).to.equal( true );
  224. }, { priority: 'lowest' } );
  225. setData( model, '<paragraph>[foo]</paragraph>' );
  226. editor.execute( 'input', { text: 'bar' } );
  227. expect( getData( model ) ).to.equal( '<paragraph>bar[]</paragraph>' );
  228. function getCurrentBatch() {
  229. return editor.model.change( writer => writer.batch );
  230. }
  231. } );
  232. } );
  233. describe( 'destroy()', () => {
  234. it( 'should destroy change buffer', () => {
  235. const command = editor.commands.get( 'input' );
  236. const destroy = command._buffer.destroy = testUtils.sinon.spy();
  237. command.destroy();
  238. expect( destroy.calledOnce ).to.be.true;
  239. } );
  240. } );
  241. } );