8
0

attributedelta.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transformations from '../../../../src/model/delta/basic-transformations'; // eslint-disable-line no-unused-vars
  6. import deltaTransform from '../../../../src/model/delta/transform';
  7. const transform = deltaTransform.transform;
  8. import Element from '../../../../src/model/element';
  9. import Text from '../../../../src/model/text';
  10. import Position from '../../../../src/model/position';
  11. import Range from '../../../../src/model/range';
  12. import AttributeDelta from '../../../../src/model/delta/attributedelta';
  13. import Delta from '../../../../src/model/delta/delta';
  14. import AttributeOperation from '../../../../src/model/operation/attributeoperation';
  15. import NoOperation from '../../../../src/model/operation/nooperation';
  16. import {
  17. expectDelta,
  18. getFilledDocument,
  19. getAttributeDelta,
  20. getWeakInsertDelta,
  21. getSplitDelta
  22. } from '../../../../tests/model/delta/transform/_utils/utils';
  23. describe( 'transform', () => {
  24. let doc, root, baseVersion, context;
  25. beforeEach( () => {
  26. doc = getFilledDocument();
  27. root = doc.getRoot();
  28. baseVersion = doc.version;
  29. context = { isStrong: false };
  30. } );
  31. describe( 'AttributeDelta by', () => {
  32. describe( 'WeakInsertDelta', () => {
  33. it( 'weak insert inside attribute range should "fix" splitting the range', () => {
  34. const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
  35. const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
  36. const insertPosition = new Position( root, [ 3, 3, 0 ] );
  37. const insertDelta = getWeakInsertDelta(
  38. insertPosition,
  39. [
  40. 'a',
  41. new Text( 'b', { key: 'new' } ),
  42. new Text( 'c', { key: 'different' } ),
  43. 'de'
  44. ],
  45. baseVersion
  46. );
  47. const transformed = transform( attrDelta, insertDelta, context );
  48. expect( transformed.length ).to.equal( 2 );
  49. baseVersion = insertDelta.operations.length;
  50. expectDelta( transformed[ 0 ], {
  51. type: AttributeDelta,
  52. operations: [
  53. {
  54. type: AttributeOperation,
  55. range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 3, 8, 9 ] ) ),
  56. key: 'key',
  57. oldValue: 'old',
  58. newValue: 'new',
  59. baseVersion
  60. },
  61. {
  62. type: AttributeOperation,
  63. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 0 ] ) ),
  64. key: 'key',
  65. oldValue: 'old',
  66. newValue: 'new',
  67. baseVersion: baseVersion + 1
  68. }
  69. ]
  70. } );
  71. expectDelta( transformed[ 1 ], {
  72. type: AttributeDelta,
  73. operations: [
  74. {
  75. type: AttributeOperation,
  76. range: new Range( new Position( root, [ 3, 3, 0 ] ), new Position( root, [ 3, 3, 1 ] ) ),
  77. key: 'key',
  78. oldValue: null,
  79. newValue: 'new',
  80. baseVersion: baseVersion + 2
  81. },
  82. {
  83. type: AttributeOperation,
  84. range: new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 3 ] ) ),
  85. key: 'key',
  86. oldValue: 'different',
  87. newValue: 'new',
  88. baseVersion: baseVersion + 3
  89. },
  90. {
  91. type: AttributeOperation,
  92. range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 5 ] ) ),
  93. key: 'key',
  94. oldValue: null,
  95. newValue: 'new',
  96. baseVersion: baseVersion + 4
  97. }
  98. ]
  99. } );
  100. } );
  101. it( 'should be normally transformed if weak insert is not in the attribute range', () => {
  102. const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
  103. const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
  104. const insertPosition = new Position( root, [ 5 ] );
  105. const insertDelta = getWeakInsertDelta( insertPosition, 'abc', baseVersion );
  106. const transformed = transform( attrDelta, insertDelta, context );
  107. expect( transformed.length ).to.equal( 1 );
  108. baseVersion = insertDelta.operations.length;
  109. expectDelta( transformed[ 0 ], {
  110. type: AttributeDelta,
  111. operations: [
  112. {
  113. type: AttributeOperation,
  114. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) ),
  115. key: 'key',
  116. oldValue: 'old',
  117. newValue: 'new',
  118. baseVersion
  119. }
  120. ]
  121. } );
  122. } );
  123. } );
  124. describe( 'SplitDelta', () => {
  125. it( 'change attribute of split element', () => {
  126. const attrRange1 = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  127. const attrRange2 = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  128. const attrDelta = new AttributeDelta();
  129. attrDelta.addOperation( new AttributeOperation( attrRange1, 'key', 'old', 'new', baseVersion ) );
  130. attrDelta.addOperation( new AttributeOperation( attrRange2, 'key', 'old', 'new', baseVersion ) );
  131. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  132. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  133. const transformed = transform( attrDelta, splitDelta, context );
  134. expect( transformed.length ).to.equal( 2 );
  135. baseVersion = splitDelta.operations.length;
  136. expectDelta( transformed[ 0 ], {
  137. type: AttributeDelta,
  138. operations: [
  139. {
  140. type: AttributeOperation,
  141. range: new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  142. key: 'key',
  143. oldValue: 'old',
  144. newValue: 'new',
  145. baseVersion
  146. },
  147. {
  148. type: AttributeOperation,
  149. range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 4 ] ) ),
  150. key: 'key',
  151. oldValue: 'old',
  152. newValue: 'new',
  153. baseVersion: baseVersion + 1
  154. },
  155. {
  156. type: AttributeOperation,
  157. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) ),
  158. key: 'key',
  159. oldValue: 'old',
  160. newValue: 'new',
  161. baseVersion: baseVersion + 2
  162. },
  163. {
  164. type: AttributeOperation,
  165. range: new Range( new Position( root, [ 3, 3, 4, 0 ] ), new Position( root, [ 3, 3, 4, 9 ] ) ),
  166. key: 'key',
  167. oldValue: 'old',
  168. newValue: 'new',
  169. baseVersion: baseVersion + 3
  170. }
  171. ]
  172. } );
  173. expectDelta( transformed[ 1 ], {
  174. type: AttributeDelta,
  175. operations: [
  176. {
  177. type: AttributeOperation,
  178. range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
  179. key: 'key',
  180. oldValue: null,
  181. newValue: 'new',
  182. baseVersion: baseVersion + 4
  183. }
  184. ]
  185. } );
  186. } );
  187. // A different case.
  188. it( 'change attribute of split element #2', () => {
  189. const attrRange = new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) );
  190. const attrDelta = new AttributeDelta();
  191. attrDelta.addOperation( new AttributeOperation( attrRange, 'foo', null, 'bar', baseVersion ) );
  192. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  193. const splitDelta = getSplitDelta( splitPosition, new Element( 'p', { foo: 'old' } ), 9, baseVersion );
  194. const transformed = transform( attrDelta, splitDelta, context );
  195. expect( transformed.length ).to.equal( 2 );
  196. baseVersion = splitDelta.operations.length;
  197. expectDelta( transformed[ 0 ], {
  198. type: AttributeDelta,
  199. operations: [
  200. {
  201. type: AttributeOperation,
  202. range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) ),
  203. key: 'foo',
  204. oldValue: null,
  205. newValue: 'bar',
  206. baseVersion
  207. }
  208. ]
  209. } );
  210. expectDelta( transformed[ 1 ], {
  211. type: AttributeDelta,
  212. operations: [
  213. {
  214. type: AttributeOperation,
  215. range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
  216. key: 'foo',
  217. oldValue: 'old',
  218. newValue: 'bar',
  219. baseVersion: baseVersion + 1
  220. }
  221. ]
  222. } );
  223. } );
  224. } );
  225. describe( 'AttributeDelta', () => {
  226. it( 'should be converted to no delta if all operations are NoOperations', () => {
  227. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  228. const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
  229. const attrDeltaA = new AttributeDelta();
  230. attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
  231. attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
  232. const attrDeltaB = new AttributeDelta();
  233. attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
  234. attrDeltaB.addOperation( new AttributeOperation( rangeB, 'key', null, 'other', 1 ) );
  235. const transformed = transform( attrDeltaA, attrDeltaB, context );
  236. expect( transformed.length ).to.equal( 1 );
  237. expectDelta( transformed[ 0 ], {
  238. type: Delta,
  239. operations: [
  240. {
  241. type: NoOperation,
  242. baseVersion: 2
  243. },
  244. {
  245. type: NoOperation,
  246. baseVersion: 3
  247. }
  248. ]
  249. } );
  250. } );
  251. it( 'should put AttributeOperation as first operation in the delta', () => {
  252. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  253. const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
  254. const attrDeltaA = new AttributeDelta();
  255. attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
  256. attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
  257. const attrDeltaB = new AttributeDelta();
  258. attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
  259. const transformed = transform( attrDeltaA, attrDeltaB, context );
  260. expect( transformed.length ).to.equal( 1 );
  261. expectDelta( transformed[ 0 ], {
  262. type: AttributeDelta,
  263. operations: [
  264. {
  265. type: AttributeOperation,
  266. range: rangeB,
  267. key: 'key',
  268. oldValue: null,
  269. newValue: 'new',
  270. baseVersion: 1
  271. },
  272. {
  273. type: NoOperation,
  274. baseVersion: 2
  275. }
  276. ]
  277. } );
  278. } );
  279. } );
  280. } );
  281. } );