deletecommand-integration.js 4.4 KB

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