headingengine.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import HeadingEngine from '../src/headingengine';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import HeadingCommand from '../src/headingcommand';
  9. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  10. import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. describe( 'HeadingEngine', () => {
  12. let editor, document;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ Enter, HeadingEngine ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. document = editor.document;
  20. } );
  21. } );
  22. it( 'should be loaded', () => {
  23. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  24. } );
  25. it( 'should load paragraph feature', () => {
  26. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( document.schema.hasItem( 'heading1' ) ).to.be.true;
  30. expect( document.schema.hasItem( 'heading2' ) ).to.be.true;
  31. expect( document.schema.hasItem( 'heading3' ) ).to.be.true;
  32. expect( document.schema.check( { name: 'heading1', inside: '$root' } ) ).to.be.true;
  33. expect( document.schema.check( { name: '$inline', inside: 'heading1' } ) ).to.be.true;
  34. expect( document.schema.check( { name: 'heading2', inside: '$root' } ) ).to.be.true;
  35. expect( document.schema.check( { name: '$inline', inside: 'heading2' } ) ).to.be.true;
  36. expect( document.schema.check( { name: 'heading3', inside: '$root' } ) ).to.be.true;
  37. expect( document.schema.check( { name: '$inline', inside: 'heading3' } ) ).to.be.true;
  38. } );
  39. it( 'should register format command', () => {
  40. expect( editor.commands.has( 'heading' ) ).to.be.true;
  41. const command = editor.commands.get( 'heading' );
  42. expect( command ).to.be.instanceOf( HeadingCommand );
  43. } );
  44. it( 'should convert heading1', () => {
  45. editor.setData( '<h2>foobar</h2>' );
  46. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  47. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  48. } );
  49. it( 'should convert heading2', () => {
  50. editor.setData( '<h3>foobar</h3>' );
  51. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
  52. expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
  53. } );
  54. it( 'should convert heading3', () => {
  55. editor.setData( '<h4>foobar</h4>' );
  56. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  57. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  58. } );
  59. it( 'should make enter command insert a defaultFormat block if selection ended at the end of heading block', () => {
  60. editor.setData( '<h2>foobar</h2>' );
  61. document.selection.collapse( document.getRoot().getChild( 0 ), 'end' );
  62. editor.execute( 'enter' );
  63. expect( getData( document ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  64. } );
  65. it( 'should not alter enter command if selection not ended at the end of heading block', () => {
  66. // This test is to fill code coverage.
  67. editor.setData( '<h2>foobar</h2>' );
  68. document.selection.collapse( document.getRoot().getChild( 0 ), 3 );
  69. editor.execute( 'enter' );
  70. expect( getData( document ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  71. } );
  72. describe( 'config', () => {
  73. describe( 'formats', () => {
  74. it( 'should have default value', () => {
  75. expect( editor.config.get( 'heading.formats' ) ).to.deep.equal( [
  76. { id: 'paragraph', element: 'p', label: 'Paragraph' },
  77. { id: 'heading1', element: 'h2', label: 'Heading 1' },
  78. { id: 'heading2', element: 'h3', label: 'Heading 2' },
  79. { id: 'heading3', element: 'h4', label: 'Heading 3' }
  80. ] );
  81. } );
  82. it( 'should customize formats', () => {
  83. const formats = [
  84. { id: 'paragraph', element: 'p', label: 'Paragraph' },
  85. { id: 'h4', element: 'h4', label: 'H4' }
  86. ];
  87. return VirtualTestEditor.create( {
  88. plugins: [ Enter, HeadingEngine ],
  89. heading: {
  90. formats: formats
  91. }
  92. } )
  93. .then( editor => {
  94. document = editor.document;
  95. expect( editor.commands.get( 'heading' ).formats ).to.deep.equal( formats );
  96. expect( document.schema.hasItem( 'paragraph' ) ).to.be.true;
  97. expect( document.schema.hasItem( 'h4' ) ).to.be.true;
  98. expect( document.schema.hasItem( 'heading1' ) ).to.be.false;
  99. expect( document.schema.hasItem( 'heading2' ) ).to.be.false;
  100. expect( document.schema.hasItem( 'heading3' ) ).to.be.false;
  101. } );
  102. } );
  103. } );
  104. describe( 'defaultFormatId', () => {
  105. it( 'should have default value', () => {
  106. expect( editor.config.get( 'heading.defaultFormatId' ) ).to.equal( 'paragraph' );
  107. } );
  108. it( 'should customize formats', () => {
  109. return VirtualTestEditor.create( {
  110. plugins: [ Enter, HeadingEngine ],
  111. heading: {
  112. formats: [
  113. { id: 'foo', element: 'f', label: 'Foo' },
  114. { id: 'bar', element: 'b', label: 'Bar' }
  115. ],
  116. defaultFormatId: 'bar'
  117. }
  118. } )
  119. .then( editor => {
  120. document = editor.document;
  121. expect( editor.commands.get( 'heading' ).value ).to.deep.equal( {
  122. id: 'bar',
  123. element: 'b',
  124. label: 'Bar'
  125. } );
  126. expect( document.schema.hasItem( 'foo' ) ).to.be.true;
  127. expect( document.schema.hasItem( 'bar' ) ).to.be.false;
  128. } );
  129. } );
  130. } );
  131. } );
  132. } );