headingcommand.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, document, commands, root, schema;
  17. beforeEach( () => {
  18. return ModelTestEditor.create().then( newEditor => {
  19. editor = newEditor;
  20. document = editor.document;
  21. commands = {};
  22. schema = document.schema;
  23. editor.commands.set( 'paragraph', new ParagraphCommand( editor ) );
  24. schema.registerItem( 'paragraph', '$block' );
  25. for ( let option of options ) {
  26. commands[ option.modelElement ] = new HeadingCommand( editor, option );
  27. schema.registerItem( option.modelElement, '$block' );
  28. }
  29. root = document.getRoot();
  30. } );
  31. } );
  32. afterEach( () => {
  33. for ( let modelElement in commands ) {
  34. commands[ modelElement ].destroy();
  35. }
  36. } );
  37. describe( 'basic properties', () => {
  38. for ( let option of options ) {
  39. test( option );
  40. }
  41. function test( { modelElement, viewElement, title } ) {
  42. it( `are set for option.modelElement = ${ modelElement }`, () => {
  43. expect( commands[ modelElement ].modelElement ).to.equal( modelElement );
  44. expect( commands[ modelElement ].viewElement ).to.equal( viewElement );
  45. expect( commands[ modelElement ].title ).to.equal( title );
  46. } );
  47. }
  48. } );
  49. describe( 'value', () => {
  50. for ( let option of options ) {
  51. test( option );
  52. }
  53. function test( { modelElement } ) {
  54. it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
  55. setData( document, `<${ modelElement }>foobar</${ modelElement }>` );
  56. const element = root.getChild( 0 );
  57. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  58. expect( commands[ modelElement ].value ).to.be.true;
  59. } );
  60. }
  61. } );
  62. describe( '_doExecute', () => {
  63. it( 'should update value after execution', () => {
  64. const command = commands.heading1;
  65. setData( document, '<paragraph>[]</paragraph>' );
  66. command._doExecute();
  67. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  68. expect( command.value ).to.be.true;
  69. } );
  70. describe( 'custom options', () => {
  71. it( 'should use provided batch', () => {
  72. const batch = editor.document.batch();
  73. const command = commands.heading1;
  74. setData( document, '<paragraph>foo[]bar</paragraph>' );
  75. expect( batch.deltas.length ).to.equal( 0 );
  76. command._doExecute( { batch } );
  77. expect( batch.deltas.length ).to.be.above( 0 );
  78. } );
  79. it( 'should use provided batch (converting to default option)', () => {
  80. const batch = editor.document.batch();
  81. const command = commands.heading1;
  82. setData( document, '<heading1>foo[]bar</heading1>' );
  83. expect( batch.deltas.length ).to.equal( 0 );
  84. command._doExecute( { batch } );
  85. expect( batch.deltas.length ).to.be.above( 0 );
  86. } );
  87. } );
  88. describe( 'collapsed selection', () => {
  89. let convertTo = options[ options.length - 1 ];
  90. for ( let option of options ) {
  91. test( option, convertTo );
  92. convertTo = option;
  93. }
  94. it( 'converts to default option when executed with already applied option', () => {
  95. const command = commands.heading1;
  96. setData( document, '<heading1>foo[]bar</heading1>' );
  97. command._doExecute();
  98. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  99. } );
  100. it( 'converts topmost blocks', () => {
  101. schema.registerItem( 'inlineImage', '$inline' );
  102. schema.allow( { name: '$text', inside: 'inlineImage' } );
  103. setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  104. commands.heading1._doExecute();
  105. expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  106. } );
  107. function test( from, to ) {
  108. it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
  109. setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
  110. commands[ to.modelElement ]._doExecute();
  111. expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
  112. } );
  113. }
  114. } );
  115. describe( 'non-collapsed selection', () => {
  116. let convertTo = options[ options.length - 1 ];
  117. for ( let option of options ) {
  118. test( option, convertTo );
  119. convertTo = option;
  120. }
  121. it( 'converts all elements where selection is applied', () => {
  122. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  123. commands.heading3._doExecute();
  124. expect( getData( document ) ).to.equal(
  125. '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
  126. );
  127. } );
  128. it( 'resets to default value all elements with same option', () => {
  129. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  130. commands.heading1._doExecute();
  131. expect( getData( document ) ).to.equal(
  132. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  133. );
  134. } );
  135. function test( { modelElement: fromElement }, { modelElement: toElement } ) {
  136. it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
  137. setData(
  138. document,
  139. `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
  140. );
  141. commands[ toElement ]._doExecute();
  142. expect( getData( document ) ).to.equal(
  143. `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
  144. );
  145. } );
  146. }
  147. } );
  148. } );
  149. } );