8
0

headingengine.js 5.5 KB

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