headingcommand.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. describe( 'collapsed selection', () => {
  56. let convertTo = formats[ formats.length - 1 ];
  57. for ( let format of formats ) {
  58. test( format, convertTo );
  59. convertTo = format;
  60. }
  61. it( 'uses paragraph as default value', () => {
  62. setData( document, '<heading1>foo[]bar</heading1>' );
  63. command._doExecute();
  64. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  65. } );
  66. it( 'converts to default format when executed with already applied format', () => {
  67. setData( document, '<heading1>foo[]bar</heading1>' );
  68. command._doExecute( 'heading1' );
  69. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  70. } );
  71. it( 'converts topmost blocks', () => {
  72. setData( document, '<heading1><b>foo[]</b>bar</heading1>' );
  73. command._doExecute( 'heading1' );
  74. expect( getData( document ) ).to.equal( '<paragraph><b>foo[]</b>bar</paragraph>' );
  75. } );
  76. function test( from, to ) {
  77. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  78. setData( document, `<${ from.id }>foo[]bar</${ from.id }>` );
  79. command._doExecute( to.id );
  80. expect( getData( document ) ).to.equal( `<${ to.id }>foo[]bar</${ to.id }>` );
  81. } );
  82. }
  83. } );
  84. describe( 'non-collapsed selection', () => {
  85. let convertTo = formats[ formats.length - 1 ];
  86. for ( let format of formats ) {
  87. test( format, convertTo );
  88. convertTo = format;
  89. }
  90. it( 'converts all elements where selection is applied', () => {
  91. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  92. command._doExecute( 'paragraph' );
  93. expect( getData( document ) ).to.equal(
  94. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
  95. );
  96. } );
  97. it( 'resets to default value all elements with same format', () => {
  98. setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
  99. command._doExecute( 'heading1' );
  100. expect( getData( document ) ).to.equal(
  101. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
  102. );
  103. } );
  104. function test( from, to ) {
  105. it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
  106. setData( document, `<${ from.id }>foo[bar</${ from.id }><${ from.id }>baz]qux</${ from.id }>` );
  107. command._doExecute( to.id );
  108. expect( getData( document ) ).to.equal( `<${ to.id }>foo[bar</${ to.id }><${ to.id }>baz]qux</${ to.id }>` );
  109. } );
  110. }
  111. } );
  112. } );
  113. } );