unwrapdelta.js 4.5 KB

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