paragraphcommand.js 7.8 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( writer => {
  58. writer.setSelection( 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. model.change( writer => {
  66. writer.setSelection( Range.createIn( element ) );
  67. expect( command.value ).to.be.true;
  68. command.refresh();
  69. expect( command.value ).to.be.false;
  70. } );
  71. } );
  72. } );
  73. describe( 'execute()', () => {
  74. it( 'should update value after execution', () => {
  75. setData( model, '<heading1>[]</heading1>' );
  76. command.execute();
  77. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  78. expect( command.value ).to.be.true;
  79. } );
  80. // https://github.com/ckeditor/ckeditor5-paragraph/issues/24
  81. it( 'should not rename blocks which cannot become paragraphs (paragraph is not allowed in their parent)', () => {
  82. model.schema.register( 'restricted', { allowIn: '$root' } );
  83. model.schema.register( 'fooBlock', {
  84. inheritAllFrom: '$block',
  85. allowIn: 'restricted'
  86. } );
  87. model.schema.addChildCheck( ( ctx, childDef ) => {
  88. if ( ctx.endsWith( 'restricted' ) && childDef.name == 'paragraph' ) {
  89. return false;
  90. }
  91. } );
  92. setData(
  93. model,
  94. '<heading1>a[bc</heading1>' +
  95. '<restricted><fooBlock></fooBlock></restricted>' +
  96. '<heading1>de]f</heading1>'
  97. );
  98. command.execute();
  99. expect( getData( model ) ).to.equal(
  100. '<paragraph>a[bc</paragraph>' +
  101. '<restricted><fooBlock></fooBlock></restricted>' +
  102. '<paragraph>de]f</paragraph>'
  103. );
  104. } );
  105. it( 'should not rename blocks which cannot become paragraphs (block is an object)', () => {
  106. model.schema.register( 'image', {
  107. isBlock: true,
  108. isObject: true,
  109. allowIn: '$root'
  110. } );
  111. setData(
  112. model,
  113. '<heading1>a[bc</heading1>' +
  114. '<image></image>' +
  115. '<heading1>de]f</heading1>'
  116. );
  117. command.execute();
  118. expect( getData( model ) ).to.equal(
  119. '<paragraph>a[bc</paragraph>' +
  120. '<image></image>' +
  121. '<paragraph>de]f</paragraph>'
  122. );
  123. } );
  124. it( 'should not rename blocks which already are pargraphs', () => {
  125. setData( model, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
  126. model.change( writer => {
  127. expect( writer.batch.deltas.length ).to.equal( 0 );
  128. command.execute();
  129. expect( writer.batch.deltas.length ).to.equal( 1 );
  130. } );
  131. } );
  132. describe( 'custom options', () => {
  133. it( 'should use parent batch', () => {
  134. setData( model, '<heading1>foo[]bar</heading1>' );
  135. model.change( writer => {
  136. expect( writer.batch.deltas.length ).to.equal( 0 );
  137. command.execute();
  138. expect( writer.batch.deltas.length ).to.above( 0 );
  139. } );
  140. } );
  141. it( 'should use provided selection', () => {
  142. setData( model, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  143. const secondToLastHeading = root.getChild( 1 );
  144. const lastHeading = root.getChild( 2 );
  145. const selection = new Selection( [ 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. } );