deletecommand-integration.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import DeleteCommand from 'ckeditor5/typing/deletecommand.js';
  7. import UndoEngine from 'ckeditor5/undo/undoengine.js';
  8. import { getData, setData } from 'ckeditor5/engine/dev-utils/model.js';
  9. let editor, doc;
  10. beforeEach( () => {
  11. return ModelTestEditor.create( {
  12. features: [
  13. UndoEngine
  14. ],
  15. undo: {
  16. step: 3
  17. }
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. doc = editor.document;
  22. const command = new DeleteCommand( editor, 'backward' );
  23. editor.commands.set( 'delete', command );
  24. doc.schema.registerItem( 'p', '$block' );
  25. doc.schema.registerItem( 'img', '$inline' );
  26. doc.schema.allow( { name: '$text', inside: 'img' } );
  27. } );
  28. } );
  29. function assertOutput( output ) {
  30. expect( getData( doc ) ).to.equal( output );
  31. }
  32. describe( 'DeleteCommand integration', () => {
  33. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  34. setData( doc, '<p>123456789[]</p>' );
  35. for ( let i = 0; i < 3; ++i ) {
  36. editor.execute( 'delete' );
  37. }
  38. editor.execute( 'undo' );
  39. assertOutput( '<p>123456789[]</p>' );
  40. } );
  41. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  42. setData( doc, '<p>123456789[]</p>' );
  43. for ( let i = 0; i < 6; ++i ) {
  44. editor.execute( 'delete' );
  45. }
  46. editor.execute( 'undo' );
  47. assertOutput( '<p>123456[]</p>' );
  48. editor.execute( 'undo' );
  49. assertOutput( '<p>123456789[]</p>' );
  50. } );
  51. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  52. setData( doc, '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  53. for ( let i = 0; i < 3; ++i ) {
  54. editor.execute( 'delete' );
  55. }
  56. editor.execute( 'undo' );
  57. assertOutput( '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</p>' );
  58. } );
  59. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  60. setData( doc, '<p>123456</p><p>[]78</p>' );
  61. for ( let i = 0; i < 6; ++i ) {
  62. editor.execute( 'delete' );
  63. }
  64. editor.execute( 'undo' );
  65. // Deleted 6,5,4, <P> does not count.
  66. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  67. assertOutput( '<p>123[]78</p>' );
  68. editor.execute( 'undo' );
  69. assertOutput( '<p>123456</p><p>[]78</p>' );
  70. } );
  71. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  72. setData( doc, '<p>12345[6</p><p>7]8</p>' );
  73. editor.execute( 'delete' );
  74. editor.execute( 'delete' );
  75. editor.execute( 'delete' );
  76. editor.execute( 'undo' );
  77. assertOutput( '<p>1234[]8</p>' );
  78. editor.execute( 'undo' );
  79. assertOutput( '<p>12345[6</p><p>7]8</p>' );
  80. } );
  81. } );