entercommand.js 4.6 KB

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