headingcommand.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  96. it( 'should not rename blocks which cannot become headings', () => {
  97. document.schema.registerItem( 'restricted' );
  98. document.schema.allow( { name: 'restricted', inside: '$root' } );
  99. document.schema.disallow( { name: 'heading1', inside: 'restricted' } );
  100. document.schema.registerItem( 'fooBlock', '$block' );
  101. document.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
  102. setData(
  103. document,
  104. '<paragraph>a[bc</paragraph>' +
  105. '<restricted><fooBlock></fooBlock></restricted>' +
  106. '<paragraph>de]f</paragraph>'
  107. );
  108. commands.heading1._doExecute();
  109. expect( getData( document ) ).to.equal(
  110. '<heading1>a[bc</heading1>' +
  111. '<restricted><fooBlock></fooBlock></restricted>' +
  112. '<heading1>de]f</heading1>'
  113. );
  114. } );
  115. describe( 'custom options', () => {
  116. it( 'should use provided batch', () => {
  117. const batch = editor.document.batch();
  118. const command = commands.heading1;
  119. setData( document, '<paragraph>foo[]bar</paragraph>' );
  120. expect( batch.deltas.length ).to.equal( 0 );
  121. command._doExecute( { batch } );
  122. expect( batch.deltas.length ).to.be.above( 0 );
  123. } );
  124. it( 'should use provided batch (converting to default option)', () => {
  125. const batch = editor.document.batch();
  126. const command = commands.heading1;
  127. setData( document, '<heading1>foo[]bar</heading1>' );
  128. expect( batch.deltas.length ).to.equal( 0 );
  129. command._doExecute( { batch } );
  130. expect( batch.deltas.length ).to.be.above( 0 );
  131. } );
  132. } );
  133. describe( 'collapsed selection', () => {
  134. let convertTo = options[ options.length - 1 ];
  135. for ( const option of options ) {
  136. test( option, convertTo );
  137. convertTo = option;
  138. }
  139. it( 'converts to default option when executed with already applied option', () => {
  140. const command = commands.heading1;
  141. setData( document, '<heading1>foo[]bar</heading1>' );
  142. command._doExecute();
  143. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  144. } );
  145. it( 'converts topmost blocks', () => {
  146. schema.registerItem( 'inlineImage', '$inline' );
  147. schema.allow( { name: '$text', inside: 'inlineImage' } );
  148. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  149. commands.heading1._doExecute();
  150. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  151. } );
  152. function test( from, to ) {
  153. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  154. setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  155. commands[ to.modelElement ]._doExecute();
  156. expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  157. } );
  158. }
  159. } );
  160. describe( 'non-collapsed selection', () => {
  161. let convertTo = options[ options.length - 1 ];
  162. for ( const option of options ) {
  163. test( option, convertTo );
  164. convertTo = option;
  165. }
  166. it( 'converts all elements where selection is applied', () => {
  167. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>]baz</heading3>' );
  168. commands.heading3._doExecute();
  169. expect( getData( document ) ).to.equal(
  170. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
  171. );
  172. } );
  173. it( 'resets to default value all elements with same option', () => {
  174. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  175. commands.heading1._doExecute();
  176. expect( getData( document ) ).to.equal(
  177. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  178. );
  179. } );
  180. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  181. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  182. setData(
  183. document,
  184. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  185. );
  186. commands[ toElement ]._doExecute();
  187. expect( getData( document ) ).to.equal(
  188. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  189. );
  190. } );
  191. }
  192. } );
  193. } );
  194. describe( 'isEnabled', () => {
  195. for ( const option of options ) {
  196. test( option.modelElement );
  197. }
  198. function test( modelElement ) {
  199. let command;
  200. beforeEach( () => {
  201. command = commands[ modelElement ];
  202. } );
  203. describe( `${ modelElement } command`, () => {
  204. it( 'should be enabled when inside another block', () => {
  205. setData( document, '<paragraph>f{}oo</paragraph>' );
  206. expect( command.isEnabled ).to.be.true;
  207. } );
  208. it( 'should be disabled if inside non-block', () => {
  209. setData( document, '<notBlock>f{}oo</notBlock>' );
  210. expect( command.isEnabled ).to.be.false;
  211. } );
  212. it( 'should be disabled if selection is placed on non-block', () => {
  213. setData( document, '[<notBlock>foo</notBlock>]' );
  214. expect( command.isEnabled ).to.be.false;
  215. } );
  216. } );
  217. }
  218. } );
  219. } );