8
0

headingediting.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Conversion from '@ckeditor/ckeditor5-engine/src/conversion/conversion';
  10. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
  13. import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'HeadingEditing', () => {
  15. let editor, model;
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( {
  19. plugins: [ HeadingEditing ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. } );
  25. } );
  26. it( 'should be loaded', () => {
  27. expect( editor.plugins.get( HeadingEditing ) ).to.be.instanceOf( HeadingEditing );
  28. } );
  29. it( 'should load paragraph feature', () => {
  30. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  31. } );
  32. it( 'should set proper schema rules', () => {
  33. expect( model.schema.isRegistered( 'heading1' ) ).to.be.true;
  34. expect( model.schema.isRegistered( 'heading2' ) ).to.be.true;
  35. expect( model.schema.isRegistered( 'heading3' ) ).to.be.true;
  36. expect( model.schema.checkChild( [ '$root' ], 'heading1' ) ).to.be.true;
  37. expect( model.schema.checkChild( [ 'heading1' ], '$text' ) ).to.be.true;
  38. expect( model.schema.checkChild( [ '$root' ], 'heading2' ) ).to.be.true;
  39. expect( model.schema.checkChild( [ 'heading2' ], '$text' ) ).to.be.true;
  40. expect( model.schema.checkChild( [ '$root' ], 'heading3' ) ).to.be.true;
  41. expect( model.schema.checkChild( [ 'heading3' ], '$text' ) ).to.be.true;
  42. } );
  43. it( 'should register #commands', () => {
  44. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
  45. expect( editor.commands.get( 'heading' ) ).to.be.instanceOf( HeadingCommand );
  46. } );
  47. it( 'should convert heading1', () => {
  48. editor.setData( '<h2>foobar</h2>' );
  49. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  50. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  51. } );
  52. it( 'should convert heading2', () => {
  53. editor.setData( '<h3>foobar</h3>' );
  54. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
  55. expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
  56. } );
  57. it( 'should convert heading3', () => {
  58. editor.setData( '<h4>foobar</h4>' );
  59. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  60. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  61. } );
  62. it( 'should convert h1 to heading1', () => {
  63. editor.setData( '<h1>foobar</h1>' );
  64. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  65. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  66. } );
  67. describe( 'user defined', () => {
  68. beforeEach( () => {
  69. return VirtualTestEditor
  70. .create( {
  71. plugins: [ HeadingEditing ],
  72. heading: {
  73. options: [
  74. { model: 'paragraph', title: 'paragraph' },
  75. {
  76. model: 'heading1',
  77. view: 'h1',
  78. upcastAlso: [
  79. { name: 'p', attributes: { 'data-heading': 'h1' } }
  80. ],
  81. title: 'User H1',
  82. converterPriority: 'high'
  83. }
  84. ]
  85. }
  86. } )
  87. .then( newEditor => {
  88. editor = newEditor;
  89. model = editor.model;
  90. } );
  91. } );
  92. it( 'should convert from defined h element', () => {
  93. editor.setData( '<h1>foobar</h1>' );
  94. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  95. expect( editor.getData() ).to.equal( '<h1>foobar</h1>' );
  96. } );
  97. it( 'should convert from defined paragraph with attributes', () => {
  98. editor.setData( '<p data-heading="h1">foobar</p><p>Normal paragraph</p>' );
  99. expect( getData( model, { withoutSelection: true } ) )
  100. .to.equal( '<heading1>foobar</heading1><paragraph>Normal paragraph</paragraph>' );
  101. expect( editor.getData() ).to.equal( '<h1>foobar</h1><p>Normal paragraph</p>' );
  102. } );
  103. } );
  104. describe( 'default h1 conversion', () => {
  105. let elementToElementSpy;
  106. testUtils.createSinonSandbox();
  107. beforeEach( () => {
  108. elementToElementSpy = testUtils.sinon.spy( Conversion.prototype, 'elementToElement' );
  109. } );
  110. it( 'should set default h1 conversion with default `options.heading` config', () => {
  111. return VirtualTestEditor
  112. .create( {
  113. plugins: [ HeadingEditing ]
  114. } )
  115. .then( () => {
  116. expect( elementToElementSpy.calledWith( {
  117. model: 'heading1',
  118. view: 'h1',
  119. title: 'Heading 1',
  120. class: 'ck-heading_heading1',
  121. converterPriority: priorities.get( 'low' ) + 1
  122. } ) ).to.true;
  123. } );
  124. } );
  125. it( 'should set default h1 conversion independently from `options.heading` config', () => {
  126. const options = [
  127. { model: 'heading1', view: 'h3', title: 'User H3' },
  128. { model: 'heading2', view: 'h4', title: 'User H4' }
  129. ];
  130. return VirtualTestEditor
  131. .create( {
  132. plugins: [ HeadingEditing ],
  133. heading: { options }
  134. } )
  135. .then( () => {
  136. expect( elementToElementSpy.calledWith( {
  137. model: 'heading1',
  138. view: 'h1',
  139. title: 'Heading 1',
  140. class: 'ck-heading_heading1',
  141. converterPriority: priorities.get( 'low' ) + 1
  142. } ) ).to.true;
  143. } );
  144. } );
  145. } );
  146. it( 'should not blow up if there\'s no enter command in the editor', () => {
  147. return VirtualTestEditor
  148. .create( {
  149. plugins: [ HeadingEditing ]
  150. } );
  151. } );
  152. describe( 'config', () => {
  153. describe( 'options', () => {
  154. describe( 'default value', () => {
  155. it( 'should be set', () => {
  156. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  157. { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  158. { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  159. { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  160. { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
  161. ] );
  162. } );
  163. } );
  164. it( 'should customize options', () => {
  165. const options = [
  166. { model: 'paragraph', title: 'Paragraph' },
  167. { model: 'h4', view: { name: 'h4' }, title: 'H4' }
  168. ];
  169. return VirtualTestEditor
  170. .create( {
  171. plugins: [ HeadingEditing ],
  172. heading: {
  173. options
  174. }
  175. } )
  176. .then( editor => {
  177. model = editor.model;
  178. expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
  179. expect( model.schema.isRegistered( 'h4' ) ).to.be.true;
  180. expect( model.schema.isRegistered( 'heading1' ) ).to.be.false;
  181. expect( model.schema.isRegistered( 'heading2' ) ).to.be.false;
  182. expect( model.schema.isRegistered( 'heading3' ) ).to.be.false;
  183. } );
  184. } );
  185. } );
  186. } );
  187. } );