deletecommand-integration.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, doc;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( {
  13. plugins: [
  14. UndoEngine
  15. ],
  16. undo: {
  17. step: 3
  18. }
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. const command = new DeleteCommand( editor, 'backward' );
  24. editor.commands.set( 'delete', command );
  25. doc.schema.registerItem( 'p', '$block' );
  26. doc.schema.registerItem( 'img', '$inline' );
  27. doc.schema.allow( { name: '$text', inside: 'img' } );
  28. doc.schema.objects.add( 'img' );
  29. } );
  30. } );
  31. function assertOutput( output ) {
  32. expect( getData( doc ) ).to.equal( output );
  33. }
  34. describe( 'DeleteCommand integration', () => {
  35. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  36. setData( doc, '<p>123456789[]</p>' );
  37. for ( let i = 0; i < 3; ++i ) {
  38. editor.execute( 'delete' );
  39. }
  40. editor.execute( 'undo' );
  41. assertOutput( '<p>123456789[]</p>' );
  42. } );
  43. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  44. setData( doc, '<p>123456789[]</p>' );
  45. for ( let i = 0; i < 6; ++i ) {
  46. editor.execute( 'delete' );
  47. }
  48. editor.execute( 'undo' );
  49. assertOutput( '<p>123456[]</p>' );
  50. editor.execute( 'undo' );
  51. assertOutput( '<p>123456789[]</p>' );
  52. } );
  53. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  54. setData( doc, '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  55. for ( let i = 0; i < 3; ++i ) {
  56. editor.execute( 'delete' );
  57. }
  58. editor.execute( 'undo' );
  59. assertOutput( '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  60. } );
  61. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  62. setData( doc, '<p>123456</p><p>[]78</p>' );
  63. for ( let i = 0; i < 6; ++i ) {
  64. editor.execute( 'delete' );
  65. }
  66. editor.execute( 'undo' );
  67. // Deleted 6,5,4, <P> does not count.
  68. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  69. assertOutput( '<p>123[]78</p>' );
  70. editor.execute( 'undo' );
  71. assertOutput( '<p>123456</p><p>[]78</p>' );
  72. } );
  73. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  74. setData( doc, '<p>12345[6</p><p>7]8</p>' );
  75. editor.execute( 'delete' );
  76. editor.execute( 'delete' );
  77. editor.execute( 'delete' );
  78. editor.execute( 'undo' );
  79. assertOutput( '<p>1234[]8</p>' );
  80. editor.execute( 'undo' );
  81. assertOutput( '<p>12345[6</p><p>7]8</p>' );
  82. } );
  83. } );
  84. } );