paragraphcommand.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ParagraphCommand from '../src/paragraphcommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'ParagraphCommand', () => {
  9. let editor, model, document, command, root, schema;
  10. beforeEach( () => {
  11. return ModelTestEditor.create().then( newEditor => {
  12. editor = newEditor;
  13. model = editor.model;
  14. document = model.document;
  15. schema = model.schema;
  16. command = new ParagraphCommand( editor );
  17. root = document.getRoot();
  18. editor.commands.add( 'paragraph', command );
  19. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  21. schema.register( 'notBlock' );
  22. schema.extend( 'notBlock', { allowIn: '$root' } );
  23. schema.extend( '$text', { allowIn: 'notBlock' } );
  24. } );
  25. } );
  26. afterEach( () => {
  27. command.destroy();
  28. } );
  29. describe( 'value', () => {
  30. it( 'responds to changes in selection (collapsed selection)', () => {
  31. setData( model, '<heading1>foo[]bar</heading1>' );
  32. expect( command.value ).to.be.false;
  33. setData( model, '<paragraph>foo[]bar</paragraph>' );
  34. expect( command.value ).to.be.true;
  35. } );
  36. it( 'responds to changes in selection (non–collapsed selection)', () => {
  37. setData( model, '<heading1>[foo]</heading1><paragraph>bar</paragraph>' );
  38. expect( command.value ).to.be.false;
  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.true;
  43. setData( model, '<heading1>foo</heading1><paragraph>[bar]</paragraph>' );
  44. expect( command.value ).to.be.true;
  45. setData( model, '<paragraph>[bar</paragraph><heading1>foo]</heading1>' );
  46. expect( command.value ).to.be.true;
  47. } );
  48. it( 'has proper value when inside non-block element', () => {
  49. setData( model, '<notBlock>[foo]</notBlock>' );
  50. expect( command.value ).to.be.false;
  51. } );
  52. it( 'has proper value when moved from block to element that is not a block', () => {
  53. setData( model, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  54. const element = document.getRoot().getChild( 1 );
  55. model.change( writer => {
  56. writer.setSelection( writer.createRangeIn( element ) );
  57. } );
  58. expect( command.value ).to.be.false;
  59. } );
  60. it( 'should be refreshed after calling refresh()', () => {
  61. setData( model, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
  62. const element = document.getRoot().getChild( 1 );
  63. model.change( writer => {
  64. writer.setSelection( writer.createRangeIn( element ) );
  65. expect( command.value ).to.be.true;
  66. command.refresh();
  67. expect( command.value ).to.be.false;
  68. } );
  69. } );
  70. } );
  71. describe( 'execute()', () => {
  72. it( 'should update value after execution', () => {
  73. setData( model, '<heading1>[]</heading1>' );
  74. command.execute();
  75. expect( getData( model ) ).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 (paragraph is not allowed in their parent)', () => {
  80. model.schema.register( 'restricted', { allowIn: '$root' } );
  81. model.schema.register( 'fooBlock', {
  82. inheritAllFrom: '$block',
  83. allowIn: 'restricted'
  84. } );
  85. model.schema.addChildCheck( ( ctx, childDef ) => {
  86. if ( ctx.endsWith( 'restricted' ) && childDef.name == 'paragraph' ) {
  87. return false;
  88. }
  89. } );
  90. setData(
  91. model,
  92. '<heading1>a[bc</heading1>' +
  93. '<restricted><fooBlock></fooBlock></restricted>' +
  94. '<heading1>de]f</heading1>'
  95. );
  96. command.execute();
  97. expect( getData( model ) ).to.equal(
  98. '<paragraph>a[bc</paragraph>' +
  99. '<restricted><fooBlock></fooBlock></restricted>' +
  100. '<paragraph>de]f</paragraph>'
  101. );
  102. } );
  103. it( 'should not rename blocks which cannot become paragraphs (block is an object)', () => {
  104. model.schema.register( 'image', {
  105. isBlock: true,
  106. isObject: true,
  107. allowIn: '$root'
  108. } );
  109. setData(
  110. model,
  111. '<heading1>a[bc</heading1>' +
  112. '<image></image>' +
  113. '<heading1>de]f</heading1>'
  114. );
  115. command.execute();
  116. expect( getData( model ) ).to.equal(
  117. '<paragraph>a[bc</paragraph>' +
  118. '<image></image>' +
  119. '<paragraph>de]f</paragraph>'
  120. );
  121. } );
  122. it( 'should not rename blocks which already are pargraphs', () => {
  123. setData( model, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
  124. model.change( writer => {
  125. expect( writer.batch.operations.length ).to.equal( 0 );
  126. command.execute();
  127. expect( writer.batch.operations.length ).to.equal( 1 );
  128. } );
  129. } );
  130. describe( 'custom options', () => {
  131. it( 'should use parent batch', () => {
  132. setData( model, '<heading1>foo[]bar</heading1>' );
  133. model.change( writer => {
  134. expect( writer.batch.operations.length ).to.equal( 0 );
  135. command.execute();
  136. expect( writer.batch.operations.length ).to.above( 0 );
  137. } );
  138. } );
  139. it( 'should use provided selection', () => {
  140. setData( model, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  141. const secondToLastHeading = root.getChild( 1 );
  142. const lastHeading = root.getChild( 2 );
  143. const selection = model.createSelection( model.createRange(
  144. model.createPositionAt( secondToLastHeading, 0 ),
  145. model.createPositionAt( lastHeading, 1 )
  146. ) );
  147. command.execute( { selection } );
  148. expect( getData( model ) ).to.equal(
  149. '<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>'
  150. );
  151. } );
  152. } );
  153. describe( 'collapsed selection', () => {
  154. it( 'does nothing when executed with already applied', () => {
  155. setData( model, '<paragraph>foo[]bar</paragraph>' );
  156. command.execute();
  157. expect( getData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  158. } );
  159. it( 'converts topmost blocks', () => {
  160. schema.register( 'inlineImage', { allowWhere: '$text' } );
  161. schema.extend( '$text', { allowIn: 'inlineImage' } );
  162. setData( model, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  163. command.execute();
  164. expect( getData( model ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  165. } );
  166. } );
  167. describe( 'non-collapsed selection', () => {
  168. it( 'converts all elements where selection is applied', () => {
  169. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  170. setData( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>baz]</heading2>' );
  171. command.execute();
  172. expect( getData( model ) ).to.equal(
  173. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>baz]</paragraph>'
  174. );
  175. } );
  176. it( 'converts all elements even if already anchored in paragraph', () => {
  177. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  178. setData( model, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );
  179. command.execute();
  180. expect( getData( model ) ).to.equal( '<paragraph>foo[</paragraph><paragraph>bar]</paragraph>' );
  181. } );
  182. } );
  183. } );
  184. describe( 'isEnabled', () => {
  185. it( 'should be enabled when inside another block', () => {
  186. setData( model, '<heading1>f{}oo</heading1>' );
  187. expect( command.isEnabled ).to.be.true;
  188. } );
  189. it( 'should be disabled if inside non-block', () => {
  190. setData( model, '<notBlock>f{}oo</notBlock>' );
  191. expect( command.isEnabled ).to.be.false;
  192. } );
  193. it( 'should be disabled if selection is placed on non-block element', () => {
  194. setData( model, '[<notBlock>foo</notBlock>]' );
  195. expect( command.isEnabled ).to.be.false;
  196. } );
  197. } );
  198. } );