headingengine.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'HeadingEngine', () => {
  13. let editor, document;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ Enter, HeadingEngine ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. document = editor.document;
  21. } );
  22. } );
  23. it( 'should be loaded', () => {
  24. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  25. } );
  26. it( 'should load paragraph feature', () => {
  27. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  28. } );
  29. it( 'should set proper schema rules', () => {
  30. expect( document.schema.hasItem( 'heading1' ) ).to.be.true;
  31. expect( document.schema.hasItem( 'heading2' ) ).to.be.true;
  32. expect( document.schema.hasItem( 'heading3' ) ).to.be.true;
  33. expect( document.schema.check( { name: 'heading1', inside: '$root' } ) ).to.be.true;
  34. expect( document.schema.check( { name: '$inline', inside: 'heading1' } ) ).to.be.true;
  35. expect( document.schema.check( { name: 'heading2', inside: '$root' } ) ).to.be.true;
  36. expect( document.schema.check( { name: '$inline', inside: 'heading2' } ) ).to.be.true;
  37. expect( document.schema.check( { name: 'heading3', inside: '$root' } ) ).to.be.true;
  38. expect( document.schema.check( { name: '$inline', inside: 'heading3' } ) ).to.be.true;
  39. } );
  40. it( 'should register #commands', () => {
  41. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  42. expect( editor.commands.get( 'heading1' ) ).to.be.instanceOf( HeadingCommand );
  43. expect( editor.commands.get( 'heading2' ) ).to.be.instanceOf( HeadingCommand );
  44. expect( editor.commands.get( 'heading3' ) ).to.be.instanceOf( HeadingCommand );
  45. } );
  46. it( 'should convert heading1', () => {
  47. editor.setData( '<h2>foobar</h2>' );
  48. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  49. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  50. } );
  51. it( 'should convert heading2', () => {
  52. editor.setData( '<h3>foobar</h3>' );
  53. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
  54. expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
  55. } );
  56. it( 'should convert heading3', () => {
  57. editor.setData( '<h4>foobar</h4>' );
  58. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  59. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  60. } );
  61. it( 'should make enter command insert a defaultOption block if selection ended at the end of heading block', () => {
  62. editor.setData( '<h2>foobar</h2>' );
  63. document.selection.collapse( document.getRoot().getChild( 0 ), 'end' );
  64. editor.execute( 'enter' );
  65. expect( getData( document ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  66. } );
  67. it( 'should not alter enter command if selection not ended at the end of heading block', () => {
  68. // This test is to fill code coverage.
  69. editor.setData( '<h2>foobar</h2>' );
  70. document.selection.collapse( document.getRoot().getChild( 0 ), 3 );
  71. editor.execute( 'enter' );
  72. expect( getData( document ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  73. } );
  74. it( 'should not blow up if there\'s no enter command in the editor', () => {
  75. return VirtualTestEditor.create( {
  76. plugins: [ HeadingEngine ]
  77. } );
  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. { modelElement: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  85. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  86. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  87. { modelElement: 'heading3', viewElement: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
  88. ] );
  89. } );
  90. } );
  91. it( 'should customize options', () => {
  92. const options = [
  93. { modelElement: 'paragraph', title: 'Paragraph' },
  94. { modelElement: 'h4', viewElement: 'h4', title: '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( 'h4' ) ).to.be.instanceOf( HeadingCommand );
  105. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  106. expect( document.schema.hasItem( 'paragraph' ) ).to.be.true;
  107. expect( document.schema.hasItem( 'h4' ) ).to.be.true;
  108. expect( document.schema.hasItem( 'heading1' ) ).to.be.false;
  109. expect( document.schema.hasItem( 'heading2' ) ).to.be.false;
  110. expect( document.schema.hasItem( 'heading3' ) ).to.be.false;
  111. } );
  112. } );
  113. } );
  114. } );
  115. } );