headingcommand.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 '@ckeditor/ckeditor5-paragraph/src/paragraphcommand';
  7. import HeadingCommand from '../src/headingcommand';
  8. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. const options = [
  11. { modelElement: 'heading1', viewElement: 'h2', title: 'H2' },
  12. { modelElement: 'heading2', viewElement: 'h3', title: 'H3' },
  13. { modelElement: 'heading3', viewElement: 'h4', title: 'H4' }
  14. ];
  15. describe( 'HeadingCommand', () => {
  16. let editor, document, commands, root, schema;
  17. beforeEach( () => {
  18. return ModelTestEditor.create().then( newEditor => {
  19. editor = newEditor;
  20. document = editor.document;
  21. commands = {};
  22. schema = document.schema;
  23. editor.commands.set( 'paragraph', new ParagraphCommand( editor ) );
  24. schema.registerItem( 'paragraph', '$block' );
  25. for ( const option of options ) {
  26. commands[ option.modelElement ] = new HeadingCommand( editor, option );
  27. schema.registerItem( option.modelElement, '$block' );
  28. }
  29. schema.registerItem( 'notBlock' );
  30. schema.allow( { name: 'notBlock', inside: '$root' } );
  31. schema.allow( { name: '$text', inside: 'notBlock' } );
  32. root = document.getRoot();
  33. } );
  34. } );
  35. afterEach( () => {
  36. for ( const modelElement in commands ) {
  37. commands[ modelElement ].destroy();
  38. }
  39. } );
  40. describe( 'basic properties', () => {
  41. for ( const option of options ) {
  42. test( option );
  43. }
  44. function test( { modelElement, viewElement, title } ) {
  45. it( `are set for option.modelElement = ${ modelElement }`, () => {
  46. expect( commands[ modelElement ].modelElement ).to.equal( modelElement );
  47. expect( commands[ modelElement ].viewElement ).to.equal( viewElement );
  48. expect( commands[ modelElement ].title ).to.equal( title );
  49. } );
  50. }
  51. } );
  52. describe( 'value', () => {
  53. for ( const option of options ) {
  54. test( option );
  55. }
  56. function test( { modelElement } ) {
  57. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  58. setData( document, `<${ modelElement }>foobar</${ modelElement }>` );
  59. const element = root.getChild( 0 );
  60. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  61. expect( commands[ modelElement ].value ).to.be.true;
  62. } );
  63. it( 'equals false if inside to non-block element', () => {
  64. setData( document, '<notBlock>[foo]</notBlock>' );
  65. expect( commands[ modelElement ].value ).to.be.false;
  66. } );
  67. it( `equals false if moved from ${ modelElement } to non-block element`, () => {
  68. setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  69. const element = document.getRoot().getChild( 1 );
  70. document.enqueueChanges( () => {
  71. document.selection.setRanges( [ Range.createIn( element ) ] );
  72. } );
  73. expect( commands[ modelElement ].value ).to.be.false;
  74. } );
  75. it( 'should be refreshed after calling refreshValue()', () => {
  76. const command = commands[ modelElement ];
  77. setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  78. const element = document.getRoot().getChild( 1 );
  79. // Purposely not putting it in `document.enqueueChanges` to update command manually.
  80. document.selection.setRanges( [ Range.createIn( element ) ] );
  81. expect( command.value ).to.be.true;
  82. command.refreshValue();
  83. expect( command.value ).to.be.false;
  84. } );
  85. }
  86. } );
  87. describe( '_doExecute', () => {
  88. it( 'should update value after execution', () => {
  89. const command = commands.heading1;
  90. setData( document, '<paragraph>[]</paragraph>' );
  91. command._doExecute();
  92. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  93. expect( command.value ).to.be.true;
  94. } );
  95. describe( 'custom options', () => {
  96. it( 'should use provided batch', () => {
  97. const batch = editor.document.batch();
  98. const command = commands.heading1;
  99. setData( document, '<paragraph>foo[]bar</paragraph>' );
  100. expect( batch.deltas.length ).to.equal( 0 );
  101. command._doExecute( { batch } );
  102. expect( batch.deltas.length ).to.be.above( 0 );
  103. } );
  104. it( 'should use provided batch (converting to default option)', () => {
  105. const batch = editor.document.batch();
  106. const command = commands.heading1;
  107. setData( document, '<heading1>foo[]bar</heading1>' );
  108. expect( batch.deltas.length ).to.equal( 0 );
  109. command._doExecute( { batch } );
  110. expect( batch.deltas.length ).to.be.above( 0 );
  111. } );
  112. } );
  113. describe( 'collapsed selection', () => {
  114. let convertTo = options[ options.length - 1 ];
  115. for ( const option of options ) {
  116. test( option, convertTo );
  117. convertTo = option;
  118. }
  119. it( 'converts to default option when executed with already applied option', () => {
  120. const command = commands.heading1;
  121. setData( document, '<heading1>foo[]bar</heading1>' );
  122. command._doExecute();
  123. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  124. } );
  125. it( 'converts topmost blocks', () => {
  126. schema.registerItem( 'inlineImage', '$inline' );
  127. schema.allow( { name: '$text', inside: 'inlineImage' } );
  128. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  129. commands.heading1._doExecute();
  130. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  131. } );
  132. function test( from, to ) {
  133. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  134. setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  135. commands[ to.modelElement ]._doExecute();
  136. expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  137. } );
  138. }
  139. } );
  140. describe( 'non-collapsed selection', () => {
  141. let convertTo = options[ options.length - 1 ];
  142. for ( const option of options ) {
  143. test( option, convertTo );
  144. convertTo = option;
  145. }
  146. it( 'converts all elements where selection is applied', () => {
  147. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>]baz</heading3>' );
  148. commands.heading3._doExecute();
  149. expect( getData( document ) ).to.equal(
  150. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
  151. );
  152. } );
  153. it( 'resets to default value all elements with same option', () => {
  154. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  155. commands.heading1._doExecute();
  156. expect( getData( document ) ).to.equal(
  157. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  158. );
  159. } );
  160. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  161. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  162. setData(
  163. document,
  164. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  165. );
  166. commands[ toElement ]._doExecute();
  167. expect( getData( document ) ).to.equal(
  168. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  169. );
  170. } );
  171. }
  172. } );
  173. } );
  174. describe( 'isEnabled', () => {
  175. for ( const option of options ) {
  176. test( option.modelElement );
  177. }
  178. function test( modelElement ) {
  179. let command;
  180. beforeEach( () => {
  181. command = commands[ modelElement ];
  182. } );
  183. describe( `${ modelElement } command`, () => {
  184. it( 'should be enabled when inside another block', () => {
  185. setData( document, '<paragraph>f{}oo</paragraph>' );
  186. expect( command.isEnabled ).to.be.true;
  187. } );
  188. it( 'should be disabled if inside non-block', () => {
  189. setData( document, '<notBlock>f{}oo</notBlock>' );
  190. expect( command.isEnabled ).to.be.false;
  191. } );
  192. it( 'should be disabled if selection is placed on non-block', () => {
  193. setData( document, '[<notBlock>foo</notBlock>]' );
  194. expect( command.isEnabled ).to.be.false;
  195. } );
  196. } );
  197. }
  198. } );
  199. } );