headingcommand.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. const options = [
  10. { model: 'heading1', view: { name: 'h2' }, title: 'H2' },
  11. { model: 'heading2', view: { name: 'h3' }, title: 'H3' },
  12. { model: 'heading3', view: { name: 'h4' }, title: 'H4' }
  13. ];
  14. describe( 'HeadingCommand', () => {
  15. let editor, model, document, command, root, schema;
  16. beforeEach( () => {
  17. return ModelTestEditor.create().then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. document = model.document;
  21. schema = model.schema;
  22. editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
  23. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  24. const modelElements = [];
  25. for ( const option of options ) {
  26. modelElements.push( option.model );
  27. schema.register( option.model, { inheritAllFrom: '$block' } );
  28. }
  29. command = new HeadingCommand( editor, modelElements );
  30. editor.commands.add( 'heading', command );
  31. schema.register( 'heading', { inheritAllFrom: '$block' } );
  32. schema.register( 'notBlock' );
  33. schema.extend( 'notBlock', { allowIn: '$root' } );
  34. schema.extend( '$text', { allowIn: 'notBlock' } );
  35. root = document.getRoot();
  36. } );
  37. } );
  38. describe( 'modelElements', () => {
  39. it( 'is set', () => {
  40. expect( command.modelElements ).to.deep.equal( [ 'heading1', 'heading2', 'heading3' ] );
  41. } );
  42. } );
  43. describe( 'value', () => {
  44. for ( const option of options ) {
  45. test( option );
  46. }
  47. function test( { model: modelElement } ) {
  48. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  49. setData( model, `<${ modelElement }>foobar</${ modelElement }>` );
  50. const element = root.getChild( 0 );
  51. model.change( writer => {
  52. const ranges = [
  53. ...model.document.selection.getRanges(),
  54. writer.createRange( writer.createPositionAt( element, 3 ) )
  55. ];
  56. writer.setSelection( ranges );
  57. } );
  58. expect( command.value ).to.equal( modelElement );
  59. } );
  60. it( 'equals false if inside to non-block element', () => {
  61. setData( model, '<notBlock>[foo]</notBlock>' );
  62. expect( command.value ).to.be.false;
  63. } );
  64. it( `equals false if moved from ${ modelElement } to non-block element`, () => {
  65. setData( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  66. const element = document.getRoot().getChild( 1 );
  67. model.change( writer => {
  68. writer.setSelection( writer.createRangeIn( element ) );
  69. } );
  70. expect( command.value ).to.be.false;
  71. } );
  72. it( 'should be refreshed after calling refresh()', () => {
  73. setData( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  74. const element = document.getRoot().getChild( 1 );
  75. model.change( writer => {
  76. writer.setSelection( writer.createRangeIn( element ) );
  77. expect( command.value ).to.equal( modelElement );
  78. command.refresh();
  79. expect( command.value ).to.be.false;
  80. } );
  81. } );
  82. }
  83. } );
  84. describe( 'execute()', () => {
  85. it( 'should update value after execution', () => {
  86. setData( model, '<paragraph>[]</paragraph>' );
  87. command.execute( { value: 'heading1' } );
  88. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  89. expect( command.value ).to.equal( 'heading1' );
  90. } );
  91. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  92. it( 'should not rename blocks which cannot become headings (heading is not allowed in their parent)', () => {
  93. schema.register( 'restricted' );
  94. schema.extend( 'restricted', { allowIn: '$root' } );
  95. schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  96. schema.extend( 'fooBlock', {
  97. allowIn: [ 'restricted', '$root' ]
  98. } );
  99. setData(
  100. model,
  101. '<paragraph>a[bc</paragraph>' +
  102. '<restricted><fooBlock></fooBlock></restricted>' +
  103. '<fooBlock>de]f</fooBlock>'
  104. );
  105. command.execute( { value: 'heading1' } );
  106. expect( getData( model ) ).to.equal(
  107. '<heading1>a[bc</heading1>' +
  108. '<restricted><fooBlock></fooBlock></restricted>' +
  109. '<heading1>de]f</heading1>'
  110. );
  111. } );
  112. it( 'should not rename blocks which cannot become headings (block is an object)', () => {
  113. schema.register( 'image', {
  114. isBlock: true,
  115. isObject: true,
  116. allowIn: '$root'
  117. } );
  118. setData(
  119. model,
  120. '<paragraph>a[bc</paragraph>' +
  121. '<image></image>' +
  122. '<paragraph>de]f</paragraph>'
  123. );
  124. command.execute( { value: 'heading1' } );
  125. expect( getData( model ) ).to.equal(
  126. '<heading1>a[bc</heading1>' +
  127. '<image></image>' +
  128. '<heading1>de]f</heading1>'
  129. );
  130. } );
  131. it( 'should use parent batch', () => {
  132. setData( model, '<paragraph>foo[]bar</paragraph>' );
  133. model.change( writer => {
  134. expect( writer.batch.operations.length ).to.equal( 0 );
  135. command.execute( { value: 'heading1' } );
  136. expect( writer.batch.operations.length ).to.be.above( 0 );
  137. } );
  138. } );
  139. it( 'should do nothing with non-registered model elements', () => {
  140. setData( model, '<heading1>[]</heading1>' );
  141. command.execute( { value: 'heading5' } );
  142. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  143. expect( command.value ).to.equal( 'heading1' );
  144. } );
  145. describe( 'collapsed selection', () => {
  146. let convertTo = options[ options.length - 1 ];
  147. for ( const option of options ) {
  148. test( option, convertTo );
  149. convertTo = option;
  150. }
  151. it( 'does nothing when executed with already applied option', () => {
  152. setData( model, '<heading1>foo[]bar</heading1>' );
  153. command.execute( { value: 'heading1' } );
  154. expect( getData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  155. } );
  156. it( 'converts topmost blocks', () => {
  157. schema.register( 'inlineImage', { allowWhere: '$text' } );
  158. schema.extend( '$text', { allowIn: 'inlineImage' } );
  159. setData( model, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  160. command.execute( { value: 'heading1' } );
  161. expect( getData( model ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  162. } );
  163. function test( from, to ) {
  164. it( `converts ${ from.model } to ${ to.model } on collapsed selection`, () => {
  165. setData( model, `<${ from.model }>foo[]bar</${ from.model }>` );
  166. command.execute( { value: to.model } );
  167. expect( getData( model ) ).to.equal( `<${ to.model }>foo[]bar</${ to.model }>` );
  168. } );
  169. }
  170. } );
  171. describe( 'non-collapsed selection', () => {
  172. let convertTo = options[ options.length - 1 ];
  173. for ( const option of options ) {
  174. test( option, convertTo );
  175. convertTo = option;
  176. }
  177. it( 'converts all elements where selection is applied', () => {
  178. setData( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>baz]</heading3>' );
  179. command.execute( { value: 'heading3' } );
  180. expect( getData( model ) ).to.equal(
  181. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>baz]</heading3>'
  182. );
  183. } );
  184. it( 'does nothing to the elements with same option (#1)', () => {
  185. setData( model, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  186. command.execute( { value: 'heading1' } );
  187. expect( getData( model ) ).to.equal(
  188. '<heading1>[foo</heading1><heading1>bar]</heading1>'
  189. );
  190. } );
  191. it( 'does nothing to the elements with same option (#2)', () => {
  192. setData( model, '<heading1>[foo</heading1><heading1>bar</heading1><heading2>baz]</heading2>' );
  193. command.execute( { value: 'heading1' } );
  194. expect( getData( model ) ).to.equal(
  195. '<heading1>[foo</heading1><heading1>bar</heading1><heading1>baz]</heading1>'
  196. );
  197. } );
  198. function test( { model: fromElement }, { model: toElement } ) {
  199. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  200. setData(
  201. model,
  202. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  203. );
  204. command.execute( { value: toElement } );
  205. expect( getData( model ) ).to.equal(
  206. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  207. );
  208. } );
  209. }
  210. } );
  211. } );
  212. describe( 'isEnabled', () => {
  213. for ( const option of options ) {
  214. test( option.model );
  215. }
  216. function test( modelElement ) {
  217. describe( `${ modelElement } command`, () => {
  218. it( 'should be enabled when inside another block', () => {
  219. setData( model, '<paragraph>f{}oo</paragraph>' );
  220. expect( command.isEnabled ).to.be.true;
  221. } );
  222. it( 'should be disabled if inside non-block', () => {
  223. setData( model, '<notBlock>f{}oo</notBlock>' );
  224. expect( command.isEnabled ).to.be.false;
  225. } );
  226. it( 'should be disabled if selection is placed on non-block', () => {
  227. setData( model, '[<notBlock>foo</notBlock>]' );
  228. expect( command.isEnabled ).to.be.false;
  229. } );
  230. } );
  231. }
  232. } );
  233. } );