8
0

deletecommand-integration.js 3.0 KB

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