paragraphcommand.js 6.4 KB

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