8
0

headingengine.js 5.7 KB

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