8
0

integration.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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, doc, element;
  16. beforeEach( () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. return ClassicTestEditor.create( element, {
  20. plugins: [ Paragraph, Heading, Enter, Image, ImageCaption, Undo ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. doc = editor.document;
  25. } );
  26. } );
  27. afterEach( () => {
  28. element.remove();
  29. return editor.destroy();
  30. } );
  31. describe( 'with the enter key', () => {
  32. it( 'should make the "enter" command insert a default heading block if the selection ended at the end of a heading block', () => {
  33. editor.setData( '<h2>foobar</h2>' );
  34. doc.selection.collapse( doc.getRoot().getChild( 0 ), 'end' );
  35. editor.execute( 'enter' );
  36. expect( getModelData( doc ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  37. } );
  38. it( 'should not alter the "enter" command if selection not ended at the end of a heading block', () => {
  39. // This test is to fill code coverage.
  40. editor.setData( '<h2>foobar</h2>' );
  41. doc.selection.collapse( doc.getRoot().getChild( 0 ), 3 );
  42. editor.execute( 'enter' );
  43. expect( getModelData( doc ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  44. } );
  45. } );
  46. describe( 'with the image feature', () => {
  47. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  48. it( 'should not destroy the image when a selection converted to a heading', () => {
  49. setModelData( editor.document,
  50. '<paragraph>fo[o</paragraph>' +
  51. '<image src="foo.png">' +
  52. '<caption>xxx</caption>' +
  53. '</image>' +
  54. '<paragraph>b]ar</paragraph>'
  55. );
  56. editor.execute( 'heading1' );
  57. expect( getModelData( doc ) ).to.equal(
  58. '<heading1>fo[o</heading1>' +
  59. '<image src="foo.png">' +
  60. '<caption>xxx</caption>' +
  61. '</image>' +
  62. '<heading1>b]ar</heading1>'
  63. );
  64. } );
  65. } );
  66. describe( 'with the undo feature', () => {
  67. it( 'does not create undo steps when applied to an existing heading (collapsed selection)', () => {
  68. setModelData( editor.document, '<heading1>foo[]bar</heading1>' );
  69. editor.execute( 'heading1' );
  70. expect( getModelData( editor.document ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  71. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  72. } );
  73. it( 'does not create undo steps when applied to an existing heading (non–collapsed selection)', () => {
  74. setModelData( editor.document, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  75. editor.execute( 'heading1' );
  76. expect( getModelData( editor.document ) ).to.equal( '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  77. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  78. } );
  79. } );
  80. } );