paragraphcommand.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license Copyright (c) 2003-2018, 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.register( 'paragraph', { inheritAllFrom: '$block' } );
  22. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  23. schema.register( 'notBlock' );
  24. schema.extend( 'notBlock', { allowIn: '$root' } );
  25. schema.extend( '$text', { allowIn: '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 (paragraph is not allowed in their parent)', () => {
  81. model.schema.register( 'restricted', { allowIn: '$root' } );
  82. model.schema.register( 'fooBlock', {
  83. inheritAllFrom: '$block',
  84. allowIn: 'restricted'
  85. } );
  86. model.schema.addChildCheck( ( ctx, childDef ) => {
  87. if ( ctx.endsWith( 'restricted' ) && childDef.name == 'paragraph' ) {
  88. return false;
  89. }
  90. } );
  91. setData(
  92. model,
  93. '<heading1>a[bc</heading1>' +
  94. '<restricted><fooBlock></fooBlock></restricted>' +
  95. '<heading1>de]f</heading1>'
  96. );
  97. command.execute();
  98. expect( getData( model ) ).to.equal(
  99. '<paragraph>a[bc</paragraph>' +
  100. '<restricted><fooBlock></fooBlock></restricted>' +
  101. '<paragraph>de]f</paragraph>'
  102. );
  103. } );
  104. it( 'should not rename blocks which cannot become paragraphs (block is an object)', () => {
  105. model.schema.register( 'image', {
  106. isBlock: true,
  107. isObject: true,
  108. allowIn: '$root'
  109. } );
  110. setData(
  111. model,
  112. '<heading1>a[bc</heading1>' +
  113. '<image></image>' +
  114. '<heading1>de]f</heading1>'
  115. );
  116. command.execute();
  117. expect( getData( model ) ).to.equal(
  118. '<paragraph>a[bc</paragraph>' +
  119. '<image></image>' +
  120. '<paragraph>de]f</paragraph>'
  121. );
  122. } );
  123. it( 'should not rename blocks which already are pargraphs', () => {
  124. setData( model, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
  125. model.change( writer => {
  126. expect( writer.batch.deltas.length ).to.equal( 0 );
  127. command.execute();
  128. expect( writer.batch.deltas.length ).to.equal( 1 );
  129. } );
  130. } );
  131. describe( 'custom options', () => {
  132. it( 'should use parent batch', () => {
  133. setData( model, '<heading1>foo[]bar</heading1>' );
  134. model.change( writer => {
  135. expect( writer.batch.deltas.length ).to.equal( 0 );
  136. command.execute();
  137. expect( writer.batch.deltas.length ).to.above( 0 );
  138. } );
  139. } );
  140. it( 'should use provided selection', () => {
  141. setData( model, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  142. const secondToLastHeading = root.getChild( 1 );
  143. const lastHeading = root.getChild( 2 );
  144. const selection = new Selection();
  145. selection.addRange( Range.createFromParentsAndOffsets( secondToLastHeading, 0, lastHeading, 1 ) );
  146. command.execute( { selection } );
  147. expect( getData( model ) ).to.equal(
  148. '<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>'
  149. );
  150. } );
  151. } );
  152. describe( 'collapsed selection', () => {
  153. it( 'does nothing when executed with already applied', () => {
  154. setData( model, '<paragraph>foo[]bar</paragraph>' );
  155. command.execute();
  156. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  157. } );
  158. it( 'converts topmost blocks', () => {
  159. schema.register( 'inlineImage', { allowWhere: '$text' } );
  160. schema.extend( '$text', { allowIn: 'inlineImage' } );
  161. setData( model, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  162. command.execute();
  163. expect( getData( model ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  164. } );
  165. } );
  166. describe( 'non-collapsed selection', () => {
  167. it( 'converts all elements where selection is applied', () => {
  168. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  169. setData( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>baz]</heading2>' );
  170. command.execute();
  171. expect( getData( model ) ).to.equal(
  172. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>baz]</paragraph>'
  173. );
  174. } );
  175. it( 'converts all elements even if already anchored in paragraph', () => {
  176. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  177. setData( model, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
  178. command.execute();
  179. expect( getData( model ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
  180. } );
  181. } );
  182. } );
  183. describe( 'isEnabled', () => {
  184. it( 'should be enabled when inside another block', () => {
  185. setData( model, '<heading1>f{}oo</heading1>' );
  186. expect( command.isEnabled ).to.be.true;
  187. } );
  188. it( 'should be disabled if inside non-block', () => {
  189. setData( model, '<notBlock>f{}oo</notBlock>' );
  190. expect( command.isEnabled ).to.be.false;
  191. } );
  192. it( 'should be disabled if selection is placed on non-block element', () => {
  193. setData( model, '[<notBlock>foo</notBlock>]' );
  194. expect( command.isEnabled ).to.be.false;
  195. } );
  196. } );
  197. } );