headingediting.js 8.3 KB

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