inputcommand.js 6.0 KB

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