deletecommand-integration.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import DeleteCommand from '../src/deletecommand';
  7. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'DeleteCommand integration', () => {
  10. let editor, model;
  11. beforeEach( () => {
  12. return ModelTestEditor
  13. .create( {
  14. plugins: [ UndoEngine ],
  15. typing: {
  16. undoStep: 3
  17. }
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. const command = new DeleteCommand( editor, 'backward' );
  23. editor.commands.add( 'delete', command );
  24. // Mock paragraph feature.
  25. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  26. model.schema.extend( 'paragraph', { allowIn: '$block' } );
  27. model.schema.register( 'img', {
  28. allowWhere: '$text',
  29. isObject: true
  30. } );
  31. model.schema.extend( '$text', { allowIn: 'img' } );
  32. } );
  33. } );
  34. afterEach( () => {
  35. return editor.destroy();
  36. } );
  37. function assertOutput( output ) {
  38. expect( getData( model ) ).to.equal( output );
  39. }
  40. describe( 'with undo', () => {
  41. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  42. setData( model, '<paragraph>123456789[]</paragraph>' );
  43. for ( let i = 0; i < 3; ++i ) {
  44. editor.execute( 'delete' );
  45. }
  46. editor.execute( 'undo' );
  47. assertOutput( '<paragraph>123456789[]</paragraph>' );
  48. } );
  49. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  50. setData( model, '<paragraph>123456789[]</paragraph>' );
  51. for ( let i = 0; i < 6; ++i ) {
  52. editor.execute( 'delete' );
  53. }
  54. editor.execute( 'undo' );
  55. assertOutput( '<paragraph>123456[]</paragraph>' );
  56. editor.execute( 'undo' );
  57. assertOutput( '<paragraph>123456789[]</paragraph>' );
  58. } );
  59. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  60. setData( model, '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  61. for ( let i = 0; i < 3; ++i ) {
  62. editor.execute( 'delete' );
  63. }
  64. editor.execute( 'undo' );
  65. assertOutput( '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  66. } );
  67. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  68. setData( model, '<paragraph>123456</paragraph><paragraph>[]78</paragraph>' );
  69. for ( let i = 0; i < 6; ++i ) {
  70. editor.execute( 'delete' );
  71. }
  72. editor.execute( 'undo' );
  73. // Deleted 6,5,4, <paragraph> does not count.
  74. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  75. assertOutput( '<paragraph>123[]78</paragraph>' );
  76. editor.execute( 'undo' );
  77. // Selection restoing in undo is not 100% correct so slight miss-settings are expected as long as
  78. // the selection makes any sense and is near the correct position.
  79. assertOutput( '<paragraph>123456</paragraph><paragraph>78[]</paragraph>' );
  80. } );
  81. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  82. setData( model, '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  83. editor.execute( 'delete' );
  84. editor.execute( 'delete' );
  85. editor.execute( 'delete' );
  86. editor.execute( 'undo' );
  87. assertOutput( '<paragraph>1234[]8</paragraph>' );
  88. editor.execute( 'undo' );
  89. assertOutput( '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  90. } );
  91. } );
  92. describe( 'with DataController.deleteContent', () => {
  93. beforeEach( () => {
  94. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  95. } );
  96. it( 'should replace content with paragraph - if whole content is selected', () => {
  97. setData( model, '<h1>[foo</h1><paragraph>bar]</paragraph>' );
  98. editor.execute( 'delete' );
  99. assertOutput( '<paragraph>[]</paragraph>' );
  100. } );
  101. it( 'should not replace content with paragraph - if not whole content is selected', () => {
  102. setData( model, '<h1>f[oo</h1><paragraph>bar]</paragraph>' );
  103. editor.execute( 'delete' );
  104. assertOutput( '<h1>f[]</h1>' );
  105. } );
  106. it( 'should not replace content with paragraph - if selection was collapsed', () => {
  107. setData( model, '<h1></h1><paragraph>[]</paragraph>' );
  108. editor.execute( 'delete' );
  109. assertOutput( '<h1>[]</h1>' );
  110. } );
  111. } );
  112. } );