integration.js 3.4 KB

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