unwrapdelta.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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();
  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, 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. type: MoveOperation,
  75. sourcePosition: new Position( root, [ 3, 3, 15 ] ),
  76. howMany: 1,
  77. targetPosition: new Position( gy, [ 1, 0 ] ),
  78. baseVersion: baseVersion + 3
  79. }
  80. ]
  81. } );
  82. // Test if deltas do what they should after applying transformed delta.
  83. applyDelta( splitDelta, doc );
  84. applyDelta( transformed[ 0 ], doc );
  85. applyDelta( transformed[ 1 ], doc );
  86. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  87. // UnwrapDelta is applied. SplitDelta is discarded.
  88. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  89. } );
  90. it( 'split position before unwrapped node', () => {
  91. let splitPosition = new Position( root, [ 3, 3, 3 ] );
  92. let splitDelta = getSplitDelta( splitPosition, new Element( 'div' ), 1, baseVersion );
  93. let transformed = transform( unwrapDelta, splitDelta );
  94. expect( transformed.length ).to.equal( 1 );
  95. baseVersion = splitDelta.operations.length;
  96. expectDelta( transformed[ 0 ], {
  97. type: UnwrapDelta,
  98. operations: [
  99. {
  100. type: MoveOperation,
  101. sourcePosition: new Position( root, [ 3, 4, 0, 0 ] ),
  102. howMany: 12,
  103. targetPosition: new Position( root, [ 3, 4, 0 ] ),
  104. baseVersion: baseVersion
  105. },
  106. {
  107. type: MoveOperation,
  108. sourcePosition: new Position( root, [ 3, 4, 12 ] ),
  109. howMany: 1,
  110. targetPosition: new Position( gy, [ 0, 0 ] ),
  111. baseVersion: baseVersion + 1
  112. }
  113. ]
  114. } );
  115. // Test if deltas do what they should after applying transformed delta.
  116. applyDelta( splitDelta, doc );
  117. applyDelta( transformed[ 0 ], doc );
  118. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  119. // UnwrapDelta and SplitDelta are applied.
  120. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVabcfoobarxyzDIV' );
  121. } );
  122. } );
  123. } );
  124. } );