8
0

headingcommand.js 8.3 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. { 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, model, document, commands, root, schema;
  17. beforeEach( () => {
  18. return ModelTestEditor.create().then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. document = model.document;
  22. commands = {};
  23. schema = model.schema;
  24. editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
  25. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  26. for ( const option of options ) {
  27. commands[ option.modelElement ] = new HeadingCommand( editor, option.modelElement );
  28. schema.register( option.modelElement, '$block' );
  29. }
  30. schema.register( 'notBlock' );
  31. schema.extend( 'notBlock', { allowIn: '$root' } );
  32. schema.extend( '$text', { allowIn: 'notBlock' } );
  33. root = document.getRoot();
  34. } );
  35. } );
  36. afterEach( () => {
  37. for ( const modelElement in commands ) {
  38. commands[ modelElement ].destroy();
  39. }
  40. } );
  41. describe( 'modelElement', () => {
  42. it( 'is set', () => {
  43. expect( commands.heading1.modelElement ).to.equal( 'heading1' );
  44. } );
  45. } );
  46. describe( 'value', () => {
  47. for ( const option of options ) {
  48. test( option );
  49. }
  50. function test( { modelElement } ) {
  51. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  52. setData( model, `<${ modelElement }>foobar</${ modelElement }>` );
  53. const element = root.getChild( 0 );
  54. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  55. expect( commands[ modelElement ].value ).to.be.true;
  56. } );
  57. it( 'equals false if inside to non-block element', () => {
  58. setData( model, '<notBlock>[foo]</notBlock>' );
  59. expect( commands[ modelElement ].value ).to.be.false;
  60. } );
  61. it( `equals false if moved from ${ modelElement } to non-block element`, () => {
  62. setData( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  63. const element = document.getRoot().getChild( 1 );
  64. model.change( () => {
  65. document.selection.setRanges( [ Range.createIn( element ) ] );
  66. } );
  67. expect( commands[ modelElement ].value ).to.be.false;
  68. } );
  69. it( 'should be refreshed after calling refresh()', () => {
  70. const command = commands[ modelElement ];
  71. setData( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  72. const element = document.getRoot().getChild( 1 );
  73. // Purposely not putting it in `model.change` to update command manually.
  74. document.selection.setRanges( [ Range.createIn( element ) ] );
  75. expect( command.value ).to.be.true;
  76. command.refresh();
  77. expect( command.value ).to.be.false;
  78. } );
  79. }
  80. } );
  81. describe( 'execute()', () => {
  82. it( 'should update value after execution', () => {
  83. const command = commands.heading1;
  84. setData( model, '<paragraph>[]</paragraph>' );
  85. command.execute();
  86. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  87. expect( command.value ).to.be.true;
  88. } );
  89. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  90. it( 'should not rename blocks which cannot become headings', () => {
  91. schema.register( 'restricted' );
  92. schema.extend( 'restricted', { allowIn: '$root' } );
  93. schema.xdisallow( 'heading1', { allowIn: 'restricted' } );
  94. schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  95. schema.extend( 'fooBlock', { allowIn: 'restricted' } );
  96. setData(
  97. model,
  98. '<paragraph>a[bc</paragraph>' +
  99. '<restricted><fooBlock></fooBlock></restricted>' +
  100. '<paragraph>de]f</paragraph>'
  101. );
  102. commands.heading1.execute();
  103. expect( getData( model ) ).to.equal(
  104. '<heading1>a[bc</heading1>' +
  105. '<restricted><fooBlock></fooBlock></restricted>' +
  106. '<heading1>de]f</heading1>'
  107. );
  108. } );
  109. it( 'should use parent batch', () => {
  110. const command = commands.heading1;
  111. setData( model, '<paragraph>foo[]bar</paragraph>' );
  112. model.change( writer => {
  113. expect( writer.batch.deltas.length ).to.equal( 0 );
  114. command.execute();
  115. expect( writer.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( model, '<heading1>foo[]bar</heading1>' );
  126. commands.heading1.execute();
  127. expect( getData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  128. } );
  129. it( 'converts topmost blocks', () => {
  130. schema.register( 'inlineImage', { allowWhere: '$text' } );
  131. schema.extend( '$text', { allowIn: 'inlineImage' } );
  132. setData( model, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  133. commands.heading1.execute();
  134. expect( getData( model ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  135. } );
  136. function test( from, to ) {
  137. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  138. setData( model, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  139. commands[ to.modelElement ].execute();
  140. expect( getData( model ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  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( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>baz]</heading3>' );
  152. commands.heading3.execute();
  153. expect( getData( model ) ).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( model, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  159. commands.heading1.execute();
  160. expect( getData( model ) ).to.equal(
  161. '<heading1>[foo</heading1><heading1>bar]</heading1>'
  162. );
  163. } );
  164. it( 'does nothing to the elements with same option (#2)', () => {
  165. setData( model, '<heading1>[foo</heading1><heading1>bar</heading1><heading2>baz]</heading2>' );
  166. commands.heading1.execute();
  167. expect( getData( model ) ).to.equal(
  168. '<heading1>[foo</heading1><heading1>bar</heading1><heading1>baz]</heading1>'
  169. );
  170. } );
  171. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  172. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  173. setData(
  174. model,
  175. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  176. );
  177. commands[ toElement ].execute();
  178. expect( getData( model ) ).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.modelElement );
  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( model, '<paragraph>f{}oo</paragraph>' );
  197. expect( command.isEnabled ).to.be.true;
  198. } );
  199. it( 'should be disabled if inside non-block', () => {
  200. setData( model, '<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( model, '[<notBlock>foo</notBlock>]' );
  205. expect( command.isEnabled ).to.be.false;
  206. } );
  207. } );
  208. }
  209. } );
  210. } );