8
0

softbreakcommand.js 5.2 KB

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