8
0

move.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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:10>ba}r</attribute:b:10></container:p>',
  60. '<container:p>[]<attribute:b:10>qux</attribute:b:10></container:p>',
  61. '<container:p>f<attribute:b:10>r</attribute:b:10></container:p>',
  62. '<container:p>[oo<attribute:b:10>ba}qux</attribute:b:10></container:p>'
  63. );
  64. } );
  65. it( 'should merge after moving #1', () => {
  66. test(
  67. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  68. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  69. '<container:p><attribute:b:1>foobazqux</attribute:b:1></container:p>',
  70. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>'
  71. );
  72. } );
  73. it( 'should merge after moving #2', () => {
  74. test(
  75. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  76. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  77. '<container:p><attribute:b:1>fozqux</attribute:b:1></container:p>',
  78. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>'
  79. );
  80. } );
  81. it( 'should move part of the text node in document fragment', () => {
  82. test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  83. } );
  84. } );
  85. } );