headingengine.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. describe( 'HeadingEngine', () => {
  12. let editor, document;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ 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 #commands', () => {
  40. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  41. expect( editor.commands.get( 'heading1' ) ).to.be.instanceOf( HeadingCommand );
  42. expect( editor.commands.get( 'heading2' ) ).to.be.instanceOf( HeadingCommand );
  43. expect( editor.commands.get( 'heading3' ) ).to.be.instanceOf( HeadingCommand );
  44. } );
  45. it( 'should convert heading1', () => {
  46. editor.setData( '<h2>foobar</h2>' );
  47. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  48. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  49. } );
  50. it( 'should convert heading2', () => {
  51. editor.setData( '<h3>foobar</h3>' );
  52. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
  53. expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
  54. } );
  55. it( 'should convert heading3', () => {
  56. editor.setData( '<h4>foobar</h4>' );
  57. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  58. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  59. } );
  60. it( 'should not blow up if there\'s no enter command in the editor', () => {
  61. return VirtualTestEditor.create( {
  62. plugins: [ HeadingEngine ]
  63. } );
  64. } );
  65. describe( 'config', () => {
  66. describe( 'options', () => {
  67. describe( 'default value', () => {
  68. it( 'should be set', () => {
  69. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  70. { modelElement: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  71. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  72. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  73. { modelElement: 'heading3', viewElement: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
  74. ] );
  75. } );
  76. } );
  77. it( 'should customize options', () => {
  78. const options = [
  79. { modelElement: 'paragraph', title: 'Paragraph' },
  80. { modelElement: 'h4', viewElement: 'h4', title: 'H4' }
  81. ];
  82. return VirtualTestEditor.create( {
  83. plugins: [ HeadingEngine ],
  84. heading: {
  85. options
  86. }
  87. } )
  88. .then( editor => {
  89. document = editor.document;
  90. expect( editor.commands.get( 'h4' ) ).to.be.instanceOf( HeadingCommand );
  91. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  92. expect( document.schema.hasItem( 'paragraph' ) ).to.be.true;
  93. expect( document.schema.hasItem( 'h4' ) ).to.be.true;
  94. expect( document.schema.hasItem( 'heading1' ) ).to.be.false;
  95. expect( document.schema.hasItem( 'heading2' ) ).to.be.false;
  96. expect( document.schema.hasItem( 'heading3' ) ).to.be.false;
  97. } );
  98. } );
  99. } );
  100. } );
  101. } );