entercommand.js 4.5 KB

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