8
0

paragraphcommand.js 7.3 KB

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