unwrapdelta.js 4.6 KB

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