1281.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Position from '../../src/model/position';
  9. import { setData as setModelData, getData as getModelData } from '../../src/dev-utils/model';
  10. describe( 'Bug ckeditor5-engine#1281', () => {
  11. let element, editor, model;
  12. beforeEach( () => {
  13. element = document.createElement( 'div' );
  14. document.body.appendChild( element );
  15. return ClassicTestEditor
  16. .create( element, { plugins: [ Paragraph ] } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. } );
  21. } );
  22. afterEach( () => {
  23. element.remove();
  24. return editor.destroy();
  25. } );
  26. it( 'loads content that contains multi-range selection', () => {
  27. setModelData( model,
  28. '<paragraph>Paragraph 1.</paragraph>' +
  29. '<paragraph>Paragraph 2.</paragraph>' +
  30. '<paragraph>[Paragraph 3.]</paragraph>' +
  31. '<paragraph>[Paragraph 4.]</paragraph>'
  32. );
  33. const root = model.document.getRoot();
  34. const thirdParagraph = root.getNodeByPath( [ 2 ] );
  35. const fourthParagraph = root.getNodeByPath( [ 3 ] );
  36. const selRanges = Array.from( model.document.selection.getRanges() );
  37. expect( selRanges.length ).to.equal( 2 );
  38. assertPositions( Position._createAt( thirdParagraph, 0 ), selRanges[ 0 ].start );
  39. assertPositions( Position._createAt( thirdParagraph, 'end' ), selRanges[ 0 ].end );
  40. assertPositions( Position._createAt( fourthParagraph, 0 ), selRanges[ 1 ].start );
  41. assertPositions( Position._createAt( fourthParagraph, 'end' ), selRanges[ 1 ].end );
  42. } );
  43. it( 'does not throw an error when content before the selection is being removed (last element is selected)', () => {
  44. setModelData( model,
  45. '<paragraph>Paragraph 1.</paragraph>' +
  46. '<paragraph>Paragraph 2.</paragraph>' +
  47. '<paragraph>[Paragraph 3.]</paragraph>' +
  48. '<paragraph>[Paragraph 4.]</paragraph>'
  49. );
  50. model.change( writer => {
  51. const root = model.document.getRoot();
  52. const firstParagraph = root.getNodeByPath( [ 0 ] );
  53. expect( () => {
  54. writer.remove( firstParagraph );
  55. } ).to.not.throw();
  56. assertOutput(
  57. '<paragraph>Paragraph 2.</paragraph>' +
  58. '<paragraph>[Paragraph 3.]</paragraph>' +
  59. '<paragraph>[Paragraph 4.]</paragraph>'
  60. );
  61. } );
  62. } );
  63. it( 'does not throw an error when content before the selection is being removed (last element is not selected)', () => {
  64. setModelData( model,
  65. '<paragraph>Paragraph 1.</paragraph>' +
  66. '<paragraph>Paragraph 2.</paragraph>' +
  67. '<paragraph>[Paragraph 3.]</paragraph>' +
  68. '<paragraph>[Paragraph 4.]</paragraph>' +
  69. '<paragraph>Paragraph 5.</paragraph>'
  70. );
  71. model.change( writer => {
  72. const root = model.document.getRoot();
  73. const firstParagraph = root.getNodeByPath( [ 0 ] );
  74. expect( () => {
  75. writer.remove( firstParagraph );
  76. } ).to.not.throw();
  77. assertOutput(
  78. '<paragraph>Paragraph 2.</paragraph>' +
  79. '<paragraph>[Paragraph 3.]</paragraph>' +
  80. '<paragraph>[Paragraph 4.]</paragraph>' +
  81. '<paragraph>Paragraph 5.</paragraph>'
  82. );
  83. } );
  84. } );
  85. it( 'does not throw an error when content after the selection is being removed (first element is selected)', () => {
  86. setModelData( model,
  87. '<paragraph>[Paragraph 1.]</paragraph>' +
  88. '<paragraph>Paragraph 2.</paragraph>' +
  89. '<paragraph>Paragraph 3.</paragraph>' +
  90. '<paragraph>[Paragraph 4.]</paragraph>' +
  91. '<paragraph>Paragraph 5.</paragraph>'
  92. );
  93. model.change( writer => {
  94. const root = model.document.getRoot();
  95. const lastParagraph = root.getNodeByPath( [ 4 ] );
  96. expect( () => {
  97. writer.remove( lastParagraph );
  98. } ).to.not.throw();
  99. assertOutput(
  100. '<paragraph>[Paragraph 1.]</paragraph>' +
  101. '<paragraph>Paragraph 2.</paragraph>' +
  102. '<paragraph>Paragraph 3.</paragraph>' +
  103. '<paragraph>[Paragraph 4.]</paragraph>'
  104. );
  105. } );
  106. } );
  107. it( 'does not throw an error when content after the selection is being removed (first element is not selected)', () => {
  108. setModelData( model,
  109. '<paragraph>Paragraph 1.</paragraph>' +
  110. '<paragraph>Paragraph 2.</paragraph>' +
  111. '<paragraph>[Paragraph 3.]</paragraph>' +
  112. '<paragraph>[Paragraph 4.]</paragraph>' +
  113. '<paragraph>Paragraph 5.</paragraph>'
  114. );
  115. model.change( writer => {
  116. const root = model.document.getRoot();
  117. const lastParagraph = root.getNodeByPath( [ 4 ] );
  118. expect( () => {
  119. writer.remove( lastParagraph );
  120. } ).to.not.throw();
  121. assertOutput(
  122. '<paragraph>Paragraph 1.</paragraph>' +
  123. '<paragraph>Paragraph 2.</paragraph>' +
  124. '<paragraph>[Paragraph 3.]</paragraph>' +
  125. '<paragraph>[Paragraph 4.]</paragraph>'
  126. );
  127. } );
  128. } );
  129. it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is selected)', () => {
  130. setModelData( model,
  131. '<paragraph>Paragraph 1.</paragraph>' +
  132. '<paragraph>[Paragraph 2.]</paragraph>' +
  133. '<paragraph>Paragraph 3.</paragraph>' +
  134. '<paragraph>[Paragraph 4.]</paragraph>'
  135. );
  136. model.change( writer => {
  137. const root = model.document.getRoot();
  138. const thirdParagraph = root.getNodeByPath( [ 2 ] );
  139. expect( () => {
  140. writer.remove( thirdParagraph );
  141. } ).to.not.throw();
  142. assertOutput(
  143. '<paragraph>Paragraph 1.</paragraph>' +
  144. '<paragraph>[Paragraph 2.]</paragraph>' +
  145. '<paragraph>[Paragraph 4.]</paragraph>'
  146. );
  147. } );
  148. } );
  149. it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is not selected)', () => {
  150. setModelData( model,
  151. '<paragraph>Paragraph 1.</paragraph>' +
  152. '<paragraph>[Paragraph 2.]</paragraph>' +
  153. '<paragraph>Paragraph 3.</paragraph>' +
  154. '<paragraph>[Paragraph 4.]</paragraph>' +
  155. '<paragraph>Paragraph 5.</paragraph>'
  156. );
  157. model.change( writer => {
  158. const root = model.document.getRoot();
  159. const thirdParagraph = root.getNodeByPath( [ 2 ] );
  160. expect( () => {
  161. writer.remove( thirdParagraph );
  162. } ).to.not.throw();
  163. assertOutput(
  164. '<paragraph>Paragraph 1.</paragraph>' +
  165. '<paragraph>[Paragraph 2.]</paragraph>' +
  166. '<paragraph>[Paragraph 4.]</paragraph>' +
  167. '<paragraph>Paragraph 5.</paragraph>'
  168. );
  169. } );
  170. } );
  171. function assertPositions( firstPosition, secondPosition ) {
  172. expect( firstPosition.isEqual( secondPosition ) ).to.be.true;
  173. }
  174. function assertOutput( output ) {
  175. expect( getModelData( model ) ).to.equal( output );
  176. }
  177. } );