inputcommand.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import InputCommand from '../src/inputcommand';
  9. import ChangeBuffer from '../src/changebuffer';
  10. describe( 'InputCommand', () => {
  11. let editor, doc, buffer;
  12. testUtils.createSinonSandbox();
  13. before( () => {
  14. return ModelTestEditor.create( )
  15. .then( newEditor => {
  16. editor = newEditor;
  17. doc = editor.document;
  18. buffer = new ChangeBuffer( doc, 20 );
  19. editor.commands.set( 'input', new InputCommand( editor ) );
  20. doc.schema.registerItem( 'p', '$block' );
  21. doc.schema.registerItem( 'h1', '$block' );
  22. } );
  23. } );
  24. beforeEach( () => {
  25. buffer.size = 0;
  26. } );
  27. describe( 'execute', () => {
  28. it( 'uses enqueueChanges', () => {
  29. setData( doc, '<p>foo[]bar</p>' );
  30. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  31. editor.execute( 'input', {
  32. buffer: buffer
  33. } );
  34. expect( spy.calledOnce ).to.be.true;
  35. } );
  36. it( 'inserts text for collapsed range', () => {
  37. setData( doc, '<p>foo[]</p>' );
  38. editor.execute( 'input', {
  39. buffer: buffer,
  40. text: 'bar',
  41. range: editor.document.selection.getFirstRange()
  42. } );
  43. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobar[]</p>' );
  44. expect( buffer.size ).to.be.equal( 3 );
  45. } );
  46. it( 'replaces text for range within single element on the beginning', () => {
  47. setData( doc, '<p>[fooba]r</p>' );
  48. editor.execute( 'input', {
  49. buffer: buffer,
  50. text: 'rab',
  51. range: editor.document.selection.getFirstRange()
  52. } );
  53. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
  54. expect( buffer.size ).to.be.equal( 3 );
  55. } );
  56. it( 'replaces text for range within single element in the middle', () => {
  57. setData( doc, '<p>fo[oba]r</p>' );
  58. editor.execute( 'input', {
  59. buffer: buffer,
  60. text: 'bazz',
  61. range: editor.document.selection.getFirstRange()
  62. } );
  63. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
  64. expect( buffer.size ).to.be.equal( 4 );
  65. } );
  66. it( 'replaces text for range within single element on the end', () => {
  67. setData( doc, '<p>fooba[r]</p>' );
  68. editor.execute( 'input', {
  69. buffer: buffer,
  70. text: 'zzz',
  71. range: editor.document.selection.getFirstRange()
  72. } );
  73. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
  74. expect( buffer.size ).to.be.equal( 3 );
  75. } );
  76. it( 'replaces text for range within multiple elements', () => {
  77. setData( doc, '<h1>F[OO</h1><p>b]ar</p>' );
  78. editor.execute( 'input', {
  79. buffer: buffer,
  80. text: 'unny c',
  81. range: editor.document.selection.getFirstRange()
  82. } );
  83. expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
  84. expect( buffer.size ).to.be.equal( 6 );
  85. } );
  86. it( 'uses current selection when range is not given', () => {
  87. setData( doc, '<p>foob[ar]</p>' );
  88. editor.execute( 'input', {
  89. buffer: buffer,
  90. text: 'az'
  91. } );
  92. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
  93. expect( buffer.size ).to.be.equal( 2 );
  94. } );
  95. it( 'only removes content when text is not given', () => {
  96. setData( doc, '<p>[fo]obar</p>' );
  97. editor.execute( 'input', {
  98. buffer: buffer,
  99. range: editor.document.selection.getFirstRange()
  100. } );
  101. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
  102. expect( buffer.size ).to.be.equal( 0 );
  103. } );
  104. it( 'does nothing when there is no range', () => {
  105. setData( doc, '<p>[fo]obar</p>' );
  106. testUtils.sinon.stub( editor.document.selection, 'getFirstRange' ).returns( null );
  107. editor.execute( 'input', {
  108. buffer: buffer,
  109. text: 'baz'
  110. } );
  111. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
  112. expect( buffer.size ).to.be.equal( 0 );
  113. } );
  114. it( 'does nothing when there is no buffer', () => {
  115. setData( doc, '<p>[fo]obar</p>' );
  116. editor.execute( 'input', {
  117. text: 'baz',
  118. range: editor.document.selection.getFirstRange()
  119. } );
  120. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
  121. expect( buffer.size ).to.be.equal( 0 );
  122. } );
  123. it( 'does nothing when there is no options object provided', () => {
  124. setData( doc, '<p>[fo]obar</p>' );
  125. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  126. editor.execute( 'input' );
  127. expect( spy.callCount ).to.be.equal( 0 );
  128. expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[fo]obar</p>' );
  129. expect( buffer.size ).to.be.equal( 0 );
  130. } );
  131. } );
  132. } );