move.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { move } from 'ckeditor5/engine/view/writer.js';
  7. import ViewPosition from 'ckeditor5/engine/view/position.js';
  8. import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js';
  9. import ContainerElement from 'ckeditor5/engine/view/containerelement.js';
  10. import AttributeElement from 'ckeditor5/engine/view/attributeelement.js';
  11. import EmptyElement from 'ckeditor5/engine/view/emptyelement.js';
  12. import Range from 'ckeditor5/engine/view/range.js';
  13. import Position from 'ckeditor5/engine/view/position.js';
  14. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  15. describe( 'writer', () => {
  16. /**
  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. */
  24. function test( source, destination, sourceAfterMove, destinationAfterMove ) {
  25. let { view: srcView, selection: srcSelection } = parse( source );
  26. let { view: dstView, selection: dstSelection } = parse( destination );
  27. const newRange = 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. describe( 'move', () => {
  32. it( 'should move single text node', () => {
  33. test(
  34. '<container:p>[foobar]</container:p>',
  35. '<container:p>[]</container:p>',
  36. '<container:p></container:p>',
  37. '<container:p>[foobar]</container:p>'
  38. );
  39. } );
  40. it( 'should not leave empty text nodes', () => {
  41. test(
  42. '<container:p>{foobar}</container:p>',
  43. '<container:p>[]</container:p>',
  44. '<container:p></container:p>',
  45. '<container:p>[foobar]</container:p>'
  46. );
  47. } );
  48. it( 'should move part of the text node', () => {
  49. test(
  50. '<container:p>f{oob}ar</container:p>',
  51. '<container:p>[]</container:p>',
  52. '<container:p>far</container:p>',
  53. '<container:p>[oob]</container:p>'
  54. );
  55. } );
  56. it( 'should support unicode', () => {
  57. test(
  58. '<container:p>நி{லை}க்கு</container:p>',
  59. '<container:p>நி{}கு</container:p>',
  60. '<container:p>நிக்கு</container:p>',
  61. '<container:p>நி{லை}கு</container:p>'
  62. );
  63. } );
  64. it( 'should move parts of nodes', () => {
  65. test(
  66. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  67. '<container:p>[]<attribute:b view-priority="10">qux</attribute:b></container:p>',
  68. '<container:p>f<attribute:b view-priority="10">r</attribute:b></container:p>',
  69. '<container:p>[oo<attribute:b view-priority="10">ba}qux</attribute:b></container:p>'
  70. );
  71. } );
  72. it( 'should merge after moving #1', () => {
  73. test(
  74. '<container:p>' +
  75. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  76. '</container:p>',
  77. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  78. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  79. '<container:p>' +
  80. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  81. '</container:p>'
  82. );
  83. } );
  84. it( 'should merge after moving #2', () => {
  85. test(
  86. '<container:p>' +
  87. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  88. '</container:p>',
  89. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  90. '<container:p><attribute:b view-priority="1">fozqux</attribute:b></container:p>',
  91. '<container:p>' +
  92. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  93. '</container:p>'
  94. );
  95. } );
  96. it( 'should move part of the text node in document fragment', () => {
  97. test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  98. } );
  99. it( 'should correctly move text nodes inside same parent', () => {
  100. let { view, selection } = parse( '<container:p>[<attribute:b>a</attribute:b>]b<attribute:b>c</attribute:b></container:p>' );
  101. const newRange = move( selection.getFirstRange(), ViewPosition.createAt( view, 2 ) );
  102. const expectedView = '<container:p>b[<attribute:b>a}c</attribute:b></container:p>';
  103. expect( stringify( view, newRange, { showType: true } ) ).to.equal( expectedView );
  104. } );
  105. it( 'should move EmptyElement', () => {
  106. test(
  107. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  108. '<container:div>baz{}quix</container:div>',
  109. '<container:p>foobar</container:p>',
  110. '<container:div>baz[<empty:img></empty:img>]quix</container:div>'
  111. );
  112. } );
  113. it( 'should throw if trying to move to EmptyElement', () => {
  114. const srcAttribute = new AttributeElement( 'b' );
  115. const srcContainer = new ContainerElement( 'p', null, srcAttribute );
  116. const srcRange = Range.createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 );
  117. const dstEmpty = new EmptyElement( 'img' );
  118. new ContainerElement( 'p', null, dstEmpty );
  119. const dstPosition = new Position( dstEmpty, 0 );
  120. expect( () => {
  121. move( srcRange, dstPosition );
  122. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  123. } );
  124. } );
  125. } );