deletecommand-integration.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.js';
  7. import DeleteCommand from '/ckeditor5/typing/deletecommand.js';
  8. import UndoEngine from '/ckeditor5/undo/undoengine.js';
  9. import { getData, setData } from '/tests/engine/_utils/model.js';
  10. let editor, doc;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( {
  13. features: [
  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. } );
  29. } );
  30. function assertOutput( output ) {
  31. expect( getData( doc ) ).to.equal( output );
  32. }
  33. describe( 'DeleteCommand integration', () => {
  34. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  35. setData( doc, '<p>123456789<selection /></p>' );
  36. for ( let i = 0; i < 3; ++i ) {
  37. editor.execute( 'delete' );
  38. }
  39. editor.execute( 'undo' );
  40. assertOutput( '<p>123456789<selection /></p>' );
  41. } );
  42. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  43. setData( doc, '<p>123456789<selection /></p>' );
  44. for ( let i = 0; i < 6; ++i ) {
  45. editor.execute( 'delete' );
  46. }
  47. editor.execute( 'undo' );
  48. assertOutput( '<p>123456<selection /></p>' );
  49. editor.execute( 'undo' );
  50. assertOutput( '<p>123456789<selection /></p>' );
  51. } );
  52. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  53. setData( doc, '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img><selection /></p>' );
  54. for ( let i = 0; i < 3; ++i ) {
  55. editor.execute( 'delete' );
  56. }
  57. editor.execute( 'undo' );
  58. assertOutput( '<p><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img><selection /></p>' );
  59. } );
  60. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  61. setData( doc, '<p>123456</p><p><selection />78</p>' );
  62. for ( let i = 0; i < 6; ++i ) {
  63. editor.execute( 'delete' );
  64. }
  65. editor.execute( 'undo' );
  66. // Deleted 6,5,4, <P> does not count.
  67. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm.
  68. assertOutput( '<p>123<selection />78</p>' );
  69. editor.execute( 'undo' );
  70. assertOutput( '<p>123456</p><p><selection />78</p>' );
  71. } );
  72. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  73. setData( doc, '<p>12345<selection>6</p><p>7</selection>8</p>' );
  74. editor.execute( 'delete' );
  75. editor.execute( 'delete' );
  76. editor.execute( 'delete' );
  77. editor.execute( 'undo' );
  78. assertOutput( '<p>1234<selection />8</p>' );
  79. editor.execute( 'undo' );
  80. assertOutput( '<p>12345<selection>6</p><p>7</selection>8</p>' );
  81. } );
  82. } );