8
0

deletecommand-integration.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. typing: {
  17. undoStep: 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. afterEach( () => {
  32. return editor.destroy();
  33. } );
  34. function assertOutput( output ) {
  35. expect( getData( doc ) ).to.equal( output );
  36. }
  37. describe( 'DeleteCommand integration', () => {
  38. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  39. setData( doc, '<p>123456789[]</p>' );
  40. for ( let i = 0; i < 3; ++i ) {
  41. editor.execute( 'delete' );
  42. }
  43. editor.execute( 'undo' );
  44. assertOutput( '<p>123456789[]</p>' );
  45. } );
  46. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  47. setData( doc, '<p>123456789[]</p>' );
  48. for ( let i = 0; i < 6; ++i ) {
  49. editor.execute( 'delete' );
  50. }
  51. editor.execute( 'undo' );
  52. assertOutput( '<p>123456[]</p>' );
  53. editor.execute( 'undo' );
  54. assertOutput( '<p>123456789[]</p>' );
  55. } );
  56. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  57. setData( doc, '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  58. for ( let i = 0; i < 3; ++i ) {
  59. editor.execute( 'delete' );
  60. }
  61. editor.execute( 'undo' );
  62. assertOutput( '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  63. } );
  64. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  65. setData( doc, '<p>123456</p><p>[]78</p>' );
  66. for ( let i = 0; i < 6; ++i ) {
  67. editor.execute( 'delete' );
  68. }
  69. editor.execute( 'undo' );
  70. // Deleted 6,5,4, <P> does not count.
  71. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  72. assertOutput( '<p>123[]78</p>' );
  73. editor.execute( 'undo' );
  74. assertOutput( '<p>123456</p><p>[]78</p>' );
  75. } );
  76. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  77. setData( doc, '<p>12345[6</p><p>7]8</p>' );
  78. editor.execute( 'delete' );
  79. editor.execute( 'delete' );
  80. editor.execute( 'delete' );
  81. editor.execute( 'undo' );
  82. assertOutput( '<p>1234[]8</p>' );
  83. editor.execute( 'undo' );
  84. assertOutput( '<p>12345[6</p><p>7]8</p>' );
  85. } );
  86. } );
  87. } );