move.js 8.3 KB

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