paragraphcommand.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.set( '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( 'has default value', () => {
  32. expect( command.value ).to.be.false;
  33. } );
  34. it( 'responds to changes in selection (collapsed selection)', () => {
  35. setData( document, '<heading1>foo[]bar</heading1>' );
  36. expect( command.value ).to.be.false;
  37. setData( document, '<paragraph>foo[]bar</paragraph>' );
  38. expect( command.value ).to.be.true;
  39. } );
  40. it( 'responds to changes in selection (non–collapsed selection)', () => {
  41. setData( document, '<heading1>[foo]</heading1><paragraph>bar</paragraph>' );
  42. expect( command.value ).to.be.false;
  43. setData( document, '<heading1>[foo</heading1><paragraph>bar]</paragraph>' );
  44. expect( command.value ).to.be.false;
  45. setData( document, '<heading1>foo</heading1>[<paragraph>bar]</paragraph>' );
  46. expect( command.value ).to.be.true;
  47. setData( document, '<heading1>foo</heading1><paragraph>[bar]</paragraph>' );
  48. expect( command.value ).to.be.true;
  49. setData( document, '<paragraph>[bar</paragraph><heading1>foo]</heading1>' );
  50. expect( command.value ).to.be.true;
  51. } );
  52. it( 'has proper value when inside non-block element', () => {
  53. setData( document, '<notBlock>[foo]</notBlock>' );
  54. expect( command.value ).to.be.false;
  55. } );
  56. it( 'has proper value when moved from block to element that is not a block', () => {
  57. setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  58. const element = document.getRoot().getChild( 1 );
  59. document.enqueueChanges( () => {
  60. document.selection.setRanges( [ Range.createIn( element ) ] );
  61. } );
  62. expect( command.value ).to.be.false;
  63. } );
  64. it( 'should be refreshed after calling refreshValue()', () => {
  65. setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  66. const element = document.getRoot().getChild( 1 );
  67. // Purposely not putting it in `document.enqueueChanges` to update command manually.
  68. document.selection.setRanges( [ Range.createIn( element ) ] );
  69. expect( command.value ).to.be.true;
  70. command.refreshValue();
  71. expect( command.value ).to.be.false;
  72. } );
  73. } );
  74. describe( '_doExecute', () => {
  75. it( 'should update value after execution', () => {
  76. setData( document, '<heading1>[]</heading1>' );
  77. command._doExecute();
  78. expect( getData( document ) ).to.equal( '<paragraph>[]</paragraph>' );
  79. expect( command.value ).to.be.true;
  80. } );
  81. it( 'should not rename blocks which already are pargraphs', () => {
  82. const batch = editor.document.batch();
  83. setData( document, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
  84. expect( batch.deltas.length ).to.equal( 0 );
  85. command._doExecute( { batch } );
  86. expect( batch.deltas.length ).to.equal( 1 );
  87. } );
  88. describe( 'custom options', () => {
  89. it( 'should use provided batch', () => {
  90. const batch = editor.document.batch();
  91. setData( document, '<heading1>foo[]bar</heading1>' );
  92. expect( batch.deltas.length ).to.equal( 0 );
  93. command._doExecute( { batch } );
  94. expect( batch.deltas.length ).to.be.above( 0 );
  95. } );
  96. it( 'should use provided selection', () => {
  97. setData( document, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  98. const secondTolastHeading = root.getChild( 1 );
  99. const lastHeading = root.getChild( 2 );
  100. const selection = new Selection();
  101. selection.addRange( Range.createFromParentsAndOffsets( secondTolastHeading, 0, lastHeading, 0 ) );
  102. command._doExecute( { selection } );
  103. expect( getData( document ) ).to.equal(
  104. '<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>'
  105. );
  106. } );
  107. } );
  108. describe( 'collapsed selection', () => {
  109. it( 'does nothing when executed with already applied', () => {
  110. setData( document, '<paragraph>foo[]bar</paragraph>' );
  111. command._doExecute();
  112. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  113. } );
  114. it( 'converts topmost blocks', () => {
  115. schema.registerItem( 'inlineImage', '$inline' );
  116. schema.allow( { name: '$text', inside: 'inlineImage' } );
  117. setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  118. command._doExecute();
  119. expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  120. } );
  121. } );
  122. describe( 'non-collapsed selection', () => {
  123. it( 'converts all elements where selection is applied', () => {
  124. schema.registerItem( 'heading2', '$block' );
  125. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  126. command._doExecute();
  127. expect( getData( document ) ).to.equal(
  128. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
  129. );
  130. } );
  131. it( 'converts all elements even if already anchored in paragraph', () => {
  132. schema.registerItem( 'heading2', '$block' );
  133. setData( document, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
  134. command._doExecute();
  135. expect( getData( document ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
  136. } );
  137. } );
  138. } );
  139. describe( 'isEnabled', () => {
  140. it( 'should be enabled when inside another block', () => {
  141. setData( document, '<heading1>f{}oo</heading1>' );
  142. expect( command.isEnabled ).to.be.true;
  143. } );
  144. it( 'should be disabled if inside non-block', () => {
  145. setData( document, '<notBlock>f{}oo</notBlock>' );
  146. expect( command.isEnabled ).to.be.false;
  147. } );
  148. it( 'should be disabled if selection is placed on non-block element', () => {
  149. setData( document, '[<notBlock>foo</notBlock>]' );
  150. expect( command.isEnabled ).to.be.false;
  151. } );
  152. } );
  153. } );