headingediting.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import HeadingEditing from '../src/headingediting';
  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( 'HeadingEditing', () => {
  12. let editor, model;
  13. beforeEach( () => {
  14. return VirtualTestEditor
  15. .create( {
  16. plugins: [ HeadingEditing ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. } );
  22. } );
  23. it( 'should be loaded', () => {
  24. expect( editor.plugins.get( HeadingEditing ) ).to.be.instanceOf( HeadingEditing );
  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( model.schema.isRegistered( 'heading1' ) ).to.be.true;
  31. expect( model.schema.isRegistered( 'heading2' ) ).to.be.true;
  32. expect( model.schema.isRegistered( 'heading3' ) ).to.be.true;
  33. expect( model.schema.checkChild( [ '$root' ], 'heading1' ) ).to.be.true;
  34. expect( model.schema.checkChild( [ 'heading1' ], '$text' ) ).to.be.true;
  35. expect( model.schema.checkChild( [ '$root' ], 'heading2' ) ).to.be.true;
  36. expect( model.schema.checkChild( [ 'heading2' ], '$text' ) ).to.be.true;
  37. expect( model.schema.checkChild( [ '$root' ], 'heading3' ) ).to.be.true;
  38. expect( model.schema.checkChild( [ 'heading3' ], '$text' ) ).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( model, { 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( model, { 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( model, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  59. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  60. } );
  61. describe( 'user defined', () => {
  62. beforeEach( () => {
  63. return VirtualTestEditor
  64. .create( {
  65. plugins: [ HeadingEditing ],
  66. heading: {
  67. options: [
  68. { model: 'paragraph', title: 'paragraph' },
  69. {
  70. model: 'heading1',
  71. view: 'h1',
  72. upcastAlso: [
  73. { name: 'p', attribute: { 'data-heading': 'h1' } }
  74. ],
  75. title: 'User H1',
  76. priority: 'high'
  77. }
  78. ]
  79. }
  80. } )
  81. .then( newEditor => {
  82. editor = newEditor;
  83. model = editor.model;
  84. } );
  85. } );
  86. it( 'should convert from defined h element', () => {
  87. editor.setData( '<h1>foobar</h1>' );
  88. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  89. expect( editor.getData() ).to.equal( '<h1>foobar</h1>' );
  90. } );
  91. it( 'should convert from defined paragraph with attributes', () => {
  92. editor.setData( '<p data-heading="h1">foobar</p><p>Normal paragraph</p>' );
  93. expect( getData( model, { withoutSelection: true } ) )
  94. .to.equal( '<heading1>foobar</heading1><paragraph>Normal paragraph</paragraph>' );
  95. expect( editor.getData() ).to.equal( '<h1>foobar</h1><p>Normal paragraph</p>' );
  96. } );
  97. } );
  98. it( 'should not blow up if there\'s no enter command in the editor', () => {
  99. return VirtualTestEditor
  100. .create( {
  101. plugins: [ HeadingEditing ]
  102. } );
  103. } );
  104. describe( 'config', () => {
  105. describe( 'options', () => {
  106. describe( 'default value', () => {
  107. it( 'should be set', () => {
  108. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  109. { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  110. { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  111. { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  112. { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
  113. ] );
  114. } );
  115. } );
  116. it( 'should customize options', () => {
  117. const options = [
  118. { model: 'paragraph', title: 'Paragraph' },
  119. { model: 'h4', view: { name: 'h4' }, title: 'H4' }
  120. ];
  121. return VirtualTestEditor
  122. .create( {
  123. plugins: [ HeadingEditing ],
  124. heading: {
  125. options
  126. }
  127. } )
  128. .then( editor => {
  129. model = editor.model;
  130. expect( editor.commands.get( 'h4' ) ).to.be.instanceOf( HeadingCommand );
  131. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  132. expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
  133. expect( model.schema.isRegistered( 'h4' ) ).to.be.true;
  134. expect( model.schema.isRegistered( 'heading1' ) ).to.be.false;
  135. expect( model.schema.isRegistered( 'heading2' ) ).to.be.false;
  136. expect( model.schema.isRegistered( 'heading3' ) ).to.be.false;
  137. } );
  138. } );
  139. } );
  140. } );
  141. } );