headingcommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. { model: 'heading1', view: { name: 'h2' }, title: 'H2' },
  12. { model: 'heading2', view: { name: 'h3' }, title: 'H3' },
  13. { model: 'heading3', view: { name: '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.add( 'paragraph', new ParagraphCommand( editor ) );
  24. schema.registerItem( 'paragraph', '$block' );
  25. for ( const option of options ) {
  26. commands[ option.model ] = new HeadingCommand( editor, option.model );
  27. schema.registerItem( option.model, '$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( { model: 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 refresh()', () => {
  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.refresh();
  76. expect( command.value ).to.be.false;
  77. } );
  78. }
  79. } );
  80. describe( 'execute()', () => {
  81. it( 'should update value after execution', () => {
  82. const command = commands.heading1;
  83. setData( document, '<paragraph>[]</paragraph>' );
  84. command.execute();
  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.execute();
  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.execute( { batch } );
  115. expect( batch.deltas.length ).to.be.above( 0 );
  116. } );
  117. } );
  118. describe( 'collapsed selection', () => {
  119. let convertTo = options[ options.length - 1 ];
  120. for ( const option of options ) {
  121. test( option, convertTo );
  122. convertTo = option;
  123. }
  124. it( 'does nothing when executed with already applied option', () => {
  125. setData( document, '<heading1>foo[]bar</heading1>' );
  126. commands.heading1.execute();
  127. expect( getData( document ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  128. } );
  129. it( 'converts topmost blocks', () => {
  130. schema.registerItem( 'inlineImage', '$inline' );
  131. schema.allow( { name: '$text', inside: 'inlineImage' } );
  132. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  133. commands.heading1.execute();
  134. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  135. } );
  136. function test( from, to ) {
  137. it( `converts ${ from.model } to ${ to.model } on collapsed selection`, () => {
  138. setData( document, `<${ from.model }>foo[]bar</${ from.model }>` );
  139. commands[ to.model ].execute();
  140. expect( getData( document ) ).to.equal( `<${ to.model }>foo[]bar</${ to.model }>` );
  141. } );
  142. }
  143. } );
  144. describe( 'non-collapsed selection', () => {
  145. let convertTo = options[ options.length - 1 ];
  146. for ( const option of options ) {
  147. test( option, convertTo );
  148. convertTo = option;
  149. }
  150. it( 'converts all elements where selection is applied', () => {
  151. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>baz]</heading3>' );
  152. commands.heading3.execute();
  153. expect( getData( document ) ).to.equal(
  154. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>baz]</heading3>'
  155. );
  156. } );
  157. it( 'does nothing to the elements with same option (#1)', () => {
  158. setData( document, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  159. commands.heading1.execute();
  160. expect( getData( document ) ).to.equal(
  161. '<heading1>[foo</heading1><heading1>bar]</heading1>'
  162. );
  163. } );
  164. it( 'does nothing to the elements with same option (#2)', () => {
  165. setData( document, '<heading1>[foo</heading1><heading1>bar</heading1><heading2>baz]</heading2>' );
  166. commands.heading1.execute();
  167. expect( getData( document ) ).to.equal(
  168. '<heading1>[foo</heading1><heading1>bar</heading1><heading1>baz]</heading1>'
  169. );
  170. } );
  171. function test( { model: fromElement }, { model: toElement } ) {
  172. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  173. setData(
  174. document,
  175. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  176. );
  177. commands[ toElement ].execute();
  178. expect( getData( document ) ).to.equal(
  179. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  180. );
  181. } );
  182. }
  183. } );
  184. } );
  185. describe( 'isEnabled', () => {
  186. for ( const option of options ) {
  187. test( option.model );
  188. }
  189. function test( modelElement ) {
  190. let command;
  191. beforeEach( () => {
  192. command = commands[ modelElement ];
  193. } );
  194. describe( `${ modelElement } command`, () => {
  195. it( 'should be enabled when inside another block', () => {
  196. setData( document, '<paragraph>f{}oo</paragraph>' );
  197. expect( command.isEnabled ).to.be.true;
  198. } );
  199. it( 'should be disabled if inside non-block', () => {
  200. setData( document, '<notBlock>f{}oo</notBlock>' );
  201. expect( command.isEnabled ).to.be.false;
  202. } );
  203. it( 'should be disabled if selection is placed on non-block', () => {
  204. setData( document, '[<notBlock>foo</notBlock>]' );
  205. expect( command.isEnabled ).to.be.false;
  206. } );
  207. } );
  208. }
  209. } );
  210. } );