inputcommand.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 Range from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  11. import InputCommand from '../src/inputcommand';
  12. import ChangeBuffer from '../src/changebuffer';
  13. import Input from '../src/input';
  14. describe( 'InputCommand', () => {
  15. let editor, doc, buffer;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. return ModelTestEditor.create()
  19. .then( newEditor => {
  20. editor = newEditor;
  21. doc = editor.document;
  22. const inputCommand = new InputCommand( editor, 20 );
  23. editor.commands.add( 'input', inputCommand );
  24. buffer = inputCommand.buffer;
  25. buffer.size = 0;
  26. doc.schema.registerItem( 'p', '$block' );
  27. doc.schema.registerItem( 'h1', '$block' );
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy();
  32. } );
  33. describe( 'buffer', () => {
  34. it( 'has buffer getter', () => {
  35. expect( editor.commands.get( 'input' ).buffer ).to.be.an.instanceof( ChangeBuffer );
  36. } );
  37. it( 'has a buffer limit configured to default value of 20', () => {
  38. expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 20 );
  39. } );
  40. it( 'has a buffer configured to config.typing.undoStep', () => {
  41. return VirtualTestEditor
  42. .create( {
  43. plugins: [ Input ],
  44. typing: {
  45. undoStep: 5
  46. }
  47. } )
  48. .then( editor => {
  49. expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 5 );
  50. } );
  51. } );
  52. } );
  53. describe( 'execute()', () => {
  54. it( 'uses enqueueChanges', () => {
  55. setData( doc, '<p>foo[]bar</p>' );
  56. doc.enqueueChanges( () => {
  57. editor.execute( 'input', { text: 'x' } );
  58. // We expect that command is executed in enqueue changes block. Since we are already in
  59. // an enqueued block, the command execution will be postponed. Hence, no changes.
  60. expect( getData( doc ) ).to.be.equal( '<p>foo[]bar</p>' );
  61. } );
  62. // After all enqueued changes are done, the command execution is reflected.
  63. expect( getData( doc ) ).to.be.equal( '<p>foox[]bar</p>' );
  64. } );
  65. it( 'should lock and unlock buffer', () => {
  66. setData( doc, '<p>foo[]bar</p>' );
  67. const spyLock = testUtils.sinon.spy( buffer, 'lock' );
  68. const spyUnlock = testUtils.sinon.spy( buffer, 'unlock' );
  69. editor.execute( 'input', {
  70. text: ''
  71. } );
  72. expect( spyLock.calledOnce ).to.be.true;
  73. expect( spyUnlock.calledOnce ).to.be.true;
  74. } );
  75. it( 'inserts text for collapsed range', () => {
  76. setData( doc, '<p>foo[]</p>' );
  77. editor.execute( 'input', {
  78. text: 'bar',
  79. range: editor.document.selection.getFirstRange()
  80. } );
  81. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobar[]</p>' );
  82. expect( buffer.size ).to.be.equal( 3 );
  83. } );
  84. it( 'replaces text for range within single element on the beginning', () => {
  85. setData( doc, '<p>[fooba]r</p>' );
  86. editor.execute( 'input', {
  87. text: 'rab',
  88. range: editor.document.selection.getFirstRange()
  89. } );
  90. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
  91. expect( buffer.size ).to.be.equal( 3 );
  92. } );
  93. it( 'replaces text for range within single element in the middle', () => {
  94. setData( doc, '<p>fo[oba]r</p>' );
  95. editor.execute( 'input', {
  96. text: 'bazz',
  97. range: editor.document.selection.getFirstRange()
  98. } );
  99. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
  100. expect( buffer.size ).to.be.equal( 4 );
  101. } );
  102. it( 'replaces text for range within single element on the end', () => {
  103. setData( doc, '<p>fooba[r]</p>' );
  104. editor.execute( 'input', {
  105. text: 'zzz',
  106. range: editor.document.selection.getFirstRange()
  107. } );
  108. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
  109. expect( buffer.size ).to.be.equal( 3 );
  110. } );
  111. it( 'replaces text for range within multiple elements', () => {
  112. setData( doc, '<h1>F[OO</h1><p>b]ar</p>' );
  113. editor.execute( 'input', {
  114. text: 'unny c',
  115. range: editor.document.selection.getFirstRange()
  116. } );
  117. expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
  118. expect( buffer.size ).to.be.equal( 6 );
  119. } );
  120. it( 'uses current selection when range is not given', () => {
  121. setData( doc, '<p>foob[ar]</p>' );
  122. editor.execute( 'input', {
  123. text: 'az'
  124. } );
  125. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
  126. expect( buffer.size ).to.be.equal( 2 );
  127. } );
  128. it( 'only removes content when empty text given', () => {
  129. setData( doc, '<p>[fo]obar</p>' );
  130. editor.execute( 'input', {
  131. text: '',
  132. range: editor.document.selection.getFirstRange()
  133. } );
  134. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
  135. expect( buffer.size ).to.be.equal( 0 );
  136. } );
  137. it( 'should set selection according to passed resultRange (collapsed)', () => {
  138. setData( doc, '<p>[foo]bar</p>' );
  139. editor.execute( 'input', {
  140. text: 'new',
  141. resultRange: new Range( new Position( doc.getRoot(), [ 0, 5 ] ) )
  142. } );
  143. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>newba[]r</p>' );
  144. expect( buffer.size ).to.be.equal( 3 );
  145. } );
  146. it( 'should set selection according to passed resultRange (non-collapsed)', () => {
  147. setData( doc, '<p>[foo]bar</p>' );
  148. editor.execute( 'input', {
  149. text: 'new',
  150. resultRange: new Range( new Position( doc.getRoot(), [ 0, 3 ] ), new Position( doc.getRoot(), [ 0, 6 ] ) )
  151. } );
  152. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>new[bar]</p>' );
  153. expect( buffer.size ).to.be.equal( 3 );
  154. } );
  155. it( 'only removes content when no text given (with default non-collapsed range)', () => {
  156. setData( doc, '<p>[fo]obar</p>' );
  157. editor.execute( 'input' );
  158. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
  159. expect( buffer.size ).to.be.equal( 0 );
  160. } );
  161. it( 'does not change selection and content when no text given (with default collapsed range)', () => {
  162. setData( doc, '<p>fo[]obar</p>' );
  163. editor.execute( 'input' );
  164. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fo[]obar</p>' );
  165. expect( buffer.size ).to.be.equal( 0 );
  166. } );
  167. } );
  168. describe( 'destroy', () => {
  169. it( 'should destroy change buffer', () => {
  170. const command = editor.commands.get( 'input' );
  171. const destroy = command._buffer.destroy = testUtils.sinon.spy();
  172. command.destroy();
  173. expect( destroy.calledOnce ).to.be.true;
  174. } );
  175. } );
  176. } );