8
0

headingcommand.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @license Copyright (c) 2003-2018, 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, 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.model ] = new HeadingCommand( editor, option.model );
  28. schema.register( option.model, { inheritAllFrom: '$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( { model: 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. model.change( writer => {
  55. const ranges = [
  56. ...model.document.selection.getRanges(),
  57. Range.createFromParentsAndOffsets( element, 3, element, 3 )
  58. ];
  59. writer.setSelection( ranges );
  60. } );
  61. expect( commands[ modelElement ].value ).to.be.true;
  62. } );
  63. it( 'equals false if inside to non-block element', () => {
  64. setData( model, '<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( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  69. const element = document.getRoot().getChild( 1 );
  70. model.change( writer => {
  71. writer.setSelection( Range.createIn( element ) );
  72. } );
  73. expect( commands[ modelElement ].value ).to.be.false;
  74. } );
  75. it( 'should be refreshed after calling refresh()', () => {
  76. const command = commands[ modelElement ];
  77. setData( model, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
  78. const element = document.getRoot().getChild( 1 );
  79. // Purposely not putting it in `model.change` to update command manually.
  80. model.document.selection._setTo( Range.createIn( element ) );
  81. expect( command.value ).to.be.true;
  82. command.refresh();
  83. expect( command.value ).to.be.false;
  84. } );
  85. }
  86. } );
  87. describe( 'execute()', () => {
  88. it( 'should update value after execution', () => {
  89. const command = commands.heading1;
  90. setData( model, '<paragraph>[]</paragraph>' );
  91. command.execute();
  92. expect( getData( model ) ).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 (heading is not allowed in their parent)', () => {
  97. schema.register( 'restricted' );
  98. schema.extend( 'restricted', { allowIn: '$root' } );
  99. schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  100. schema.extend( 'fooBlock', {
  101. allowIn: [ 'restricted', '$root' ]
  102. } );
  103. setData(
  104. model,
  105. '<paragraph>a[bc</paragraph>' +
  106. '<restricted><fooBlock></fooBlock></restricted>' +
  107. '<fooBlock>de]f</fooBlock>'
  108. );
  109. commands.heading1.execute();
  110. expect( getData( model ) ).to.equal(
  111. '<heading1>a[bc</heading1>' +
  112. '<restricted><fooBlock></fooBlock></restricted>' +
  113. '<heading1>de]f</heading1>'
  114. );
  115. } );
  116. it( 'should not rename blocks which cannot become headings (block is an object)', () => {
  117. schema.register( 'image', {
  118. isBlock: true,
  119. isObject: true,
  120. allowIn: '$root'
  121. } );
  122. setData(
  123. model,
  124. '<paragraph>a[bc</paragraph>' +
  125. '<image></image>' +
  126. '<paragraph>de]f</paragraph>'
  127. );
  128. commands.heading1.execute();
  129. expect( getData( model ) ).to.equal(
  130. '<heading1>a[bc</heading1>' +
  131. '<image></image>' +
  132. '<heading1>de]f</heading1>'
  133. );
  134. } );
  135. it( 'should use parent batch', () => {
  136. const command = commands.heading1;
  137. setData( model, '<paragraph>foo[]bar</paragraph>' );
  138. model.change( writer => {
  139. expect( writer.batch.deltas.length ).to.equal( 0 );
  140. command.execute();
  141. expect( writer.batch.deltas.length ).to.be.above( 0 );
  142. } );
  143. } );
  144. describe( '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( 'does nothing when executed with already applied option', () => {
  151. setData( model, '<heading1>foo[]bar</heading1>' );
  152. commands.heading1.execute();
  153. expect( getData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  154. } );
  155. it( 'converts topmost blocks', () => {
  156. schema.register( 'inlineImage', { allowWhere: '$text' } );
  157. schema.extend( '$text', { allowIn: 'inlineImage' } );
  158. setData( model, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  159. commands.heading1.execute();
  160. expect( getData( model ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  161. } );
  162. function test( from, to ) {
  163. it( `converts ${ from.model } to ${ to.model } on collapsed selection`, () => {
  164. setData( model, `<${ from.model }>foo[]bar</${ from.model }>` );
  165. commands[ to.model ].execute();
  166. expect( getData( model ) ).to.equal( `<${ to.model }>foo[]bar</${ to.model }>` );
  167. } );
  168. }
  169. } );
  170. describe( 'non-collapsed selection', () => {
  171. let convertTo = options[ options.length - 1 ];
  172. for ( const option of options ) {
  173. test( option, convertTo );
  174. convertTo = option;
  175. }
  176. it( 'converts all elements where selection is applied', () => {
  177. setData( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>baz]</heading3>' );
  178. commands.heading3.execute();
  179. expect( getData( model ) ).to.equal(
  180. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>baz]</heading3>'
  181. );
  182. } );
  183. it( 'does nothing to the elements with same option (#1)', () => {
  184. setData( model, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  185. commands.heading1.execute();
  186. expect( getData( model ) ).to.equal(
  187. '<heading1>[foo</heading1><heading1>bar]</heading1>'
  188. );
  189. } );
  190. it( 'does nothing to the elements with same option (#2)', () => {
  191. setData( model, '<heading1>[foo</heading1><heading1>bar</heading1><heading2>baz]</heading2>' );
  192. commands.heading1.execute();
  193. expect( getData( model ) ).to.equal(
  194. '<heading1>[foo</heading1><heading1>bar</heading1><heading1>baz]</heading1>'
  195. );
  196. } );
  197. function test( { model: fromElement }, { model: toElement } ) {
  198. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  199. setData(
  200. model,
  201. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  202. );
  203. commands[ toElement ].execute();
  204. expect( getData( model ) ).to.equal(
  205. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  206. );
  207. } );
  208. }
  209. } );
  210. } );
  211. describe( 'isEnabled', () => {
  212. for ( const option of options ) {
  213. test( option.model );
  214. }
  215. function test( modelElement ) {
  216. let command;
  217. beforeEach( () => {
  218. command = commands[ modelElement ];
  219. } );
  220. describe( `${ modelElement } command`, () => {
  221. it( 'should be enabled when inside another block', () => {
  222. setData( model, '<paragraph>f{}oo</paragraph>' );
  223. expect( command.isEnabled ).to.be.true;
  224. } );
  225. it( 'should be disabled if inside non-block', () => {
  226. setData( model, '<notBlock>f{}oo</notBlock>' );
  227. expect( command.isEnabled ).to.be.false;
  228. } );
  229. it( 'should be disabled if selection is placed on non-block', () => {
  230. setData( model, '[<notBlock>foo</notBlock>]' );
  231. expect( command.isEnabled ).to.be.false;
  232. } );
  233. } );
  234. }
  235. } );
  236. } );