attributedelta.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 SplitDelta from '../../../../src/model/delta/splitdelta';
  15. import AttributeOperation from '../../../../src/model/operation/attributeoperation';
  16. import MoveOperation from '../../../../src/model/operation/moveoperation';
  17. import ReinsertOperation from '../../../../src/model/operation/reinsertoperation';
  18. import NoOperation from '../../../../src/model/operation/nooperation';
  19. import {
  20. expectDelta,
  21. getFilledDocument,
  22. getAttributeDelta,
  23. getWeakInsertDelta,
  24. getSplitDelta
  25. } from '../../../../tests/model/delta/transform/_utils/utils';
  26. describe( 'transform', () => {
  27. let doc, root, baseVersion, context;
  28. beforeEach( () => {
  29. doc = getFilledDocument();
  30. root = doc.getRoot();
  31. baseVersion = doc.version;
  32. context = { isStrong: false };
  33. } );
  34. describe( 'AttributeDelta by', () => {
  35. describe( 'WeakInsertDelta', () => {
  36. it( 'weak insert inside attribute range should "fix" splitting the range', () => {
  37. const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
  38. const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
  39. const insertPosition = new Position( root, [ 3, 3, 0 ] );
  40. const insertDelta = getWeakInsertDelta(
  41. insertPosition,
  42. [
  43. 'a',
  44. new Text( 'b', { key: 'new' } ),
  45. new Text( 'c', { key: 'different' } ),
  46. 'de'
  47. ],
  48. baseVersion
  49. );
  50. const transformed = transform( attrDelta, insertDelta, context );
  51. expect( transformed.length ).to.equal( 2 );
  52. baseVersion = insertDelta.operations.length;
  53. expectDelta( transformed[ 0 ], {
  54. type: AttributeDelta,
  55. operations: [
  56. {
  57. type: AttributeOperation,
  58. range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 3, 8, 9 ] ) ),
  59. key: 'key',
  60. oldValue: 'old',
  61. newValue: 'new',
  62. baseVersion
  63. },
  64. {
  65. type: AttributeOperation,
  66. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 0 ] ) ),
  67. key: 'key',
  68. oldValue: 'old',
  69. newValue: 'new',
  70. baseVersion: baseVersion + 1
  71. }
  72. ]
  73. } );
  74. expectDelta( transformed[ 1 ], {
  75. type: AttributeDelta,
  76. operations: [
  77. {
  78. type: AttributeOperation,
  79. range: new Range( new Position( root, [ 3, 3, 0 ] ), new Position( root, [ 3, 3, 1 ] ) ),
  80. key: 'key',
  81. oldValue: null,
  82. newValue: 'new',
  83. baseVersion: baseVersion + 2
  84. },
  85. {
  86. type: AttributeOperation,
  87. range: new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 3 ] ) ),
  88. key: 'key',
  89. oldValue: 'different',
  90. newValue: 'new',
  91. baseVersion: baseVersion + 3
  92. },
  93. {
  94. type: AttributeOperation,
  95. range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 5 ] ) ),
  96. key: 'key',
  97. oldValue: null,
  98. newValue: 'new',
  99. baseVersion: baseVersion + 4
  100. }
  101. ]
  102. } );
  103. } );
  104. it( 'should be normally transformed if weak insert is not in the attribute range', () => {
  105. const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
  106. const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
  107. const insertPosition = new Position( root, [ 5 ] );
  108. const insertDelta = getWeakInsertDelta( insertPosition, 'abc', baseVersion );
  109. const transformed = transform( attrDelta, insertDelta, context );
  110. expect( transformed.length ).to.equal( 1 );
  111. baseVersion = insertDelta.operations.length;
  112. expectDelta( transformed[ 0 ], {
  113. type: AttributeDelta,
  114. operations: [
  115. {
  116. type: AttributeOperation,
  117. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) ),
  118. key: 'key',
  119. oldValue: 'old',
  120. newValue: 'new',
  121. baseVersion
  122. }
  123. ]
  124. } );
  125. } );
  126. } );
  127. describe( 'SplitDelta', () => {
  128. it( 'change attribute of split element', () => {
  129. const attrRange1 = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  130. const attrRange2 = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  131. const attrDelta = new AttributeDelta();
  132. attrDelta.addOperation( new AttributeOperation( attrRange1, 'key', 'old', 'new', baseVersion ) );
  133. attrDelta.addOperation( new AttributeOperation( attrRange2, 'key', 'old', 'new', baseVersion ) );
  134. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  135. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  136. const transformed = transform( attrDelta, splitDelta, context );
  137. expect( transformed.length ).to.equal( 2 );
  138. baseVersion = splitDelta.operations.length;
  139. expectDelta( transformed[ 0 ], {
  140. type: AttributeDelta,
  141. operations: [
  142. {
  143. type: AttributeOperation,
  144. range: new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  145. key: 'key',
  146. oldValue: 'old',
  147. newValue: 'new',
  148. baseVersion
  149. },
  150. {
  151. type: AttributeOperation,
  152. range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 4 ] ) ),
  153. key: 'key',
  154. oldValue: 'old',
  155. newValue: 'new',
  156. baseVersion: baseVersion + 1
  157. },
  158. {
  159. type: AttributeOperation,
  160. range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) ),
  161. key: 'key',
  162. oldValue: 'old',
  163. newValue: 'new',
  164. baseVersion: baseVersion + 2
  165. },
  166. {
  167. type: AttributeOperation,
  168. range: new Range( new Position( root, [ 3, 3, 4, 0 ] ), new Position( root, [ 3, 3, 4, 9 ] ) ),
  169. key: 'key',
  170. oldValue: 'old',
  171. newValue: 'new',
  172. baseVersion: baseVersion + 3
  173. }
  174. ]
  175. } );
  176. expectDelta( transformed[ 1 ], {
  177. type: AttributeDelta,
  178. operations: [
  179. {
  180. type: AttributeOperation,
  181. range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
  182. key: 'key',
  183. oldValue: null,
  184. newValue: 'new',
  185. baseVersion: baseVersion + 4
  186. }
  187. ]
  188. } );
  189. } );
  190. // A different case.
  191. it( 'change attribute of split element #2', () => {
  192. const attrRange = new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) );
  193. const attrDelta = new AttributeDelta();
  194. attrDelta.addOperation( new AttributeOperation( attrRange, 'foo', null, 'bar', baseVersion ) );
  195. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  196. const splitDelta = getSplitDelta( splitPosition, new Element( 'p', { foo: 'old' } ), 9, baseVersion );
  197. const transformed = transform( attrDelta, splitDelta, context );
  198. expect( transformed.length ).to.equal( 2 );
  199. baseVersion = splitDelta.operations.length;
  200. expectDelta( transformed[ 0 ], {
  201. type: AttributeDelta,
  202. operations: [
  203. {
  204. type: AttributeOperation,
  205. range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) ),
  206. key: 'foo',
  207. oldValue: null,
  208. newValue: 'bar',
  209. baseVersion
  210. }
  211. ]
  212. } );
  213. expectDelta( transformed[ 1 ], {
  214. type: AttributeDelta,
  215. operations: [
  216. {
  217. type: AttributeOperation,
  218. range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
  219. key: 'foo',
  220. oldValue: 'old',
  221. newValue: 'bar',
  222. baseVersion: baseVersion + 1
  223. }
  224. ]
  225. } );
  226. } );
  227. it( 'change attribute of split element that reinserts from graveyard', () => {
  228. const gy = doc.graveyard;
  229. const splitDelta = new SplitDelta();
  230. splitDelta.operations[ 0 ] = new ReinsertOperation(
  231. new Position( gy, [ 1 ] ),
  232. 1,
  233. new Position( root, [ 2 ] ),
  234. baseVersion
  235. );
  236. splitDelta.operations[ 1 ] = new MoveOperation(
  237. new Position( root, [ 1, 4 ] ),
  238. 4,
  239. new Position( root, [ 2, 0 ] ),
  240. baseVersion
  241. );
  242. const attributeDelta = new AttributeDelta();
  243. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 1, 0 ] ) );
  244. attributeDelta.addOperation( new AttributeOperation( range, 'key', 'oldValue', 'newValue', baseVersion ) );
  245. // The split delta was undone.
  246. context.bWasUndone = true;
  247. const transformed = transform( attributeDelta, splitDelta, context );
  248. baseVersion = attributeDelta.operations.length;
  249. // We expect only one delta. Split delta should not affect attribute delta transformation. It was undone.
  250. expect( transformed.length ).to.equal( 1 );
  251. expectDelta( transformed[ 0 ], {
  252. type: AttributeDelta,
  253. operations: [
  254. {
  255. type: AttributeOperation,
  256. range,
  257. key: 'key',
  258. oldValue: 'oldValue',
  259. newValue: 'newValue',
  260. baseVersion
  261. }
  262. ]
  263. } );
  264. } );
  265. it( 'should use default algorithm and not throw if split delta has NoOperation', () => {
  266. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 3 ] ) );
  267. const attrDelta = getAttributeDelta( range, 'foo', null, 'bar', 0 );
  268. const splitDelta = getSplitDelta( new Position( root, [ 0, 2 ] ), new Element( 'paragraph' ), 3, 0 );
  269. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  270. const transformed = transform( attrDelta, splitDelta, context );
  271. baseVersion = splitDelta.operations.length;
  272. expect( transformed.length ).to.equal( 1 );
  273. expectDelta( transformed[ 0 ], {
  274. type: AttributeDelta,
  275. operations: [
  276. {
  277. type: AttributeOperation,
  278. range: new Range( new Position( root, [ 2 ] ), new Position( root, [ 3, 3 ] ) ),
  279. key: 'foo',
  280. oldValue: null,
  281. newValue: 'bar',
  282. baseVersion
  283. }
  284. ]
  285. } );
  286. } );
  287. } );
  288. describe( 'AttributeDelta', () => {
  289. it( 'should be converted to no delta if all operations are NoOperations', () => {
  290. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  291. const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
  292. const attrDeltaA = new AttributeDelta();
  293. attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
  294. attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
  295. const attrDeltaB = new AttributeDelta();
  296. attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
  297. attrDeltaB.addOperation( new AttributeOperation( rangeB, 'key', null, 'other', 1 ) );
  298. const transformed = transform( attrDeltaA, attrDeltaB, context );
  299. expect( transformed.length ).to.equal( 1 );
  300. expectDelta( transformed[ 0 ], {
  301. type: Delta,
  302. operations: [
  303. {
  304. type: NoOperation,
  305. baseVersion: 2
  306. },
  307. {
  308. type: NoOperation,
  309. baseVersion: 3
  310. }
  311. ]
  312. } );
  313. } );
  314. it( 'should put AttributeOperation as first operation in the delta', () => {
  315. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  316. const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
  317. const attrDeltaA = new AttributeDelta();
  318. attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
  319. attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
  320. const attrDeltaB = new AttributeDelta();
  321. attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
  322. const transformed = transform( attrDeltaA, attrDeltaB, context );
  323. expect( transformed.length ).to.equal( 1 );
  324. expectDelta( transformed[ 0 ], {
  325. type: AttributeDelta,
  326. operations: [
  327. {
  328. type: AttributeOperation,
  329. range: rangeB,
  330. key: 'key',
  331. oldValue: null,
  332. newValue: 'new',
  333. baseVersion: 1
  334. },
  335. {
  336. type: NoOperation,
  337. baseVersion: 2
  338. }
  339. ]
  340. } );
  341. } );
  342. } );
  343. } );
  344. } );