entercommand.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
  7. import { default as EnterCommand, enterBlock } from '/ckeditor5/enter/entercommand.js';
  8. import { getData, setData } from '/tests/engine/_utils/model.js';
  9. let editor, doc;
  10. beforeEach( () => {
  11. return VirtualTestEditor.create( )
  12. .then( newEditor => {
  13. editor = newEditor;
  14. doc = editor.document;
  15. doc.createRoot();
  16. const command = new EnterCommand( editor );
  17. editor.commands.set( 'enter', command );
  18. const schema = doc.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.registerItem( 'img', '$inline' );
  22. schema.registerItem( 'p', '$block' );
  23. schema.registerItem( 'h', '$block' );
  24. } );
  25. } );
  26. describe( 'EnterCommand', () => {
  27. it( 'enters a block using enqueueChanges', () => {
  28. const spy = sinon.spy( doc, 'enqueueChanges' );
  29. setData( doc, '<p>foo<selection /></p>' );
  30. editor.execute( 'enter' );
  31. expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p></p>' );
  32. expect( spy.calledOnce ).to.be.true;
  33. } );
  34. } );
  35. describe( 'enterBlock', () => {
  36. describe( 'collapsed selection', () => {
  37. test(
  38. 'does nothing in the root',
  39. 'foo<selection />bar',
  40. 'foo<selection />bar',
  41. { defaultBlockName: 'p' }
  42. );
  43. test(
  44. 'splits block',
  45. '<p>x</p><p>foo<selection />bar</p><p>y</p>',
  46. '<p>x</p><p>foo</p><p><selection />bar</p><p>y</p>',
  47. { defaultBlockName: 'p' }
  48. );
  49. test(
  50. 'splits block (other than default)',
  51. '<p>x</p><h>foo<selection />bar</h><p>y</p>',
  52. '<p>x</p><h>foo</h><h><selection />bar</h><p>y</p>',
  53. { defaultBlockName: 'p' }
  54. );
  55. test(
  56. 'splits block at the end',
  57. '<p>x</p><p>foo<selection /></p><p>y</p>',
  58. '<p>x</p><p>foo</p><p><selection /></p><p>y</p>',
  59. { defaultBlockName: 'p' }
  60. );
  61. test(
  62. 'splits block at the beginning',
  63. '<p>x</p><p><selection />foo</p><p>y</p>',
  64. '<p>x</p><p></p><p><selection />foo</p><p>y</p>',
  65. { defaultBlockName: 'p' }
  66. );
  67. test(
  68. 'splits block at the beginning (other than default)',
  69. '<p>x</p><h><selection />foo</h><p>y</p>',
  70. '<p>x</p><h></h><h><selection />foo</h><p>y</p>',
  71. { defaultBlockName: 'p' }
  72. );
  73. test(
  74. 'creates default block when leaving other block',
  75. '<h>foo<selection /></h><p>x</p>',
  76. '<h>foo</h><p><selection /></p><p>x</p>',
  77. { defaultBlockName: 'p' }
  78. );
  79. test(
  80. 'does not rename when default block is not allowed',
  81. '<h>foo<selection /></h><p>x</p>',
  82. '<h>foo</h><h><selection /></h><p>x</p>',
  83. { defaultBlockName: 'xxx' }
  84. );
  85. test(
  86. 'inserts new block after empty one',
  87. '<p>x</p><p><selection /></p><p>y</p>',
  88. '<p>x</p><p></p><p><selection /></p><p>y</p>',
  89. { defaultBlockName: 'p' }
  90. );
  91. test(
  92. 'inserts new block after empty one (other than default)',
  93. '<p>x</p><h><selection /></h><p>y</p>',
  94. '<p>x</p><h></h><p><selection /></p><p>y</p>',
  95. { defaultBlockName: 'p' }
  96. );
  97. } );
  98. describe( 'non-collapsed selection', () => {
  99. test(
  100. 'only deletes the content when directly in the root',
  101. 'fo<selection>ob</selection>ar',
  102. 'fo<selection />ar',
  103. { defaultBlockName: 'p' }
  104. );
  105. test(
  106. 'deletes text and splits',
  107. '<p>ab<selection>cd</selection>ef</p><p>ghi</p>',
  108. '<p>ab</p><p><selection />ef</p><p>ghi</p>',
  109. { defaultBlockName: 'p' }
  110. );
  111. test(
  112. 'deletes text and splits (other than default)',
  113. '<h>ab<selection>cd</selection>ef</h>',
  114. '<h>ab</h><h><selection />ef</h>',
  115. { defaultBlockName: 'p' }
  116. );
  117. test(
  118. 'places selection in the 2nd element',
  119. '<h>ab<selection>c</h><p>d</selection>ef</p><p>ghi</p>',
  120. '<h>ab</h><p><selection />ef</p><p>ghi</p>',
  121. { defaultBlockName: 'p' }
  122. );
  123. test(
  124. 'leaves one empty element after one was fully selected',
  125. '<p>x</p><p><selection>abcdef</selection></p><p>y</p>',
  126. '<p>x</p><p><selection /></p><p>y</p>',
  127. { defaultBlockName: 'p' }
  128. );
  129. test(
  130. 'leaves one (default) empty element after one was fully selected',
  131. '<h><selection>abcdef</selection></h>',
  132. '<p><selection /></p>',
  133. { defaultBlockName: 'p' }
  134. );
  135. test(
  136. 'leaves one (default) empty element after two were fully selected',
  137. '<h><selection>abc</h><p>def</selection></p>',
  138. '<p><selection /></p>',
  139. { defaultBlockName: 'p' }
  140. );
  141. test(
  142. 'leaves one (default) empty element after two were fully selected (backward)',
  143. '<h><selection backward>abc</h><p>def</selection></p>',
  144. '<p><selection /></p>',
  145. { defaultBlockName: 'p' }
  146. );
  147. it( 'uses composer.deleteContents', () => {
  148. const spy = sinon.spy();
  149. doc.composer.on( 'deleteContents', spy );
  150. setData( doc, '<p><selection>x</selection></p>' );
  151. enterBlock( doc.batch(), doc.selection, { defaultBlockName: 'p' } );
  152. expect( spy.calledOnce ).to.be.true;
  153. } );
  154. } );
  155. function test( title, input, output, options ) {
  156. it( title, () => {
  157. setData( doc, input );
  158. enterBlock( doc.batch(), doc.selection, options );
  159. expect( getData( doc ) ).to.equal( output );
  160. } );
  161. }
  162. } );