paragraphcommand.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 ParagraphCommand from '../src/paragraphcommand';
  7. import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
  8. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'ParagraphCommand', () => {
  11. let editor, document, command, root, schema;
  12. beforeEach( () => {
  13. return ModelTestEditor.create().then( newEditor => {
  14. editor = newEditor;
  15. document = editor.document;
  16. schema = document.schema;
  17. command = new ParagraphCommand( editor );
  18. root = document.getRoot();
  19. editor.commands.add( 'paragraph', command );
  20. schema.registerItem( 'paragraph', '$block' );
  21. schema.registerItem( 'heading1', '$block' );
  22. schema.registerItem( 'notBlock' );
  23. schema.allow( { name: 'notBlock', inside: '$root' } );
  24. schema.allow( { name: '$text', inside: 'notBlock' } );
  25. } );
  26. } );
  27. afterEach( () => {
  28. command.destroy();
  29. } );
  30. describe( 'value', () => {
  31. it( 'responds to changes in selection (collapsed selection)', () => {
  32. setData( document, '<heading1>foo[]bar</heading1>' );
  33. expect( command.value ).to.be.false;
  34. setData( document, '<paragraph>foo[]bar</paragraph>' );
  35. expect( command.value ).to.be.true;
  36. } );
  37. it( 'responds to changes in selection (non–collapsed selection)', () => {
  38. setData( document, '<heading1>[foo]</heading1><paragraph>bar</paragraph>' );
  39. expect( command.value ).to.be.false;
  40. setData( document, '<heading1>[foo</heading1><paragraph>bar]</paragraph>' );
  41. expect( command.value ).to.be.false;
  42. setData( document, '<heading1>foo</heading1>[<paragraph>bar]</paragraph>' );
  43. expect( command.value ).to.be.true;
  44. setData( document, '<heading1>foo</heading1><paragraph>[bar]</paragraph>' );
  45. expect( command.value ).to.be.true;
  46. setData( document, '<paragraph>[bar</paragraph><heading1>foo]</heading1>' );
  47. expect( command.value ).to.be.true;
  48. } );
  49. it( 'has proper value when inside non-block element', () => {
  50. setData( document, '<notBlock>[foo]</notBlock>' );
  51. expect( command.value ).to.be.false;
  52. } );
  53. it( 'has proper value when moved from block to element that is not a block', () => {
  54. setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  55. const element = document.getRoot().getChild( 1 );
  56. document.enqueueChanges( () => {
  57. document.selection.setRanges( [ Range.createIn( element ) ] );
  58. } );
  59. expect( command.value ).to.be.false;
  60. } );
  61. it( 'should be refreshed after calling refresh()', () => {
  62. setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  63. const element = document.getRoot().getChild( 1 );
  64. // Purposely not putting it in `document.enqueueChanges` to update command manually.
  65. document.selection.setRanges( [ Range.createIn( element ) ] );
  66. expect( command.value ).to.be.true;
  67. command.refresh();
  68. expect( command.value ).to.be.false;
  69. } );
  70. } );
  71. describe( 'execute()', () => {
  72. it( 'should update value after execution', () => {
  73. setData( document, '<heading1>[]</heading1>' );
  74. command.execute();
  75. expect( getData( document ) ).to.equal( '<paragraph>[]</paragraph>' );
  76. expect( command.value ).to.be.true;
  77. } );
  78. // https://github.com/ckeditor/ckeditor5-paragraph/issues/24
  79. it( 'should not rename blocks which cannot become paragraphs', () => {
  80. document.schema.registerItem( 'restricted' );
  81. document.schema.allow( { name: 'restricted', inside: '$root' } );
  82. document.schema.disallow( { name: 'paragraph', inside: 'restricted' } );
  83. document.schema.registerItem( 'fooBlock', '$block' );
  84. document.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
  85. setData(
  86. document,
  87. '<heading1>a[bc</heading1>' +
  88. '<restricted><fooBlock></fooBlock></restricted>' +
  89. '<heading1>de]f</heading1>'
  90. );
  91. command.execute();
  92. expect( getData( document ) ).to.equal(
  93. '<paragraph>a[bc</paragraph>' +
  94. '<restricted><fooBlock></fooBlock></restricted>' +
  95. '<paragraph>de]f</paragraph>'
  96. );
  97. } );
  98. it( 'should not rename blocks which already are pargraphs', () => {
  99. const batch = editor.document.batch();
  100. setData( document, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
  101. expect( batch.deltas.length ).to.equal( 0 );
  102. command.execute( { batch } );
  103. expect( batch.deltas.length ).to.equal( 1 );
  104. } );
  105. describe( 'custom options', () => {
  106. it( 'should use provided batch', () => {
  107. const batch = editor.document.batch();
  108. setData( document, '<heading1>foo[]bar</heading1>' );
  109. expect( batch.deltas.length ).to.equal( 0 );
  110. command.execute( { batch } );
  111. expect( batch.deltas.length ).to.be.above( 0 );
  112. } );
  113. it( 'should use provided selection', () => {
  114. setData( document, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  115. const secondTolastHeading = root.getChild( 1 );
  116. const lastHeading = root.getChild( 2 );
  117. const selection = new Selection();
  118. selection.addRange( Range.createFromParentsAndOffsets( secondTolastHeading, 0, lastHeading, 0 ) );
  119. command.execute( { selection } );
  120. expect( getData( document ) ).to.equal(
  121. '<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>'
  122. );
  123. } );
  124. } );
  125. describe( 'collapsed selection', () => {
  126. it( 'does nothing when executed with already applied', () => {
  127. setData( document, '<paragraph>foo[]bar</paragraph>' );
  128. command.execute();
  129. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  130. } );
  131. it( 'converts topmost blocks', () => {
  132. schema.registerItem( 'inlineImage', '$inline' );
  133. schema.allow( { name: '$text', inside: 'inlineImage' } );
  134. setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  135. command.execute();
  136. expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  137. } );
  138. } );
  139. describe( 'non-collapsed selection', () => {
  140. it( 'converts all elements where selection is applied', () => {
  141. schema.registerItem( 'heading2', '$block' );
  142. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  143. command.execute();
  144. expect( getData( document ) ).to.equal(
  145. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
  146. );
  147. } );
  148. it( 'converts all elements even if already anchored in paragraph', () => {
  149. schema.registerItem( 'heading2', '$block' );
  150. setData( document, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
  151. command.execute();
  152. expect( getData( document ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
  153. } );
  154. } );
  155. } );
  156. describe( 'isEnabled', () => {
  157. it( 'should be enabled when inside another block', () => {
  158. setData( document, '<heading1>f{}oo</heading1>' );
  159. expect( command.isEnabled ).to.be.true;
  160. } );
  161. it( 'should be disabled if inside non-block', () => {
  162. setData( document, '<notBlock>f{}oo</notBlock>' );
  163. expect( command.isEnabled ).to.be.false;
  164. } );
  165. it( 'should be disabled if selection is placed on non-block element', () => {
  166. setData( document, '[<notBlock>foo</notBlock>]' );
  167. expect( command.isEnabled ).to.be.false;
  168. } );
  169. } );
  170. } );