8
0

integration.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /* global document */
  6. import Heading from '../src/heading.js';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import Image from '@ckeditor/ckeditor5-image/src/image';
  10. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  11. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  12. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  13. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'Heading integration', () => {
  15. let editor, model, doc, element;
  16. beforeEach( () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. return ClassicTestEditor
  20. .create( element, {
  21. plugins: [ Paragraph, Heading, Enter, Image, ImageCaption, Undo ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. doc = model.document;
  27. } );
  28. } );
  29. afterEach( () => {
  30. element.remove();
  31. return editor.destroy();
  32. } );
  33. describe( 'with the enter key', () => {
  34. it( 'should make the "enter" command insert a default heading block if the selection ended at the end of a heading block', () => {
  35. editor.setData( '<h2>foobar</h2>' );
  36. model.change( writer => {
  37. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  38. } );
  39. editor.execute( 'enter' );
  40. expect( getModelData( model ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  41. } );
  42. it( 'should not alter the "enter" command if selection not ended at the end of a heading block', () => {
  43. // This test is to fill code coverage.
  44. editor.setData( '<h2>foobar</h2>' );
  45. model.change( writer => {
  46. writer.setSelection( doc.getRoot().getChild( 0 ), 3 );
  47. } );
  48. editor.execute( 'enter' );
  49. expect( getModelData( model ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  50. } );
  51. } );
  52. describe( 'with the image feature', () => {
  53. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  54. it( 'should not destroy the image when a selection converted to a heading', () => {
  55. setModelData( model,
  56. '<paragraph>fo[o</paragraph>' +
  57. '<image src="/assets/sample.png">' +
  58. '<caption>xxx</caption>' +
  59. '</image>' +
  60. '<paragraph>b]ar</paragraph>'
  61. );
  62. editor.execute( 'heading', { value: 'heading1' } );
  63. expect( getModelData( model ) ).to.equal(
  64. '<heading1>fo[o</heading1>' +
  65. '<image src="/assets/sample.png">' +
  66. '<caption>xxx</caption>' +
  67. '</image>' +
  68. '<heading1>b]ar</heading1>'
  69. );
  70. } );
  71. } );
  72. describe( 'with the undo feature', () => {
  73. it( 'does not create undo steps when applied to an existing heading (collapsed selection)', () => {
  74. // Ensure no undo step by using a transparent batch.
  75. model.enqueueChange( 'transparent', () => {
  76. setModelData( model, '<heading1>foo[]bar</heading1>' );
  77. } );
  78. editor.execute( 'heading', { value: 'heading1' } );
  79. expect( getModelData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  80. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  81. } );
  82. it( 'does not create undo steps when applied to an existing heading (non–collapsed selection)', () => {
  83. // Ensure no undo step by using a transparent batch.
  84. model.enqueueChange( 'transparent', () => {
  85. setModelData( model, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  86. } );
  87. editor.execute( 'heading', { value: 'heading1' } );
  88. expect( getModelData( model ) ).to.equal( '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  89. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  90. } );
  91. } );
  92. // Remember to sync docs/_snippets/features/custom-heading-elements.js and docs/features/headings.md
  93. // with this test when changing it.
  94. describe( 'fancy heading sample in the docs', () => {
  95. it( 'upcasts the <h2> and <h2 class=fancy> elements when configured to do so', () => {
  96. const element = document.createElement( 'div' );
  97. document.body.appendChild( element );
  98. return ClassicTestEditor
  99. .create( element, {
  100. plugins: [ Paragraph, Heading ],
  101. heading: {
  102. options: [
  103. { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  104. { model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
  105. {
  106. model: 'headingFancy',
  107. view: {
  108. name: 'h2',
  109. classes: 'fancy'
  110. },
  111. title: 'Heading 2 (fancy)',
  112. class: 'ck-heading_heading2_fancy',
  113. converterPriority: 'high'
  114. }
  115. ]
  116. }
  117. } )
  118. .then( editor => {
  119. editor.setData( '<h2>Heading 2</h2><h2 class="fancy">Fancy Heading 2</h2>' );
  120. expect( editor.getData() )
  121. .to.equal( '<h2>Heading 2</h2><h2 class="fancy">Fancy Heading 2</h2>' );
  122. editor.destroy();
  123. element.remove();
  124. } );
  125. } );
  126. } );
  127. } );