inputcommand.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. after( () => {
  30. return editor.destroy();
  31. } );
  32. beforeEach( () => {
  33. buffer.size = 0;
  34. } );
  35. describe( 'buffer', () => {
  36. it( 'has buffer getter', () => {
  37. expect( editor.commands.get( 'input' ).buffer ).to.be.an.instanceof( ChangeBuffer );
  38. } );
  39. it( 'has a buffer limit configured to default value of 20', () => {
  40. expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 20 );
  41. } );
  42. it( 'has a buffer configured to config.typing.undoStep', () => {
  43. return VirtualTestEditor.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 enqueueChanges', () => {
  56. setData( doc, '<p>foo[]bar</p>' );
  57. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  58. editor.execute( 'input', {
  59. text: ''
  60. } );
  61. expect( spy.calledOnce ).to.be.true;
  62. } );
  63. it( 'should lock and unlock buffer', () => {
  64. setData( doc, '<p>foo[]bar</p>' );
  65. const spyLock = testUtils.sinon.spy( buffer, 'lock' );
  66. const spyUnlock = testUtils.sinon.spy( buffer, 'unlock' );
  67. editor.execute( 'input', {
  68. text: ''
  69. } );
  70. expect( spyLock.calledOnce ).to.be.true;
  71. expect( spyUnlock.calledOnce ).to.be.true;
  72. } );
  73. it( 'inserts text for collapsed range', () => {
  74. setData( doc, '<p>foo[]</p>' );
  75. editor.execute( 'input', {
  76. text: 'bar',
  77. range: editor.document.selection.getFirstRange()
  78. } );
  79. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobar[]</p>' );
  80. expect( buffer.size ).to.be.equal( 3 );
  81. } );
  82. it( 'replaces text for range within single element on the beginning', () => {
  83. setData( doc, '<p>[fooba]r</p>' );
  84. editor.execute( 'input', {
  85. text: 'rab',
  86. range: editor.document.selection.getFirstRange()
  87. } );
  88. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
  89. expect( buffer.size ).to.be.equal( 3 );
  90. } );
  91. it( 'replaces text for range within single element in the middle', () => {
  92. setData( doc, '<p>fo[oba]r</p>' );
  93. editor.execute( 'input', {
  94. text: 'bazz',
  95. range: editor.document.selection.getFirstRange()
  96. } );
  97. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
  98. expect( buffer.size ).to.be.equal( 4 );
  99. } );
  100. it( 'replaces text for range within single element on the end', () => {
  101. setData( doc, '<p>fooba[r]</p>' );
  102. editor.execute( 'input', {
  103. text: 'zzz',
  104. range: editor.document.selection.getFirstRange()
  105. } );
  106. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
  107. expect( buffer.size ).to.be.equal( 3 );
  108. } );
  109. it( 'replaces text for range within multiple elements', () => {
  110. setData( doc, '<h1>F[OO</h1><p>b]ar</p>' );
  111. editor.execute( 'input', {
  112. text: 'unny c',
  113. range: editor.document.selection.getFirstRange()
  114. } );
  115. expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
  116. expect( buffer.size ).to.be.equal( 6 );
  117. } );
  118. it( 'uses current selection when range is not given', () => {
  119. setData( doc, '<p>foob[ar]</p>' );
  120. editor.execute( 'input', {
  121. text: 'az'
  122. } );
  123. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
  124. expect( buffer.size ).to.be.equal( 2 );
  125. } );
  126. it( 'only removes content when empty text given', () => {
  127. setData( doc, '<p>[fo]obar</p>' );
  128. editor.execute( 'input', {
  129. text: '',
  130. range: editor.document.selection.getFirstRange()
  131. } );
  132. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
  133. expect( buffer.size ).to.be.equal( 0 );
  134. } );
  135. it( 'should set selection according to passed resultRange (collapsed)', () => {
  136. setData( doc, '<p>[foo]bar</p>' );
  137. editor.execute( 'input', {
  138. text: 'new',
  139. resultRange: new Range( new Position( doc.getRoot(), [ 0, 5 ] ) )
  140. } );
  141. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>newba[]r</p>' );
  142. expect( buffer.size ).to.be.equal( 3 );
  143. } );
  144. it( 'should set selection according to passed resultRange (non-collapsed)', () => {
  145. setData( doc, '<p>[foo]bar</p>' );
  146. editor.execute( 'input', {
  147. text: 'new',
  148. resultRange: new Range( new Position( doc.getRoot(), [ 0, 3 ] ), new Position( doc.getRoot(), [ 0, 6 ] ) )
  149. } );
  150. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>new[bar]</p>' );
  151. expect( buffer.size ).to.be.equal( 3 );
  152. } );
  153. it( 'only removes content when no text given (with default non-collapsed range)', () => {
  154. setData( doc, '<p>[fo]obar</p>' );
  155. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  156. editor.execute( 'input' );
  157. expect( spy.callCount ).to.be.equal( 1 );
  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. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  164. editor.execute( 'input' );
  165. expect( spy.callCount ).to.be.equal( 1 );
  166. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fo[]obar</p>' );
  167. expect( buffer.size ).to.be.equal( 0 );
  168. } );
  169. } );
  170. describe( 'destroy', () => {
  171. it( 'should destroy change buffer', () => {
  172. const command = editor.commands.get( 'input' );
  173. const destroy = command._buffer.destroy = testUtils.sinon.spy();
  174. command.destroy();
  175. expect( destroy.calledOnce ).to.be.true;
  176. expect( command._buffer ).to.be.null;
  177. } );
  178. } );
  179. } );