headingcommand.js 5.3 KB

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