deletecommand-integration.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import Typing from '../src/typing';
  7. import DeleteCommand from '../src/deletecommand';
  8. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  9. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  10. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  11. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  12. import List from '@ckeditor/ckeditor5-list/src/list';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Image from '@ckeditor/ckeditor5-image/src/image';
  15. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  16. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  17. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. describe( 'DeleteCommand integration', () => {
  19. let editor, model;
  20. beforeEach( () => {
  21. return ModelTestEditor
  22. .create( {
  23. plugins: [ UndoEditing ],
  24. typing: {
  25. undoStep: 3
  26. }
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = editor.model;
  31. const command = new DeleteCommand( editor, 'backward' );
  32. editor.commands.add( 'delete', command );
  33. // Mock paragraph feature.
  34. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  35. model.schema.extend( 'paragraph', { allowIn: '$block' } );
  36. model.schema.register( 'img', {
  37. allowWhere: '$text',
  38. isObject: true
  39. } );
  40. model.schema.extend( '$text', { allowIn: 'img' } );
  41. } );
  42. } );
  43. afterEach( () => {
  44. return editor.destroy();
  45. } );
  46. function assertOutput( output ) {
  47. expect( getData( model ) ).to.equal( output );
  48. }
  49. describe( 'with undo', () => {
  50. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  51. setData( model, '<paragraph>123456789[]</paragraph>' );
  52. for ( let i = 0; i < 3; ++i ) {
  53. editor.execute( 'delete' );
  54. }
  55. editor.execute( 'undo' );
  56. assertOutput( '<paragraph>123456789[]</paragraph>' );
  57. } );
  58. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  59. setData( model, '<paragraph>123456789[]</paragraph>' );
  60. for ( let i = 0; i < 6; ++i ) {
  61. editor.execute( 'delete' );
  62. }
  63. editor.execute( 'undo' );
  64. assertOutput( '<paragraph>123456[]</paragraph>' );
  65. editor.execute( 'undo' );
  66. assertOutput( '<paragraph>123456789[]</paragraph>' );
  67. } );
  68. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  69. setData( model, '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  70. for ( let i = 0; i < 3; ++i ) {
  71. editor.execute( 'delete' );
  72. }
  73. editor.execute( 'undo' );
  74. assertOutput( '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  75. } );
  76. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  77. setData( model, '<paragraph>123456</paragraph><paragraph>[]78</paragraph>' );
  78. for ( let i = 0; i < 6; ++i ) {
  79. editor.execute( 'delete' );
  80. }
  81. editor.execute( 'undo' );
  82. // Deleted 6,5,4, <paragraph> does not count.
  83. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  84. assertOutput( '<paragraph>123[]78</paragraph>' );
  85. editor.execute( 'undo' );
  86. // Selection restoing in undo is not 100% correct so slight miss-settings are expected as long as
  87. // the selection makes any sense and is near the correct position.
  88. assertOutput( '<paragraph>123456</paragraph><paragraph>78[]</paragraph>' );
  89. } );
  90. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  91. setData( model, '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  92. editor.execute( 'delete' );
  93. editor.execute( 'delete' );
  94. editor.execute( 'delete' );
  95. editor.execute( 'undo' );
  96. assertOutput( '<paragraph>1234[]8</paragraph>' );
  97. editor.execute( 'undo' );
  98. assertOutput( '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  99. } );
  100. } );
  101. describe( 'with DataController.deleteContent', () => {
  102. beforeEach( () => {
  103. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  104. } );
  105. it( 'should replace content with paragraph - if whole content is selected', () => {
  106. setData( model, '<h1>[foo</h1><paragraph>bar]</paragraph>' );
  107. editor.execute( 'delete' );
  108. assertOutput( '<paragraph>[]</paragraph>' );
  109. } );
  110. it( 'should not replace content with paragraph - if not whole content is selected', () => {
  111. setData( model, '<h1>f[oo</h1><paragraph>bar]</paragraph>' );
  112. editor.execute( 'delete' );
  113. assertOutput( '<h1>f[]</h1>' );
  114. } );
  115. it( 'should not replace content with paragraph - if selection was collapsed', () => {
  116. setData( model, '<h1></h1><paragraph>[]</paragraph>' );
  117. editor.execute( 'delete' );
  118. assertOutput( '<h1>[]</h1>' );
  119. } );
  120. } );
  121. describe( 'with multi-range selection', () => {
  122. let element, editor, model;
  123. beforeEach( () => {
  124. element = document.createElement( 'div' );
  125. document.body.appendChild( element );
  126. return ClassicEditor
  127. .create( element, {
  128. plugins: [ Typing, Heading, List, Image, ImageCaption, Paragraph, BlockQuote ]
  129. } )
  130. .then( newEditor => {
  131. editor = newEditor;
  132. model = editor.model;
  133. } );
  134. } );
  135. afterEach( () => {
  136. element.remove();
  137. return editor.destroy();
  138. } );
  139. it( 'should not throw an error if content with the image is being removed', () => {
  140. setData( model,
  141. '<listItem listIndent="0" listType="numbered">OL List i[tem 1</listItem>' +
  142. '<listItem listIndent="0" listType="numbered">OL List item 2</listItem>]' +
  143. '<image alt="bar" imageStyle="side" src="sample.jpg"><caption>[Caption</caption></image>' +
  144. '<blockQuote>' +
  145. '<paragraph>Quote</paragraph>' +
  146. '<listItem listIndent="0" listType="bulleted">Quoted UL List item 1</listItem>' +
  147. '<listItem listIndent="0" listType="bulleted">Quote]d UL List item 2</listItem>' +
  148. '<paragraph>Quote</paragraph>' +
  149. '</blockQuote>'
  150. );
  151. expect( () => {
  152. editor.execute( 'delete' );
  153. } ).to.not.throw();
  154. } );
  155. } );
  156. } );