8
0

move.js 6.9 KB

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