move.js 8.4 KB

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