8
0

inputcommand.js 6.7 KB

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