deletecommand-integration.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 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( 'softBreak', { allowWhere: '$text', isInline: true } );
  37. model.schema.register( 'img', {
  38. allowWhere: '$text',
  39. isObject: true
  40. } );
  41. model.schema.extend( '$text', { allowIn: 'img' } );
  42. } );
  43. } );
  44. afterEach( () => {
  45. return editor.destroy();
  46. } );
  47. function assertOutput( output ) {
  48. expect( getData( model ) ).to.equal( output );
  49. }
  50. describe( 'with undo', () => {
  51. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  52. setData( model, '<paragraph>123456789[]</paragraph>' );
  53. for ( let i = 0; i < 3; ++i ) {
  54. editor.execute( 'delete' );
  55. }
  56. editor.execute( 'undo' );
  57. assertOutput( '<paragraph>123456789[]</paragraph>' );
  58. } );
  59. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  60. setData( model, '<paragraph>123456789[]</paragraph>' );
  61. for ( let i = 0; i < 6; ++i ) {
  62. editor.execute( 'delete' );
  63. }
  64. editor.execute( 'undo' );
  65. assertOutput( '<paragraph>123456[]</paragraph>' );
  66. editor.execute( 'undo' );
  67. assertOutput( '<paragraph>123456789[]</paragraph>' );
  68. } );
  69. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  70. setData( model, '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  71. for ( let i = 0; i < 3; ++i ) {
  72. editor.execute( 'delete' );
  73. }
  74. editor.execute( 'undo' );
  75. assertOutput( '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  76. } );
  77. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  78. setData( model, '<paragraph>123456</paragraph><paragraph>[]78</paragraph>' );
  79. for ( let i = 0; i < 6; ++i ) {
  80. editor.execute( 'delete' );
  81. }
  82. editor.execute( 'undo' );
  83. // Deleted 6,5,4, <paragraph> does not count.
  84. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  85. assertOutput( '<paragraph>123[]78</paragraph>' );
  86. editor.execute( 'undo' );
  87. // Selection restoing in undo is not 100% correct so slight miss-settings are expected as long as
  88. // the selection makes any sense and is near the correct position.
  89. assertOutput( '<paragraph>123456[]</paragraph><paragraph>78</paragraph>' );
  90. } );
  91. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  92. setData( model, '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  93. editor.execute( 'delete' );
  94. editor.execute( 'delete' );
  95. editor.execute( 'delete' );
  96. editor.execute( 'undo' );
  97. assertOutput( '<paragraph>1234[]8</paragraph>' );
  98. editor.execute( 'undo' );
  99. assertOutput( '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  100. } );
  101. } );
  102. describe( 'with DataController.deleteContent', () => {
  103. beforeEach( () => {
  104. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  105. } );
  106. it( 'should replace content with paragraph - if whole content is selected', () => {
  107. setData( model, '<h1>[foo</h1><paragraph>bar]</paragraph>' );
  108. editor.execute( 'delete' );
  109. assertOutput( '<paragraph>[]</paragraph>' );
  110. } );
  111. it( 'should not replace content with paragraph - if not whole content is selected', () => {
  112. setData( model, '<h1>f[oo</h1><paragraph>bar]</paragraph>' );
  113. editor.execute( 'delete' );
  114. assertOutput( '<h1>f[]</h1>' );
  115. } );
  116. it( 'should not replace content with paragraph - if selection was collapsed', () => {
  117. setData( model, '<h1></h1><paragraph>[]</paragraph>' );
  118. editor.execute( 'delete' );
  119. assertOutput( '<h1>[]</h1>' );
  120. } );
  121. } );
  122. describe( 'with multi-range selection', () => {
  123. let element, editor, model;
  124. beforeEach( () => {
  125. element = document.createElement( 'div' );
  126. document.body.appendChild( element );
  127. return ClassicEditor
  128. .create( element, {
  129. plugins: [ Typing, Heading, List, Image, ImageCaption, Paragraph, BlockQuote ]
  130. } )
  131. .then( newEditor => {
  132. editor = newEditor;
  133. model = editor.model;
  134. } );
  135. } );
  136. afterEach( () => {
  137. element.remove();
  138. return editor.destroy();
  139. } );
  140. it( 'should not throw an error if content with the image is being removed', () => {
  141. setData( model,
  142. '<listItem listIndent="0" listType="numbered">OL List i[tem 1</listItem>' +
  143. '<listItem listIndent="0" listType="numbered">OL List item 2</listItem>]' +
  144. '<image alt="bar" imageStyle="side" src="/assets/sample.png"><caption>[Caption</caption></image>' +
  145. '<blockQuote>' +
  146. '<paragraph>Quote</paragraph>' +
  147. '<listItem listIndent="0" listType="bulleted">Quoted UL List item 1</listItem>' +
  148. '<listItem listIndent="0" listType="bulleted">Quote]d UL List item 2</listItem>' +
  149. '<paragraph>Quote</paragraph>' +
  150. '</blockQuote>'
  151. );
  152. expect( () => {
  153. editor.execute( 'delete' );
  154. } ).to.not.throw();
  155. } );
  156. } );
  157. // See: https://github.com/ckeditor/ckeditor5/issues/1064
  158. it( 'should remove entire word in a paragraph that contains the soft break', () => {
  159. setData( model, '<paragraph>Foo.<softBreak></softBreak>Bar[]</paragraph>' );
  160. editor.execute( 'delete', { unit: 'word' } );
  161. assertOutput( '<paragraph>Foo.<softBreak></softBreak>[]</paragraph>' );
  162. } );
  163. } );