entercommand.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. describe( 'copyOnEnter', () => {
  78. beforeEach( () => {
  79. schema.extend( '$text', { allowAttributes: [ 'foo', 'bar' ] } );
  80. schema.setAttributeProperties( 'foo', { copyOnEnter: true } );
  81. } );
  82. test(
  83. 'allowed attributes are copied',
  84. '<p><$text foo="true">test[]</$text></p>',
  85. '<p><$text foo="true">test</$text></p><p selection:foo="true"><$text foo="true">[]</$text></p>'
  86. );
  87. test(
  88. 'unknown attributes are not copied',
  89. '<p><$text bar="true">test[]</$text></p>',
  90. '<p><$text bar="true">test</$text></p><p>[]</p>'
  91. );
  92. test(
  93. 'only allowed attributes are copied from mix set',
  94. '<p><$text bar="true" foo="true">test[]</$text></p>',
  95. '<p><$text bar="true" foo="true">test</$text></p><p selection:foo="true"><$text foo="true">[]</$text></p>'
  96. );
  97. } );
  98. } );
  99. describe( 'non-collapsed selection', () => {
  100. test(
  101. 'only deletes the content when directly in the root',
  102. 'fo[ob]ar',
  103. 'fo[]ar'
  104. );
  105. test(
  106. 'deletes text and splits',
  107. '<p>ab[cd]ef</p><p>ghi</p>',
  108. '<p>ab</p><p>[]ef</p><p>ghi</p>'
  109. );
  110. test(
  111. 'places selection in the 2nd element',
  112. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  113. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  114. );
  115. test(
  116. 'leaves one empty element after one was fully selected',
  117. '<p>x</p><p>[abcdef]</p><p>y</p>',
  118. '<p>x</p><p>[]</p><p>y</p>'
  119. );
  120. test(
  121. 'leaves one empty element after two were fully selected',
  122. '<p>[abc</p><p>def]</p>',
  123. '<p>[]</p>'
  124. );
  125. test(
  126. 'should not break inline limit elements - collapsed',
  127. '<p><inlineLimit>foo[]bar</inlineLimit></p>',
  128. '<p><inlineLimit>foo[]bar</inlineLimit></p>'
  129. );
  130. test(
  131. 'should not break inline limit elements',
  132. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  133. '<p><inlineLimit>foo[]baz</inlineLimit></p>'
  134. );
  135. it( 'should not break inline limit elements - selection partially inside', () => {
  136. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  137. model.change( () => {
  138. setData( model, '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  139. command.execute();
  140. expect( getData( model ) ).to.equal( '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  141. } );
  142. } );
  143. test(
  144. 'should break paragraph in blockLimit',
  145. '<blockLimit><p>foo[]bar</p></blockLimit>',
  146. '<blockLimit><p>foo</p><p>[]bar</p></blockLimit>'
  147. );
  148. it( 'leaves one empty element after two were fully selected (backward)', () => {
  149. setData( model, '<p>[abc</p><p>def]</p>' );
  150. // @TODO: Add option for setting selection direction to model utils.
  151. doc.selection._lastRangeBackward = true;
  152. command.execute();
  153. expect( getData( model ) ).to.equal( '<p>[]</p>' );
  154. } );
  155. it( 'uses DataController.deleteContent', () => {
  156. const spy = sinon.spy();
  157. editor.model.on( 'deleteContent', spy );
  158. setData( model, '<p>[x]</p>' );
  159. command.execute();
  160. expect( spy.calledOnce ).to.be.true;
  161. } );
  162. } );
  163. function test( title, input, output ) {
  164. it( title, () => {
  165. setData( model, input );
  166. command.execute();
  167. expect( getData( model ) ).to.equal( output );
  168. } );
  169. }
  170. } );
  171. } );