8
0

move.js 8.5 KB

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