integration.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 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. doc.selection.setCollapsedAt( doc.getRoot().getChild( 0 ), 'end' );
  37. editor.execute( 'enter' );
  38. expect( getModelData( model ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  39. } );
  40. it( 'should not alter the "enter" command if selection not ended at the end of a heading block', () => {
  41. // This test is to fill code coverage.
  42. editor.setData( '<h2>foobar</h2>' );
  43. doc.selection.setCollapsedAt( doc.getRoot().getChild( 0 ), 3 );
  44. editor.execute( 'enter' );
  45. expect( getModelData( model ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  46. } );
  47. } );
  48. describe( 'with the image feature', () => {
  49. // https://github.com/ckeditor/ckeditor5-heading/issues/73
  50. it( 'should not destroy the image when a selection converted to a heading', () => {
  51. setModelData( model,
  52. '<paragraph>fo[o</paragraph>' +
  53. '<image src="foo.png">' +
  54. '<caption>xxx</caption>' +
  55. '</image>' +
  56. '<paragraph>b]ar</paragraph>'
  57. );
  58. editor.execute( 'heading1' );
  59. expect( getModelData( model ) ).to.equal(
  60. '<heading1>fo[o</heading1>' +
  61. '<image src="foo.png">' +
  62. '<caption>xxx</caption>' +
  63. '</image>' +
  64. '<heading1>b]ar</heading1>'
  65. );
  66. } );
  67. } );
  68. describe( 'with the undo feature', () => {
  69. it( 'does not create undo steps when applied to an existing heading (collapsed selection)', () => {
  70. setModelData( model, '<heading1>foo[]bar</heading1>' );
  71. editor.execute( 'heading1' );
  72. expect( getModelData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  73. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  74. } );
  75. it( 'does not create undo steps when applied to an existing heading (non–collapsed selection)', () => {
  76. setModelData( model, '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  77. editor.execute( 'heading1' );
  78. expect( getModelData( model ) ).to.equal( '<heading1>[foo</heading1><heading1>bar]</heading1>' );
  79. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  80. } );
  81. } );
  82. } );