headingengine.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. it( 'should be localized', () => {
  91. return VirtualTestEditor.create( {
  92. plugins: [ Enter, HeadingEngine ],
  93. lang: 'pl',
  94. } )
  95. .then( editor => {
  96. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  97. { id: 'paragraph', element: 'p', label: 'Akapit' },
  98. { id: 'heading1', element: 'h2', label: 'Nagłówek 1' },
  99. { id: 'heading2', element: 'h3', label: 'Nagłówek 2' },
  100. { id: 'heading3', element: 'h4', label: 'Nagłówek 3' }
  101. ] );
  102. } );
  103. } );
  104. } );
  105. it( 'should customize options', () => {
  106. const options = [
  107. { id: 'paragraph', element: 'p', label: 'Paragraph' },
  108. { id: 'h4', element: 'h4', label: 'H4' }
  109. ];
  110. return VirtualTestEditor.create( {
  111. plugins: [ Enter, HeadingEngine ],
  112. heading: {
  113. options: options
  114. }
  115. } )
  116. .then( editor => {
  117. document = editor.document;
  118. expect( editor.commands.get( 'heading' ).options ).to.deep.equal( options );
  119. expect( document.schema.hasItem( 'paragraph' ) ).to.be.true;
  120. expect( document.schema.hasItem( 'h4' ) ).to.be.true;
  121. expect( document.schema.hasItem( 'heading1' ) ).to.be.false;
  122. expect( document.schema.hasItem( 'heading2' ) ).to.be.false;
  123. expect( document.schema.hasItem( 'heading3' ) ).to.be.false;
  124. } );
  125. } );
  126. } );
  127. describe( 'defaultOptionId', () => {
  128. it( 'should have default value', () => {
  129. expect( editor.config.get( 'heading.defaultOptionId' ) ).to.equal( 'paragraph' );
  130. } );
  131. it( 'should customize options', () => {
  132. return VirtualTestEditor.create( {
  133. plugins: [ Enter, HeadingEngine ],
  134. heading: {
  135. options: [
  136. { id: 'foo', element: 'f', label: 'Foo' },
  137. { id: 'bar', element: 'b', label: 'Bar' }
  138. ],
  139. defaultOptionId: 'bar'
  140. }
  141. } )
  142. .then( editor => {
  143. document = editor.document;
  144. expect( editor.commands.get( 'heading' ).value ).to.deep.equal( {
  145. id: 'bar',
  146. element: 'b',
  147. label: 'Bar'
  148. } );
  149. expect( document.schema.hasItem( 'foo' ) ).to.be.true;
  150. expect( document.schema.hasItem( 'bar' ) ).to.be.false;
  151. } );
  152. } );
  153. } );
  154. } );
  155. } );