move.js 6.9 KB

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