move.js 3.7 KB

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