headingcommand.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 ( let 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. schema.registerItem( 'object' );
  33. schema.allow( { name: 'object', inside: '$root' } );
  34. schema.allow( { name: '$text', inside: 'object' } );
  35. schema.objects.add( 'object' );
  36. root = document.getRoot();
  37. } );
  38. } );
  39. afterEach( () => {
  40. for ( let modelElement in commands ) {
  41. commands[ modelElement ].destroy();
  42. }
  43. } );
  44. describe( 'basic properties', () => {
  45. for ( let option of options ) {
  46. test( option );
  47. }
  48. function test( { modelElement, viewElement, title } ) {
  49. it( `are set for option.modelElement = ${ modelElement }`, () => {
  50. expect( commands[ modelElement ].modelElement ).to.equal( modelElement );
  51. expect( commands[ modelElement ].viewElement ).to.equal( viewElement );
  52. expect( commands[ modelElement ].title ).to.equal( title );
  53. } );
  54. }
  55. } );
  56. describe( 'value', () => {
  57. for ( let option of options ) {
  58. test( option );
  59. }
  60. function test( { modelElement } ) {
  61. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  62. setData( document, `<${ modelElement }>foobar</${ modelElement }>` );
  63. const element = root.getChild( 0 );
  64. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  65. expect( commands[ modelElement ].value ).to.be.true;
  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. expect( commands[ modelElement ].value ).to.be.true;
  71. document.enqueueChanges( () => {
  72. document.selection.setRanges( [ Range.createIn( element ) ] );
  73. } );
  74. expect( commands[ modelElement ].value ).to.be.false;
  75. } );
  76. }
  77. } );
  78. describe( '_doExecute', () => {
  79. it( 'should update value after execution', () => {
  80. const command = commands.heading1;
  81. setData( document, '<paragraph>[]</paragraph>' );
  82. command._doExecute();
  83. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  84. expect( command.value ).to.be.true;
  85. } );
  86. describe( 'custom options', () => {
  87. it( 'should use provided batch', () => {
  88. const batch = editor.document.batch();
  89. const command = commands.heading1;
  90. setData( document, '<paragraph>foo[]bar</paragraph>' );
  91. expect( batch.deltas.length ).to.equal( 0 );
  92. command._doExecute( { batch } );
  93. expect( batch.deltas.length ).to.be.above( 0 );
  94. } );
  95. it( 'should use provided batch (converting to default option)', () => {
  96. const batch = editor.document.batch();
  97. const command = commands.heading1;
  98. setData( document, '<heading1>foo[]bar</heading1>' );
  99. expect( batch.deltas.length ).to.equal( 0 );
  100. command._doExecute( { batch } );
  101. expect( batch.deltas.length ).to.be.above( 0 );
  102. } );
  103. } );
  104. describe( 'collapsed selection', () => {
  105. let convertTo = options[ options.length - 1 ];
  106. for ( let option of options ) {
  107. test( option, convertTo );
  108. convertTo = option;
  109. }
  110. it( 'converts to default option when executed with already applied option', () => {
  111. const command = commands.heading1;
  112. setData( document, '<heading1>foo[]bar</heading1>' );
  113. command._doExecute();
  114. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  115. } );
  116. it( 'converts topmost blocks', () => {
  117. schema.registerItem( 'inlineImage', '$inline' );
  118. schema.allow( { name: '$text', inside: 'inlineImage' } );
  119. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  120. commands.heading1._doExecute();
  121. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  122. } );
  123. function test( from, to ) {
  124. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  125. setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  126. commands[ to.modelElement ]._doExecute();
  127. expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  128. } );
  129. }
  130. } );
  131. describe( 'non-collapsed selection', () => {
  132. let convertTo = options[ options.length - 1 ];
  133. for ( let option of options ) {
  134. test( option, convertTo );
  135. convertTo = option;
  136. }
  137. it( 'converts all elements where selection is applied', () => {
  138. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>]baz</heading3>' );
  139. commands.heading3._doExecute();
  140. expect( getData( document ) ).to.equal(
  141. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
  142. );
  143. } );
  144. it( 'resets to default value all elements with same option', () => {
  145. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  146. commands.heading1._doExecute();
  147. expect( getData( document ) ).to.equal(
  148. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  149. );
  150. } );
  151. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  152. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  153. setData(
  154. document,
  155. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  156. );
  157. commands[ toElement ]._doExecute();
  158. expect( getData( document ) ).to.equal(
  159. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  160. );
  161. } );
  162. }
  163. } );
  164. } );
  165. describe( 'isEnabled', () => {
  166. for ( let option of options ) {
  167. test( option.modelElement );
  168. }
  169. function test( modelElement ) {
  170. let command;
  171. beforeEach( () => {
  172. command = commands[ modelElement ];
  173. } );
  174. describe( `${ modelElement } command`, () => {
  175. it( 'should be enabled when inside another block', () => {
  176. setData( document, '<paragraph>f{}oo</paragraph>' );
  177. expect( command.isEnabled ).to.be.true;
  178. } );
  179. it( 'should be disabled if inside non-block', () => {
  180. setData( document, '<notBlock>f{}oo</notBlock>' );
  181. expect( command.isEnabled ).to.be.false;
  182. } );
  183. it( 'should be disabled if inside object', () => {
  184. setData( document, '<object>f{}oo</object>' );
  185. expect( command.isEnabled ).to.be.false;
  186. } );
  187. it( 'should be disabled if selection is placed on object', () => {
  188. setData( document, '[<object>foo</object>]' );
  189. expect( command.isEnabled ).to.be.false;
  190. } );
  191. } );
  192. }
  193. } );
  194. } );