deletecommand-integration.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.add( 'delete', command );
  18. // Mock paragraph feature.
  19. doc.schema.registerItem( 'paragraph', '$block' );
  20. doc.schema.allow( { name: 'paragraph', inside: '$block' } );
  21. doc.schema.registerItem( 'img', '$inline' );
  22. doc.schema.allow( { name: '$text', inside: 'img' } );
  23. doc.schema.objects.add( 'img' );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. function assertOutput( output ) {
  30. expect( getData( doc ) ).to.equal( output );
  31. }
  32. describe( 'with undo', () => {
  33. it( 'deletes characters (and group changes in batches) and rollbacks', () => {
  34. setData( doc, '<paragraph>123456789[]</paragraph>' );
  35. for ( let i = 0; i < 3; ++i ) {
  36. editor.execute( 'delete' );
  37. }
  38. editor.execute( 'undo' );
  39. assertOutput( '<paragraph>123456789[]</paragraph>' );
  40. } );
  41. it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => {
  42. setData( doc, '<paragraph>123456789[]</paragraph>' );
  43. for ( let i = 0; i < 6; ++i ) {
  44. editor.execute( 'delete' );
  45. }
  46. editor.execute( 'undo' );
  47. assertOutput( '<paragraph>123456[]</paragraph>' );
  48. editor.execute( 'undo' );
  49. assertOutput( '<paragraph>123456789[]</paragraph>' );
  50. } );
  51. it( 'deletes elements (and group changes in batches) and rollbacks', () => {
  52. setData( doc, '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  53. for ( let i = 0; i < 3; ++i ) {
  54. editor.execute( 'delete' );
  55. }
  56. editor.execute( 'undo' );
  57. assertOutput( '<paragraph><img>1</img><img>2</img><img>3</img><img>4</img><img>5</img><img>6</img>[]</paragraph>' );
  58. } );
  59. it( 'merges elements (and group changes in batches) and rollbacks', () => {
  60. setData( doc, '<paragraph>123456</paragraph><paragraph>[]78</paragraph>' );
  61. for ( let i = 0; i < 6; ++i ) {
  62. editor.execute( 'delete' );
  63. }
  64. editor.execute( 'undo' );
  65. // Deleted 6,5,4, <paragraph> 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( '<paragraph>123[]78</paragraph>' );
  68. editor.execute( 'undo' );
  69. // Selection restoing in undo is not 100% correct so slight miss-settings are expected as long as
  70. // the selection makes any sense and is near the correct position.
  71. assertOutput( '<paragraph>123456</paragraph><paragraph>78[]</paragraph>' );
  72. } );
  73. it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => {
  74. setData( doc, '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  75. editor.execute( 'delete' );
  76. editor.execute( 'delete' );
  77. editor.execute( 'delete' );
  78. editor.execute( 'undo' );
  79. assertOutput( '<paragraph>1234[]8</paragraph>' );
  80. editor.execute( 'undo' );
  81. assertOutput( '<paragraph>12345[6</paragraph><paragraph>7]8</paragraph>' );
  82. } );
  83. } );
  84. describe( 'with DataController.deleteContent', () => {
  85. beforeEach( () => {
  86. doc.schema.registerItem( 'h1', '$block' );
  87. } );
  88. it( 'should replace content with paragraph - if whole content is selected', () => {
  89. setData( doc, '<h1>[foo</h1><paragraph>bar]</paragraph>' );
  90. editor.execute( 'delete' );
  91. assertOutput( '<paragraph>[]</paragraph>' );
  92. } );
  93. it( 'should not replace content with paragraph - if not whole content is selected', () => {
  94. setData( doc, '<h1>f[oo</h1><paragraph>bar]</paragraph>' );
  95. editor.execute( 'delete' );
  96. assertOutput( '<h1>f[]</h1>' );
  97. } );
  98. it( 'should not replace content with paragraph - if selection was collapsed', () => {
  99. setData( doc, '<h1></h1><paragraph>[]</paragraph>' );
  100. editor.execute( 'delete' );
  101. assertOutput( '<h1>[]</h1>' );
  102. } );
  103. } );
  104. } );