move.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { move } from '../../../src/view/writer';
  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 EmptyElement from '../../../src/view/emptyelement';
  10. import UIElement from '../../../src/view/uielement';
  11. import Range from '../../../src/view/range';
  12. import Position from '../../../src/view/position';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. describe( 'writer', () => {
  15. /**
  16. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  17. * test ranges.
  18. *
  19. * @param {String} input
  20. * @param {String} expectedResult
  21. * @param {String} expectedRemoved
  22. */
  23. function test( source, destination, sourceAfterMove, destinationAfterMove ) {
  24. const { view: srcView, selection: srcSelection } = parse( source );
  25. const { view: dstView, selection: dstSelection } = parse( destination );
  26. const newRange = move( srcSelection.getFirstRange(), dstSelection.getFirstPosition() );
  27. expect( stringify( dstView, newRange, { showType: true, showPriority: true } ) ).to.equal( destinationAfterMove );
  28. expect( stringify( srcView, null, { showType: true, showPriority: true } ) ).to.equal( sourceAfterMove );
  29. }
  30. describe( 'move', () => {
  31. it( 'should move single text node', () => {
  32. test(
  33. '<container:p>[foobar]</container:p>',
  34. '<container:p>[]</container:p>',
  35. '<container:p></container:p>',
  36. '<container:p>[foobar]</container:p>'
  37. );
  38. } );
  39. it( 'should not leave empty text nodes', () => {
  40. test(
  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 move part of the text node', () => {
  48. test(
  49. '<container:p>f{oob}ar</container:p>',
  50. '<container:p>[]</container:p>',
  51. '<container:p>far</container:p>',
  52. '<container:p>[oob]</container:p>'
  53. );
  54. } );
  55. it( 'should support unicode', () => {
  56. test(
  57. '<container:p>நி{லை}க்கு</container:p>',
  58. '<container:p>நி{}கு</container:p>',
  59. '<container:p>நிக்கு</container:p>',
  60. '<container:p>நி{லை}கு</container:p>'
  61. );
  62. } );
  63. it( 'should move parts of nodes', () => {
  64. test(
  65. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  66. '<container:p>[]<attribute:b view-priority="10">qux</attribute:b></container:p>',
  67. '<container:p>f<attribute:b view-priority="10">r</attribute:b></container:p>',
  68. '<container:p>[oo<attribute:b view-priority="10">ba}qux</attribute:b></container:p>'
  69. );
  70. } );
  71. it( 'should merge after moving #1', () => {
  72. test(
  73. '<container:p>' +
  74. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  75. '</container:p>',
  76. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  77. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  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. );
  82. } );
  83. it( 'should merge after moving #2', () => {
  84. test(
  85. '<container:p>' +
  86. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  87. '</container:p>',
  88. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  89. '<container:p><attribute:b view-priority="1">fozqux</attribute:b></container:p>',
  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. );
  94. } );
  95. it( 'should move part of the text node in document fragment', () => {
  96. test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  97. } );
  98. it( 'should correctly move text nodes inside same parent', () => {
  99. const { view, selection } = parse( '<container:p>[<attribute:b>a</attribute:b>]b<attribute:b>c</attribute:b></container:p>' );
  100. const newRange = move( selection.getFirstRange(), Position.createAt( view, 2 ) );
  101. const expectedView = '<container:p>b[<attribute:b>a}c</attribute:b></container:p>';
  102. expect( stringify( view, newRange, { showType: true } ) ).to.equal( expectedView );
  103. } );
  104. it( 'should correctly move text nodes inside same container', () => {
  105. const { view, selection } = parse(
  106. '<container:p><attribute:b>a{b</attribute:b>xx<attribute:b>c}d</attribute:b>yy</container:p>'
  107. );
  108. const viewText = view.getChild( 3 );
  109. const newRange = move( selection.getFirstRange(), Position.createAt( viewText, 1 ) );
  110. expect( stringify( view, newRange, { showType: true } ) ).to.equal(
  111. '<container:p><attribute:b>ad</attribute:b>y[<attribute:b>b</attribute:b>xx<attribute:b>c</attribute:b>]y</container:p>'
  112. );
  113. } );
  114. it( 'should move EmptyElement', () => {
  115. test(
  116. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  117. '<container:div>baz{}quix</container:div>',
  118. '<container:p>foobar</container:p>',
  119. '<container:div>baz[<empty:img></empty:img>]quix</container:div>'
  120. );
  121. } );
  122. it( 'should throw if trying to move to EmptyElement', () => {
  123. const srcAttribute = new AttributeElement( 'b' );
  124. const srcContainer = new ContainerElement( 'p', null, srcAttribute );
  125. const srcRange = Range.createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  126. const dstEmpty = new EmptyElement( 'img' );
  127. new ContainerElement( 'p', null, dstEmpty ); // eslint-disable-line no-new
  128. const dstPosition = new Position( dstEmpty, 0 );
  129. expect( () => {
  130. move( srcRange, dstPosition );
  131. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  132. } );
  133. it( 'should move UIElement', () => {
  134. test(
  135. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  136. '<container:div>baz{}quix</container:div>',
  137. '<container:p>foobar</container:p>',
  138. '<container:div>baz[<ui:span></ui:span>]quix</container:div>'
  139. );
  140. } );
  141. it( 'should throw if trying to move to UIElement', () => {
  142. const srcAttribute = new AttributeElement( 'b' );
  143. const srcContainer = new ContainerElement( 'p', null, srcAttribute );
  144. const srcRange = Range.createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  145. const dstUI = new UIElement( 'span' );
  146. new ContainerElement( 'p', null, dstUI ); // eslint-disable-line no-new
  147. const dstPosition = new Position( dstUI, 0 );
  148. expect( () => {
  149. move( srcRange, dstPosition );
  150. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  151. } );
  152. } );
  153. } );