headingcommand.js 5.4 KB

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