move.js 3.6 KB

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