move.js 8.1 KB

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