entercommand.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 EnterCommand from '../src/entercommand';
  7. import InsertDelta from '@ckeditor/ckeditor5-engine/src/model/delta/insertdelta';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'EnterCommand', () => {
  10. let editor, doc, schema, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. doc = editor.document;
  16. command = new EnterCommand( editor );
  17. editor.commands.add( 'enter', command );
  18. schema = doc.schema;
  19. // Note: We could use real names like 'paragraph', but that would make test patterns too long.
  20. // Plus, this is actually a good test that the algorithm can be used for any model.
  21. schema.registerItem( 'img', '$inline' );
  22. schema.registerItem( 'p', '$block' );
  23. schema.registerItem( 'h', '$block' );
  24. schema.registerItem( 'inlineLimit' );
  25. schema.registerItem( 'blockLimit' );
  26. schema.allow( { name: 'inlineLimit', inside: 'p' } );
  27. schema.allow( { name: '$text', inside: 'inlineLimit' } );
  28. schema.allow( { name: '$text', inside: '$root' } );
  29. schema.allow( { name: 'blockLimit', inside: '$root' } );
  30. schema.allow( { name: 'p', inside: 'blockLimit' } );
  31. schema.limits.add( 'inlineLimit' );
  32. schema.limits.add( 'blockLimit' );
  33. } );
  34. } );
  35. describe( 'EnterCommand', () => {
  36. it( 'enters a block using enqueueChanges', () => {
  37. setData( doc, '<p>foo[]</p>' );
  38. doc.enqueueChanges( () => {
  39. editor.execute( 'enter' );
  40. // We expect that command is executed in enqueue changes block. Since we are already in
  41. // an enqueued block, the command execution will be postponed. Hence, no changes.
  42. expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p>' );
  43. } );
  44. // After all enqueued changes are done, the command execution is reflected.
  45. expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p></p>' );
  46. } );
  47. it( 'creates InsertDelta if enter is at the beginning of block', () => {
  48. setData( doc, '<p>[]foo</p>' );
  49. editor.execute( 'enter' );
  50. const deltas = Array.from( doc.history.getDeltas() );
  51. expect( deltas[ deltas.length - 1 ] ).to.be.instanceof( InsertDelta );
  52. } );
  53. it( 'creates InsertDelta if enter is at the end of block', () => {
  54. setData( doc, '<p>foo[]</p>' );
  55. editor.execute( 'enter' );
  56. const deltas = Array.from( doc.history.getDeltas() );
  57. expect( deltas[ deltas.length - 1 ] ).to.be.instanceof( InsertDelta );
  58. } );
  59. } );
  60. describe( 'execute()', () => {
  61. describe( 'collapsed selection', () => {
  62. test(
  63. 'does nothing in the root',
  64. 'foo[]bar',
  65. 'foo[]bar'
  66. );
  67. test(
  68. 'splits block',
  69. '<p>x</p><p>foo[]bar</p><p>y</p>',
  70. '<p>x</p><p>foo</p><p>[]bar</p><p>y</p>'
  71. );
  72. test(
  73. 'splits block at the end',
  74. '<p>x</p><p>foo[]</p><p>y</p>',
  75. '<p>x</p><p>foo</p><p>[]</p><p>y</p>'
  76. );
  77. test(
  78. 'splits block at the beginning',
  79. '<p>x</p><p>[]foo</p><p>y</p>',
  80. '<p>x</p><p></p><p>[]foo</p><p>y</p>'
  81. );
  82. test(
  83. 'inserts new block after empty one',
  84. '<p>x</p><p>[]</p><p>y</p>',
  85. '<p>x</p><p></p><p>[]</p><p>y</p>'
  86. );
  87. } );
  88. describe( 'non-collapsed selection', () => {
  89. test(
  90. 'only deletes the content when directly in the root',
  91. 'fo[ob]ar',
  92. 'fo[]ar'
  93. );
  94. test(
  95. 'deletes text and splits',
  96. '<p>ab[cd]ef</p><p>ghi</p>',
  97. '<p>ab</p><p>[]ef</p><p>ghi</p>'
  98. );
  99. test(
  100. 'places selection in the 2nd element',
  101. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  102. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  103. );
  104. test(
  105. 'leaves one empty element after one was fully selected',
  106. '<p>x</p><p>[abcdef]</p><p>y</p>',
  107. '<p>x</p><p>[]</p><p>y</p>'
  108. );
  109. test(
  110. 'leaves one empty element after two were fully selected',
  111. '<p>[abc</p><p>def]</p>',
  112. '<p>[]</p>'
  113. );
  114. test(
  115. 'should not break inline limit elements - collapsed',
  116. '<p><inlineLimit>foo[]bar</inlineLimit></p>',
  117. '<p><inlineLimit>foo[]bar</inlineLimit></p>'
  118. );
  119. test(
  120. 'should not break inline limit elements',
  121. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  122. '<p><inlineLimit>foo[]baz</inlineLimit></p>'
  123. );
  124. test(
  125. 'should not break inline limit elements - selection partially inside',
  126. '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>',
  127. '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>'
  128. );
  129. test(
  130. 'should break paragraph in blockLimit',
  131. '<blockLimit><p>foo[]bar</p></blockLimit>',
  132. '<blockLimit><p>foo</p><p>[]bar</p></blockLimit>'
  133. );
  134. it( 'leaves one empty element after two were fully selected (backward)', () => {
  135. setData( doc, '<p>[abc</p><p>def]</p>' );
  136. // @TODO: Add option for setting selection direction to model utils.
  137. doc.selection._lastRangeBackward = true;
  138. command.execute();
  139. expect( getData( doc ) ).to.equal( '<p>[]</p>' );
  140. } );
  141. it( 'uses DataController.deleteContent', () => {
  142. const spy = sinon.spy();
  143. editor.data.on( 'deleteContent', spy );
  144. setData( doc, '<p>[x]</p>' );
  145. command.execute();
  146. expect( spy.calledOnce ).to.be.true;
  147. } );
  148. } );
  149. function test( title, input, output ) {
  150. it( title, () => {
  151. setData( doc, input );
  152. command.execute();
  153. expect( getData( doc ) ).to.equal( output );
  154. } );
  155. }
  156. } );
  157. } );