shiftentercommand.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 InsertDelta from '@ckeditor/ckeditor5-engine/src/model/delta/insertdelta';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import ShiftEnter from '../src/shiftenter';
  9. describe( 'ShiftEnterCommand', () => {
  10. let editor, model, doc, schema, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( { plugins: [ ShiftEnter ] } )
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. command = editor.commands.get( 'shiftEnter' );
  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( 'ShiftEnterCommand', () => {
  41. it( 'soft breaks a block using parent batch', () => {
  42. setData( model, '<p>foo[]</p>' );
  43. model.change( writer => {
  44. expect( writer.batch.deltas ).to.length( 0 );
  45. editor.execute( 'shiftEnter' );
  46. expect( writer.batch.deltas ).to.length.above( 0 );
  47. } );
  48. } );
  49. it( 'creates InsertDelta if soft enter is at the beginning of block', () => {
  50. setData( model, '<p>[]foo</p>' );
  51. editor.execute( 'shiftEnter' );
  52. const deltas = Array.from( doc.history.getDeltas() );
  53. expect( deltas[ deltas.length - 1 ] ).to.be.instanceof( InsertDelta );
  54. } );
  55. it( 'creates InsertDelta if soft enter is at the end of block', () => {
  56. setData( model, '<p>foo[]</p>' );
  57. editor.execute( 'shiftEnter' );
  58. const deltas = Array.from( doc.history.getDeltas() );
  59. expect( deltas[ deltas.length - 1 ] ).to.be.instanceof( InsertDelta );
  60. } );
  61. } );
  62. describe( 'execute()', () => {
  63. describe( 'collapsed selection', () => {
  64. test(
  65. 'inserts in the root',
  66. 'foo[]bar',
  67. 'foo<break></break>[]bar'
  68. );
  69. test(
  70. 'inserts inside block',
  71. '<p>x</p><p>foo[]bar</p><p>y</p>',
  72. '<p>x</p><p>foo<break></break>[]bar</p><p>y</p>'
  73. );
  74. test(
  75. 'inserts at the end of block',
  76. '<p>x</p><p>foo[]</p><p>y</p>',
  77. '<p>x</p><p>foo<break></break>[]</p><p>y</p>'
  78. );
  79. test(
  80. 'inserts at the beginning of block',
  81. '<p>x</p><p>[]foo</p><p>y</p>',
  82. '<p>x</p><p><break></break>[]foo</p><p>y</p>'
  83. );
  84. } );
  85. describe( 'non-collapsed selection', () => {
  86. test(
  87. 'deletes the content and inserts the break when directly in the root',
  88. 'fo[ob]ar',
  89. 'fo<break></break>[]ar'
  90. );
  91. test(
  92. 'deletes text and adds break',
  93. '<p>ab[cd]ef</p><p>ghi</p>',
  94. '<p>ab<break></break>[]ef</p><p>ghi</p>'
  95. );
  96. test(
  97. 'places selection in the 2nd element',
  98. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  99. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  100. );
  101. test(
  102. 'does nothing for selection that contains more than one range',
  103. '<p>[abc]</p><p>[def]</p>',
  104. '<p>[abc]</p><p>[def]</p>'
  105. );
  106. test(
  107. 'inserts break in empty element after it was fully selected',
  108. '<p>x</p><p>[abcdef]</p><p>y</p>',
  109. '<p>x</p><p><break></break>[]</p><p>y</p>'
  110. );
  111. test(
  112. 'leaves one empty element after two were fully selected',
  113. '<p>[abc</p><p>def]</p>',
  114. '<p>[]</p>'
  115. );
  116. test(
  117. 'should insert the break in inline limit element - collapsed',
  118. '<p><inlineLimit>foo[]bar</inlineLimit></p>',
  119. '<p><inlineLimit>foo<break></break>[]bar</inlineLimit></p>'
  120. );
  121. test(
  122. 'should insert the break in inline limit elements',
  123. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  124. '<p><inlineLimit>foo<break></break>[]baz</inlineLimit></p>'
  125. );
  126. test(
  127. 'should insert the break at beginning of the inline limit elements',
  128. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  129. '<p><inlineLimit>foo<break></break>[]baz</inlineLimit></p>'
  130. );
  131. test(
  132. 'should insert the break at ending of the inline limit elements',
  133. '<p><inlineLimit>foobaz[]</inlineLimit></p>',
  134. '<p><inlineLimit>foobaz<break></break>[]</inlineLimit></p>'
  135. );
  136. test(
  137. 'should not break inline limit elements - selection partially inside',
  138. '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>',
  139. '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>'
  140. );
  141. test(
  142. 'should break paragraph in blockLimit',
  143. '<blockLimit><p>foo[]bar</p></blockLimit>',
  144. '<blockLimit><p>foo<break></break>[]bar</p></blockLimit>'
  145. );
  146. test(
  147. 'does nothing when break element cannot be inserted in specified context',
  148. '<img>[]</img>',
  149. '<img>[]</img>'
  150. );
  151. it( 'leaves one empty element after two were fully selected (backward)', () => {
  152. setData( model, '<p>[abc</p><p>def]</p>' );
  153. // @TODO: Add option for setting selection direction to model utils.
  154. doc.selection._lastRangeBackward = true;
  155. command.execute();
  156. expect( getData( model ) ).to.equal( '<p>[]</p>' );
  157. } );
  158. it( 'uses DataController.deleteContent', () => {
  159. const spy = sinon.spy();
  160. editor.model.on( 'deleteContent', spy );
  161. setData( model, '<p>[x]</p>' );
  162. command.execute();
  163. expect( spy.calledOnce ).to.be.true;
  164. } );
  165. } );
  166. function test( title, input, output ) {
  167. it( title, () => {
  168. setData( model, input );
  169. command.execute();
  170. expect( getData( model ) ).to.equal( output );
  171. } );
  172. }
  173. } );
  174. } );