8
0

inputcommand.js 5.8 KB

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