entercommand.js 5.1 KB

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