unwrapdelta.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. import transformations from '/ckeditor5/engine/model/delta/basic-transformations.js';
  7. /*jshint unused: false*/
  8. import transform from '/ckeditor5/engine/model/delta/transform.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import Range from '/ckeditor5/engine/model/range.js';
  12. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  13. import MergeDelta from '/ckeditor5/engine/model/delta/mergedelta.js';
  14. import UnwrapDelta from '/ckeditor5/engine/model/delta/unwrapdelta.js';
  15. import { getNodesAndText, jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  16. import {
  17. applyDelta,
  18. expectDelta,
  19. getFilledDocument,
  20. getSplitDelta,
  21. getUnwrapDelta
  22. } from '/tests/engine/model/delta/transform/_utils/utils.js';
  23. describe( 'transform', () => {
  24. let doc, root, gy, baseVersion;
  25. beforeEach( () => {
  26. doc = getFilledDocument();
  27. root = doc.getRoot();
  28. gy = doc.graveyard;
  29. baseVersion = doc.version;
  30. } );
  31. describe( 'UnwrapDelta by', () => {
  32. let unwrapDelta;
  33. beforeEach( () => {
  34. unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  35. } );
  36. describe( 'SplitDelta', () => {
  37. it( 'split position directly in unwrapped node', () => {
  38. let splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  39. let splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  40. let transformed = transform( unwrapDelta, splitDelta );
  41. expect( transformed.length ).to.equal( 2 );
  42. baseVersion = splitDelta.operations.length;
  43. expectDelta( transformed[ 0 ], {
  44. type: MergeDelta,
  45. operations: [
  46. {
  47. type: MoveOperation,
  48. sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
  49. howMany: 9,
  50. targetPosition: new Position( root, [ 3, 3, 3, 3 ] ),
  51. baseVersion: baseVersion
  52. },
  53. {
  54. type: MoveOperation,
  55. sourcePosition: new Position( root, [ 3, 3, 4 ] ),
  56. howMany: 1,
  57. targetPosition: new Position( gy, [ 0, 0 ] ),
  58. baseVersion: baseVersion + 1
  59. }
  60. ]
  61. } );
  62. expectDelta( transformed[ 1 ], {
  63. type: UnwrapDelta,
  64. operations: [
  65. {
  66. type: MoveOperation,
  67. sourcePosition: new Position( root, [ 3, 3, 3, 0 ] ),
  68. howMany: 12,
  69. targetPosition: new Position( root, [ 3, 3, 3 ] ),
  70. baseVersion: baseVersion + 2
  71. },
  72. {
  73. type: MoveOperation,
  74. sourcePosition: new Position( root, [ 3, 3, 15 ] ),
  75. howMany: 1,
  76. targetPosition: new Position( gy, [ 1, 0 ] ),
  77. baseVersion: baseVersion + 3
  78. }
  79. ]
  80. } );
  81. // Test if deltas do what they should after applying transformed delta.
  82. applyDelta( splitDelta, doc );
  83. applyDelta( transformed[ 0 ], doc );
  84. applyDelta( transformed[ 1 ], doc );
  85. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  86. // UnwrapDelta is applied. SplitDelta is discarded.
  87. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  88. } );
  89. it( 'split position before unwrapped node', () => {
  90. let splitPosition = new Position( root, [ 3, 3, 3 ] );
  91. let splitDelta = getSplitDelta( splitPosition, new Element( 'div' ), 1, baseVersion );
  92. let transformed = transform( unwrapDelta, splitDelta );
  93. expect( transformed.length ).to.equal( 1 );
  94. baseVersion = splitDelta.operations.length;
  95. expectDelta( transformed[ 0 ], {
  96. type: UnwrapDelta,
  97. operations: [
  98. {
  99. type: MoveOperation,
  100. sourcePosition: new Position( root, [ 3, 4, 0, 0 ] ),
  101. howMany: 12,
  102. targetPosition: new Position( root, [ 3, 4, 0 ] ),
  103. baseVersion: baseVersion
  104. },
  105. {
  106. type: MoveOperation,
  107. sourcePosition: new Position( root, [ 3, 4, 12 ] ),
  108. howMany: 1,
  109. targetPosition: new Position( gy, [ 0, 0 ] ),
  110. baseVersion: baseVersion + 1
  111. }
  112. ]
  113. } );
  114. // Test if deltas do what they should after applying transformed delta.
  115. applyDelta( splitDelta, doc );
  116. applyDelta( transformed[ 0 ], doc );
  117. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  118. // UnwrapDelta and SplitDelta are applied.
  119. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVabcfoobarxyzDIV' );
  120. } );
  121. } );
  122. } );
  123. } );