headingediting.js 8.2 KB

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