8
0

inputcommand.js 5.8 KB

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