headingcommand.js 9.3 KB

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