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