headingscommand.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 HeadingsCommand from '/ckeditor5/headings/headingscommand.js';
  7. import Range from '/ckeditor5/engine/model/range.js';
  8. import { setData, getData } from '/tests/engine/_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( 'HeadingsCommand', () => {
  16. let editor, document, command, root;
  17. beforeEach( () => {
  18. return ModelTestEditor.create()
  19. .then( newEditor => {
  20. editor = newEditor;
  21. document = editor.document;
  22. command = new HeadingsCommand( editor, formats );
  23. const 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. } );
  47. describe( '_doExecute', () => {
  48. describe( 'collapsed selection', () => {
  49. let convertTo = formats[ formats.length - 1 ];
  50. for ( let format of formats ) {
  51. test( format, convertTo );
  52. convertTo = format;
  53. }
  54. it( 'uses paragraph as default value', () => {
  55. setData( document, '<heading1>foo<selection />bar</heading1>' );
  56. command._doExecute();
  57. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  58. } );
  59. it( 'converts to default format when executed with already applied format', () => {
  60. setData( document, '<heading1>foo<selection />bar</heading1>' );
  61. command._doExecute( 'heading1' );
  62. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  63. } );
  64. it( 'converts topmost blocks', () => {
  65. setData( document, '<heading1><b>foo<selection /></b>bar</heading1>' );
  66. command._doExecute( 'heading1' );
  67. expect( getData( document ) ).to.equal( '<paragraph><b>foo<selection /></b>bar</paragraph>' );
  68. } );
  69. function test( from, to ) {
  70. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  71. setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
  72. command._doExecute( to.id );
  73. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection />bar</${ to.id }>` );
  74. } );
  75. }
  76. } );
  77. describe( 'non-collapsed selection', () => {
  78. let convertTo = formats[ formats.length - 1 ];
  79. for ( let format of formats ) {
  80. test( format, convertTo );
  81. convertTo = format;
  82. }
  83. it( 'converts all elements where selection is applied', () => {
  84. setData( document, '<heading1>foo<selection></heading1><heading2>bar</heading2><heading2></selection>baz</heading2>' );
  85. command._doExecute( 'paragraph' );
  86. expect( getData( document ) ).to.equal(
  87. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><paragraph></selection>baz</paragraph>'
  88. );
  89. } );
  90. it( 'resets to default value all elements with same format', () => {
  91. setData( document, '<heading1>foo<selection></heading1><heading1>bar</heading1><heading2>baz</heading2></selection>' );
  92. command._doExecute( 'heading1' );
  93. expect( getData( document ) ).to.equal(
  94. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><heading2>baz</heading2></selection>'
  95. );
  96. } );
  97. function test( from, to ) {
  98. it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
  99. setData( document, `<${ from.id }>foo<selection>bar</${ from.id }><${ from.id }>baz</selection>qux</${ from.id }>` );
  100. command._doExecute( to.id );
  101. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection>bar</${ to.id }><${ to.id }>baz</selection>qux</${ to.id }>` );
  102. } );
  103. }
  104. } );
  105. } );
  106. } );