move.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. import DowncastWriter from '../../../src/view/downcastwriter';
  6. import { stringify, parse } from '../../../src/dev-utils/view';
  7. import ContainerElement from '../../../src/view/containerelement';
  8. import AttributeElement from '../../../src/view/attributeelement';
  9. import RootEditableElement from '../../../src/view/rooteditableelement';
  10. import EmptyElement from '../../../src/view/emptyelement';
  11. import UIElement from '../../../src/view/uielement';
  12. import RawElement from '../../../src/view/rawelement';
  13. import Range from '../../../src/view/range';
  14. import Position from '../../../src/view/position';
  15. import Document from '../../../src/view/document';
  16. import Mapper from '../../../src/conversion/mapper';
  17. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  18. import { StylesProcessor } from '../../../src/view/stylesmap';
  19. describe( 'DowncastWriter', () => {
  20. describe( 'move()', () => {
  21. let writer, document;
  22. // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  23. // test ranges.
  24. //
  25. // @param {String} input
  26. // @param {String} expectedResult
  27. // @param {String} expectedRemoved
  28. function testMove( source, destination, sourceAfterMove, destinationAfterMove ) {
  29. const { view: srcView, selection: srcSelection } = parse( source );
  30. const { view: dstView, selection: dstSelection } = parse( destination );
  31. const newRange = writer.move( srcSelection.getFirstRange(), dstSelection.getFirstPosition() );
  32. expect( stringify( dstView, newRange, { showType: true, showPriority: true } ) ).to.equal( destinationAfterMove );
  33. expect( stringify( srcView, null, { showType: true, showPriority: true } ) ).to.equal( sourceAfterMove );
  34. }
  35. before( () => {
  36. document = new Document( new StylesProcessor() );
  37. writer = new DowncastWriter( document );
  38. } );
  39. it( 'should move single text node', () => {
  40. testMove(
  41. '<container:p>[foobar]</container:p>',
  42. '<container:p>[]</container:p>',
  43. '<container:p></container:p>',
  44. '<container:p>[foobar]</container:p>'
  45. );
  46. } );
  47. it( 'should not leave empty text nodes', () => {
  48. testMove(
  49. '<container:p>{foobar}</container:p>',
  50. '<container:p>[]</container:p>',
  51. '<container:p></container:p>',
  52. '<container:p>[foobar]</container:p>'
  53. );
  54. } );
  55. it( 'should move part of the text node', () => {
  56. testMove(
  57. '<container:p>f{oob}ar</container:p>',
  58. '<container:p>[]</container:p>',
  59. '<container:p>far</container:p>',
  60. '<container:p>[oob]</container:p>'
  61. );
  62. } );
  63. it( 'should support unicode', () => {
  64. testMove(
  65. '<container:p>நி{லை}க்கு</container:p>',
  66. '<container:p>நி{}கு</container:p>',
  67. '<container:p>நிக்கு</container:p>',
  68. '<container:p>நி{லை}கு</container:p>'
  69. );
  70. } );
  71. it( 'should move parts of nodes', () => {
  72. testMove(
  73. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  74. '<container:p>[]<attribute:b view-priority="10">qux</attribute:b></container:p>',
  75. '<container:p>f<attribute:b view-priority="10">r</attribute:b></container:p>',
  76. '<container:p>[oo<attribute:b view-priority="10">ba}qux</attribute:b></container:p>'
  77. );
  78. } );
  79. it( 'should merge after moving #1', () => {
  80. testMove(
  81. '<container:p>' +
  82. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  83. '</container:p>',
  84. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  85. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  86. '<container:p>' +
  87. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  88. '</container:p>'
  89. );
  90. } );
  91. it( 'should merge after moving #2', () => {
  92. testMove(
  93. '<container:p>' +
  94. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  95. '</container:p>',
  96. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  97. '<container:p><attribute:b view-priority="1">fozqux</attribute:b></container:p>',
  98. '<container:p>' +
  99. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  100. '</container:p>'
  101. );
  102. } );
  103. it( 'should move part of the text node in document fragment', () => {
  104. testMove( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  105. } );
  106. it( 'should correctly move text nodes inside same parent', () => {
  107. const { view, selection } = parse( '<container:p>[<attribute:b>a</attribute:b>]b<attribute:b>c</attribute:b></container:p>' );
  108. const newRange = writer.move( selection.getFirstRange(), Position._createAt( view, 2 ) );
  109. const expectedView = '<container:p>b[<attribute:b>a}c</attribute:b></container:p>';
  110. expect( stringify( view, newRange, { showType: true } ) ).to.equal( expectedView );
  111. } );
  112. it( 'should correctly move text nodes inside same container', () => {
  113. const { view, selection } = parse(
  114. '<container:p><attribute:b>a{b</attribute:b>xx<attribute:b>c}d</attribute:b>yy</container:p>'
  115. );
  116. const viewText = view.getChild( 3 );
  117. const newRange = writer.move( selection.getFirstRange(), Position._createAt( viewText, 1 ) );
  118. expect( stringify( view, newRange, { showType: true } ) ).to.equal(
  119. '<container:p><attribute:b>ad</attribute:b>y[<attribute:b>b</attribute:b>xx<attribute:b>c</attribute:b>]y</container:p>'
  120. );
  121. } );
  122. it( 'should move EmptyElement', () => {
  123. testMove(
  124. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  125. '<container:div>baz{}quix</container:div>',
  126. '<container:p>foobar</container:p>',
  127. '<container:div>baz[<empty:img></empty:img>]quix</container:div>'
  128. );
  129. } );
  130. it( 'should throw if trying to move to EmptyElement', () => {
  131. const srcAttribute = new AttributeElement( document, 'b' );
  132. const srcContainer = new ContainerElement( document, 'p', null, srcAttribute );
  133. const srcRange = Range._createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  134. const dstEmpty = new EmptyElement( document, 'img' );
  135. new ContainerElement( document, 'p', null, dstEmpty ); // eslint-disable-line no-new
  136. const dstPosition = new Position( dstEmpty, 0 );
  137. expectToThrowCKEditorError( () => {
  138. writer.move( srcRange, dstPosition );
  139. }, 'view-writer-cannot-break-empty-element', writer );
  140. } );
  141. it( 'should move UIElement', () => {
  142. testMove(
  143. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  144. '<container:div>baz{}quix</container:div>',
  145. '<container:p>foobar</container:p>',
  146. '<container:div>baz[<ui:span></ui:span>]quix</container:div>'
  147. );
  148. } );
  149. it( 'should throw if trying to move to UIElement', () => {
  150. const srcAttribute = new AttributeElement( document, 'b' );
  151. const srcContainer = new ContainerElement( document, 'p', null, srcAttribute );
  152. const srcRange = Range._createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  153. const dstUI = new UIElement( document, 'span' );
  154. new ContainerElement( document, 'p', null, dstUI ); // eslint-disable-line no-new
  155. const dstPosition = new Position( dstUI, 0 );
  156. expectToThrowCKEditorError( () => {
  157. writer.move( srcRange, dstPosition );
  158. }, 'view-writer-cannot-break-ui-element', writer );
  159. } );
  160. it( 'should move a RawElement', () => {
  161. testMove(
  162. '<container:p>foo[<raw:span></raw:span>]bar</container:p>',
  163. '<container:div>baz{}quix</container:div>',
  164. '<container:p>foobar</container:p>',
  165. '<container:div>baz[<raw:span></raw:span>]quix</container:div>'
  166. );
  167. } );
  168. it( 'should throw if trying to move to a RawElement', () => {
  169. const srcAttribute = new AttributeElement( document, 'b' );
  170. const srcContainer = new ContainerElement( document, 'p', null, srcAttribute );
  171. const srcRange = Range._createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  172. const dstRawElement = new RawElement( document, 'span' );
  173. new ContainerElement( document, 'p', null, dstRawElement ); // eslint-disable-line no-new
  174. const dstPosition = new Position( dstRawElement, 0 );
  175. expectToThrowCKEditorError( () => {
  176. writer.move( srcRange, dstPosition );
  177. }, 'view-writer-cannot-break-raw-element', writer );
  178. } );
  179. it( 'should not break marker mappings if marker element was split and the original element was removed', () => {
  180. const mapper = new Mapper();
  181. const srcContainer = new ContainerElement( document, 'p' );
  182. const dstContainer = new ContainerElement( document, 'p' );
  183. const root = new RootEditableElement( document, 'div' );
  184. root._appendChild( [ srcContainer, dstContainer ] );
  185. const attrElemA = new AttributeElement( document, 'span' );
  186. attrElemA._id = 'foo';
  187. const attrElemB = new AttributeElement( document, 'span' );
  188. attrElemB._id = 'foo';
  189. writer.insert( new Position( srcContainer, 0 ), [ attrElemA, attrElemB ] );
  190. mapper.bindElementToMarker( attrElemA, 'foo' );
  191. expect( mapper.markerNameToElements( 'foo' ).size ).to.equal( 2 );
  192. writer.remove( writer.createRangeOn( attrElemA ) );
  193. expect( mapper.markerNameToElements( 'foo' ).size ).to.equal( 1 );
  194. writer.move( writer.createRangeOn( attrElemB ), new Position( dstContainer, 0 ) );
  195. expect( mapper.markerNameToElements( 'foo' ).size ).to.equal( 1 );
  196. } );
  197. } );
  198. } );