headingcommand.js 5.4 KB

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