headingcommand.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.modelElement );
  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( 'modelElement', () => {
  41. it( 'is set', () => {
  42. expect( commands.heading1.modelElement ).to.equal( 'heading1' );
  43. } );
  44. } );
  45. describe( 'value', () => {
  46. for ( const option of options ) {
  47. test( option );
  48. }
  49. function test( { modelElement } ) {
  50. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  51. setData( document, `<${ modelElement }>foobar</${ modelElement }>` );
  52. const element = root.getChild( 0 );
  53. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  54. expect( commands[ modelElement ].value ).to.be.true;
  55. } );
  56. it( 'equals false if inside to non-block element', () => {
  57. setData( document, '<notBlock>[foo]</notBlock>' );
  58. expect( commands[ modelElement ].value ).to.be.false;
  59. } );
  60. it( `equals false if moved from ${ modelElement } to non-block element`, () => {
  61. setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  62. const element = document.getRoot().getChild( 1 );
  63. document.enqueueChanges( () => {
  64. document.selection.setRanges( [ Range.createIn( element ) ] );
  65. } );
  66. expect( commands[ modelElement ].value ).to.be.false;
  67. } );
  68. it( 'should be refreshed after calling refreshValue()', () => {
  69. const command = commands[ modelElement ];
  70. setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  71. const element = document.getRoot().getChild( 1 );
  72. // Purposely not putting it in `document.enqueueChanges` to update command manually.
  73. document.selection.setRanges( [ Range.createIn( element ) ] );
  74. expect( command.value ).to.be.true;
  75. command.refreshValue();
  76. expect( command.value ).to.be.false;
  77. } );
  78. }
  79. } );
  80. describe( '_doExecute', () => {
  81. it( 'should update value after execution', () => {
  82. const command = commands.heading1;
  83. setData( document, '<paragraph>[]</paragraph>' );
  84. command._doExecute();
  85. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  86. expect( command.value ).to.be.true;
  87. } );
  88. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  89. it( 'should not rename blocks which cannot become headings', () => {
  90. document.schema.registerItem( 'restricted' );
  91. document.schema.allow( { name: 'restricted', inside: '$root' } );
  92. document.schema.disallow( { name: 'heading1', inside: 'restricted' } );
  93. document.schema.registerItem( 'fooBlock', '$block' );
  94. document.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
  95. setData(
  96. document,
  97. '<paragraph>a[bc</paragraph>' +
  98. '<restricted><fooBlock></fooBlock></restricted>' +
  99. '<paragraph>de]f</paragraph>'
  100. );
  101. commands.heading1._doExecute();
  102. expect( getData( document ) ).to.equal(
  103. '<heading1>a[bc</heading1>' +
  104. '<restricted><fooBlock></fooBlock></restricted>' +
  105. '<heading1>de]f</heading1>'
  106. );
  107. } );
  108. describe( 'custom options', () => {
  109. it( 'should use provided batch', () => {
  110. const batch = editor.document.batch();
  111. const command = commands.heading1;
  112. setData( document, '<paragraph>foo[]bar</paragraph>' );
  113. expect( batch.deltas.length ).to.equal( 0 );
  114. command._doExecute( { batch } );
  115. expect( batch.deltas.length ).to.be.above( 0 );
  116. } );
  117. it( 'should use provided batch (converting to default option)', () => {
  118. const batch = editor.document.batch();
  119. const command = commands.heading1;
  120. setData( document, '<heading1>foo[]bar</heading1>' );
  121. expect( batch.deltas.length ).to.equal( 0 );
  122. command._doExecute( { batch } );
  123. expect( batch.deltas.length ).to.be.above( 0 );
  124. } );
  125. } );
  126. describe( 'collapsed selection', () => {
  127. let convertTo = options[ options.length - 1 ];
  128. for ( const option of options ) {
  129. test( option, convertTo );
  130. convertTo = option;
  131. }
  132. it( 'converts to default option when executed with already applied option', () => {
  133. const command = commands.heading1;
  134. setData( document, '<heading1>foo[]bar</heading1>' );
  135. command._doExecute();
  136. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  137. } );
  138. it( 'converts topmost blocks', () => {
  139. schema.registerItem( 'inlineImage', '$inline' );
  140. schema.allow( { name: '$text', inside: 'inlineImage' } );
  141. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  142. commands.heading1._doExecute();
  143. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  144. } );
  145. function test( from, to ) {
  146. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  147. setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  148. commands[ to.modelElement ]._doExecute();
  149. expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  150. } );
  151. }
  152. } );
  153. describe( 'non-collapsed selection', () => {
  154. let convertTo = options[ options.length - 1 ];
  155. for ( const option of options ) {
  156. test( option, convertTo );
  157. convertTo = option;
  158. }
  159. it( 'converts all elements where selection is applied', () => {
  160. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>]baz</heading3>' );
  161. commands.heading3._doExecute();
  162. expect( getData( document ) ).to.equal(
  163. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
  164. );
  165. } );
  166. it( 'resets to default value all elements with same option', () => {
  167. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  168. commands.heading1._doExecute();
  169. expect( getData( document ) ).to.equal(
  170. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  171. );
  172. } );
  173. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  174. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  175. setData(
  176. document,
  177. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  178. );
  179. commands[ toElement ]._doExecute();
  180. expect( getData( document ) ).to.equal(
  181. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  182. );
  183. } );
  184. }
  185. } );
  186. } );
  187. describe( 'isEnabled', () => {
  188. for ( const option of options ) {
  189. test( option.modelElement );
  190. }
  191. function test( modelElement ) {
  192. let command;
  193. beforeEach( () => {
  194. command = commands[ modelElement ];
  195. } );
  196. describe( `${ modelElement } command`, () => {
  197. it( 'should be enabled when inside another block', () => {
  198. setData( document, '<paragraph>f{}oo</paragraph>' );
  199. expect( command.isEnabled ).to.be.true;
  200. } );
  201. it( 'should be disabled if inside non-block', () => {
  202. setData( document, '<notBlock>f{}oo</notBlock>' );
  203. expect( command.isEnabled ).to.be.false;
  204. } );
  205. it( 'should be disabled if selection is placed on non-block', () => {
  206. setData( document, '[<notBlock>foo</notBlock>]' );
  207. expect( command.isEnabled ).to.be.false;
  208. } );
  209. } );
  210. }
  211. } );
  212. } );