move.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 'use strict';
  7. import { move } from '/ckeditor5/engine/view/writer.js';
  8. import { stringify, parse } from '/tests/engine/_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 move parts of nodes', () => {
  51. test(
  52. '<container:p>f{oo<attribute:b:10>ba}r</attribute:b:10></container:p>',
  53. '<container:p>[]<attribute:b:10>qux</attribute:b:10></container:p>',
  54. '<container:p>f<attribute:b:10>r</attribute:b:10></container:p>',
  55. '<container:p>[oo<attribute:b:10>ba}qux</attribute:b:10></container:p>'
  56. );
  57. } );
  58. it( 'should merge after moving #1', () => {
  59. test(
  60. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  61. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  62. '<container:p><attribute:b:1>foobazqux</attribute:b:1></container:p>',
  63. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>'
  64. );
  65. } );
  66. it( 'should merge after moving #2', () => {
  67. test(
  68. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  69. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  70. '<container:p><attribute:b:1>fozqux</attribute:b:1></container:p>',
  71. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>'
  72. );
  73. } );
  74. it( 'should move part of the text node in document fragment', () => {
  75. test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' );
  76. } );
  77. } );
  78. } );