8
0

paragraphcommand.js 8.0 KB

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