8
0

headingcommand.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import HeadingCommand from 'ckeditor5/heading/headingcommand.js';
  7. import Range from 'ckeditor5/engine/model/range.js';
  8. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  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. schema.registerItem( 'b', '$inline' );
  28. root = document.getRoot();
  29. } );
  30. } );
  31. afterEach( () => {
  32. command.destroy();
  33. } );
  34. describe( 'value', () => {
  35. for ( let format of formats ) {
  36. test( format );
  37. }
  38. function test( format ) {
  39. it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => {
  40. setData( document, `<${ format.id }>foobar</${ format.id }>` );
  41. const element = root.getChild( 0 );
  42. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  43. expect( command.value ).to.equal( format );
  44. } );
  45. }
  46. it( 'should be equal to defaultFormat if format has not been found', () => {
  47. schema.registerItem( 'div', '$block' );
  48. setData( document, '<div>xyz</div>' );
  49. const element = root.getChild( 0 );
  50. document.selection.addRange( Range.createFromParentsAndOffsets( element, 1, element, 1 ) );
  51. expect( command.value ).to.equal( command.defaultFormat );
  52. } );
  53. } );
  54. describe( '_doExecute', () => {
  55. it( 'should update value after execution', () => {
  56. setData( document, '<paragraph>[]</paragraph>' );
  57. command._doExecute( { formatId: 'heading1' } );
  58. expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
  59. expect( command.value ).to.be.object;
  60. expect( command.value.id ).to.equal( 'heading1' );
  61. expect( command.value.viewElement ).to.equal( 'h2' );
  62. } );
  63. describe( 'custom options', () => {
  64. it( 'should use provided batch', () => {
  65. const batch = editor.document.batch();
  66. setData( document, '<paragraph>foo[]bar</paragraph>' );
  67. expect( batch.deltas.length ).to.equal( 0 );
  68. command._doExecute( { batch } );
  69. expect( batch.deltas.length ).to.be.above( 0 );
  70. } );
  71. } );
  72. describe( 'collapsed selection', () => {
  73. let convertTo = formats[ formats.length - 1 ];
  74. for ( let format of formats ) {
  75. test( format, convertTo );
  76. convertTo = format;
  77. }
  78. it( 'uses paragraph as default value', () => {
  79. setData( document, '<heading1>foo[]bar</heading1>' );
  80. command._doExecute();
  81. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  82. } );
  83. it( 'converts to default format when executed with already applied format', () => {
  84. setData( document, '<heading1>foo[]bar</heading1>' );
  85. command._doExecute( { formatId: 'heading1' } );
  86. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  87. } );
  88. it( 'converts topmost blocks', () => {
  89. setData( document, '<heading1><b>foo[]</b>bar</heading1>' );
  90. command._doExecute( { formatId: 'heading1' } );
  91. expect( getData( document ) ).to.equal( '<paragraph><b>foo[]</b>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( { formatId: 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 = formats[ formats.length - 1 ];
  103. for ( let format of formats ) {
  104. test( format, convertTo );
  105. convertTo = format;
  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( { formatId: '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 format', () => {
  115. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  116. command._doExecute( { formatId: '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( { formatId: 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. } );