headingcommand.js 9.4 KB

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