headingcommand.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 HeadingCommand from '../src/headingcommand';
  7. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  8. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. const options = [
  10. { id: 'paragraph', element: 'p', label: 'P' },
  11. { id: 'heading1', element: 'h2', label: 'H2' },
  12. { id: 'heading2', element: 'h3', label: 'H3' },
  13. { id: 'heading3', element: 'h4', label: '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. for ( let option of options ) {
  24. commands[ option.id ] = new HeadingCommand( editor, option );
  25. schema.registerItem( option.id, '$block' );
  26. }
  27. root = document.getRoot();
  28. } );
  29. } );
  30. afterEach( () => {
  31. for ( let id in commands ) {
  32. commands[ id ].destroy();
  33. }
  34. } );
  35. describe( 'basic properties', () => {
  36. for ( let option of options ) {
  37. test( option );
  38. }
  39. function test( { id, element, label } ) {
  40. it( `are set for option.id = ${ id }`, () => {
  41. expect( commands[ id ].id ).to.equal( id );
  42. expect( commands[ id ].element ).to.equal( element );
  43. expect( commands[ id ].label ).to.equal( label );
  44. } );
  45. }
  46. } );
  47. describe( 'value', () => {
  48. for ( let option of options ) {
  49. test( option );
  50. }
  51. function test( { id } ) {
  52. it( `equals ${ id } when collapsed selection is placed inside ${ id } element`, () => {
  53. setData( document, `<${ id }>foobar</${ id }>` );
  54. const element = root.getChild( 0 );
  55. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  56. expect( commands[ id ].value ).to.be.true;
  57. } );
  58. }
  59. } );
  60. describe( '_doExecute', () => {
  61. it( 'should update value after execution', () => {
  62. const command = commands.heading1;
  63. setData( document, '<paragraph>[]</paragraph>' );
  64. command._doExecute();
  65. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  66. expect( command.value ).to.be.true;
  67. } );
  68. describe( 'custom options', () => {
  69. it( 'should use provided batch', () => {
  70. const batch = editor.document.batch();
  71. const command = commands.heading1;
  72. setData( document, '<paragraph>foo[]bar</paragraph>' );
  73. expect( batch.deltas.length ).to.equal( 0 );
  74. command._doExecute( { batch } );
  75. expect( batch.deltas.length ).to.be.above( 0 );
  76. } );
  77. } );
  78. describe( 'collapsed selection', () => {
  79. let convertTo = options[ options.length - 1 ];
  80. for ( let option of options ) {
  81. test( option, convertTo );
  82. convertTo = option;
  83. }
  84. it( 'uses paragraph as default value', () => {
  85. setData( document, '<heading1>foo[]bar</heading1>' );
  86. commands.paragraph._doExecute();
  87. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  88. } );
  89. // it( 'converts to default option when executed with already applied option', () => {
  90. // const command = commands.paragraph;
  91. // setData( document, '<heading1>foo[]bar</heading1>' );
  92. // command._doExecute( { id: 'heading1' } );
  93. // expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  94. // } );
  95. it( 'converts topmost blocks', () => {
  96. schema.registerItem( 'inlineImage', '$inline' );
  97. schema.allow( { name: '$text', inside: 'inlineImage' } );
  98. setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  99. commands.paragraph._doExecute();
  100. expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  101. } );
  102. function test( from, to ) {
  103. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  104. setData( document, `<${ from.id }>foo[]bar</${ from.id }>` );
  105. commands[ to.id ]._doExecute();
  106. expect( getData( document ) ).to.equal( `<${ to.id }>foo[]bar</${ to.id }>` );
  107. } );
  108. }
  109. } );
  110. describe( 'non-collapsed selection', () => {
  111. let convertTo = options[ options.length - 1 ];
  112. for ( let option of options ) {
  113. test( option, convertTo );
  114. convertTo = option;
  115. }
  116. it( 'converts all elements where selection is applied', () => {
  117. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  118. commands.paragraph._doExecute();
  119. expect( getData( document ) ).to.equal(
  120. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
  121. );
  122. } );
  123. // it( 'resets to default value all elements with same option', () => {
  124. // setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  125. // commands.heading1._doExecute();
  126. // expect( getData( document ) ).to.equal(
  127. // '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  128. // );
  129. // } );
  130. function test( from, to ) {
  131. it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
  132. setData( document, `<${ from.id }>foo[bar</${ from.id }><${ from.id }>baz]qux</${ from.id }>` );
  133. commands[ to.id ]._doExecute();
  134. expect( getData( document ) ).to.equal( `<${ to.id }>foo[bar</${ to.id }><${ to.id }>baz]qux</${ to.id }>` );
  135. } );
  136. }
  137. } );
  138. } );
  139. } );