inputcommand.js 7.1 KB

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