8
0

move.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. describe( 'writer', () => {
  10. /**
  11. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  12. * test ranges.
  13. *
  14. * @param {String} input
  15. * @param {String} expectedResult
  16. * @param {String} expectedRemoved
  17. */
  18. function test( source, destination, sourceAfterMove, destinationAfterMove ) {
  19. let { view: srcView, selection: srcSelection } = parse( source );
  20. let { view: dstView, selection: dstSelection } = parse( destination );
  21. const newRange = move( srcSelection.getFirstRange(), dstSelection.getFirstPosition() );
  22. expect( stringify( dstView, newRange, { showType: true, showPriority: true } ) ).to.equal( destinationAfterMove );
  23. expect( stringify( srcView, null, { showType: true, showPriority: true } ) ).to.equal( sourceAfterMove );
  24. }
  25. describe( 'move', () => {
  26. it( 'should move single text node', () => {
  27. test(
  28. '<container:p>[foobar]</container:p>',
  29. '<container:p>[]</container:p>',
  30. '<container:p></container:p>',
  31. '<container:p>[foobar]</container:p>'
  32. );
  33. } );
  34. it( 'should not leave empty text nodes', () => {
  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 move part of the text node', () => {
  43. test(
  44. '<container:p>f{oob}ar</container:p>',
  45. '<container:p>[]</container:p>',
  46. '<container:p>far</container:p>',
  47. '<container:p>[oob]</container:p>'
  48. );
  49. } );
  50. it( 'should support unicode', () => {
  51. test(
  52. '<container:p>நி{லை}க்கு</container:p>',
  53. '<container:p>நி{}கு</container:p>',
  54. '<container:p>நிக்கு</container:p>',
  55. '<container:p>நி{லை}கு</container:p>'
  56. );
  57. } );
  58. it( 'should move parts of nodes', () => {
  59. test(
  60. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  61. '<container:p>[]<attribute:b view-priority="10">qux</attribute:b></container:p>',
  62. '<container:p>f<attribute:b view-priority="10">r</attribute:b></container:p>',
  63. '<container:p>[oo<attribute:b view-priority="10">ba}qux</attribute:b></container:p>'
  64. );
  65. } );
  66. it( 'should merge after moving #1', () => {
  67. test(
  68. '<container:p>' +
  69. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  70. '</container:p>',
  71. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  72. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  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. );
  77. } );
  78. it( 'should merge after moving #2', () => {
  79. test(
  80. '<container:p>' +
  81. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  82. '</container:p>',
  83. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  84. '<container:p><attribute:b view-priority="1">fozqux</attribute:b></container:p>',
  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. );
  89. } );
  90. it( 'should move part of the text node in document fragment', () => {
  91. test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  92. } );
  93. it( 'should correctly move text nodes inside same parent', () => {
  94. let { view, selection } = parse( '<container:p>[<attribute:b>a</attribute:b>]b<attribute:b>c</attribute:b></container:p>' );
  95. const newRange = move( selection.getFirstRange(), ViewPosition.createAt( view, 2 ) );
  96. const expectedView = '<container:p>b[<attribute:b>a}c</attribute:b></container:p>';
  97. expect( stringify( view, newRange, { showType: true } ) ).to.equal( expectedView );
  98. } );
  99. } );
  100. } );