entercommand.js 5.0 KB

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