enableenginedebug.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as enableEngineDebug, disableEngineDebug } from '../../src/dev-utils/enableenginedebug';
  6. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ModelPosition from '../../src/model/position';
  10. import ModelRange from '../../src/model/range';
  11. import ModelText from '../../src/model/text';
  12. import ModelTextProxy from '../../src/model/textproxy';
  13. import ModelElement from '../../src/model/element';
  14. import AttributeOperation from '../../src/model/operation/attributeoperation';
  15. import DetachOperation from '../../src/model/operation/detachoperation';
  16. import InsertOperation from '../../src/model/operation/insertoperation';
  17. import MarkerOperation from '../../src/model/operation/markeroperation';
  18. import MoveOperation from '../../src/model/operation/moveoperation';
  19. import NoOperation from '../../src/model/operation/nooperation';
  20. import RenameOperation from '../../src/model/operation/renameoperation';
  21. import RootAttributeOperation from '../../src/model/operation/rootattributeoperation';
  22. import RemoveOperation from '../../src/model/operation/removeoperation';
  23. import DeltaFactory from '../../src/model/delta/deltafactory';
  24. import Delta from '../../src/model/delta/delta';
  25. import AttributeDelta from '../../src/model/delta/attributedelta';
  26. import InsertDelta from '../../src/model/delta/insertdelta';
  27. import MarkerDelta from '../../src/model/delta/markerdelta';
  28. import MergeDelta from '../../src/model/delta/mergedelta';
  29. import MoveDelta from '../../src/model/delta/movedelta';
  30. import RenameDelta from '../../src/model/delta/renamedelta';
  31. import RootAttributeDelta from '../../src/model/delta/rootattributedelta';
  32. import SplitDelta from '../../src/model/delta/splitdelta';
  33. import UnwrapDelta from '../../src/model/delta/unwrapdelta';
  34. import WrapDelta from '../../src/model/delta/wrapdelta';
  35. import deltaTransform from '../../src/model/delta/transform';
  36. import Model from '../../src/model/model';
  37. import ModelDocumentFragment from '../../src/model/documentfragment';
  38. import ViewDocument from '../../src/view/document';
  39. import ViewAttributeElement from '../../src/view/attributeelement';
  40. import ViewContainerElement from '../../src/view/containerelement';
  41. import ViewText from '../../src/view/text';
  42. import ViewTextProxy from '../../src/view/textproxy';
  43. import ViewDocumentFragment from '../../src/view/documentfragment';
  44. import ViewElement from '../../src/view/element';
  45. import createViewRoot from '../view/_utils/createroot';
  46. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  47. testUtils.createSinonSandbox();
  48. describe( 'enableEngineDebug', () => {
  49. afterEach( () => {
  50. disableEngineDebug();
  51. } );
  52. it( 'should return plugin class', () => {
  53. const result = enableEngineDebug();
  54. expect( result.prototype ).to.be.instanceof( Plugin );
  55. } );
  56. it( 'should not throw when called multiple times', () => {
  57. enableEngineDebug();
  58. enableEngineDebug();
  59. const result = enableEngineDebug();
  60. expect( result.prototype ).to.be.instanceof( Plugin );
  61. } );
  62. } );
  63. describe( 'disableEngineDebug', () => {
  64. it( 'restores modified stubs', () => {
  65. expect( ModelPosition.prototype.log ).to.equal( undefined, 'Initial value (model/position)' );
  66. expect( ModelElement.prototype.printTree ).to.equal( undefined, 'Initial value (model/element)' );
  67. expect( Delta.prototype.log ).to.equal( undefined, 'Initial value (model/delta/delta)' );
  68. expect( ViewElement.prototype.printTree ).to.equal( undefined, 'Initial value (view/element)' );
  69. expect( Model.prototype.createReplayer ).to.equal( undefined, 'Initial value (model/document)' );
  70. expect( Editor.prototype.logDocuments ).to.equal( undefined, 'Initial value (core~editor/editor)' );
  71. enableEngineDebug();
  72. expect( ModelPosition.prototype.log ).to.be.a( 'function', 'After enabling engine debug (model/position)' );
  73. expect( ModelElement.prototype.printTree ).to.be.a( 'function', 'After enabling engine debug (model/element)' );
  74. expect( Delta.prototype.log ).to.be.a( 'function', 'After enabling engine debug (model/delta/delta)' );
  75. expect( ViewElement.prototype.printTree ).to.be.a( 'function', 'After enabling engine debug (view/element)' );
  76. expect( Model.prototype.createReplayer ).to.be.a( 'function', 'After enabling engine debug (model/document)' );
  77. expect( Editor.prototype.logDocuments ).to.be.a( 'function', 'After enabling engine debug (core~editor/editor)' );
  78. disableEngineDebug();
  79. expect( ModelPosition.prototype.log ).to.equal( undefined, 'After disabling engine debug (model/position)' );
  80. expect( ModelElement.prototype.printTree ).to.equal( undefined, 'After disabling engine debug (model/element)' );
  81. expect( Delta.prototype.log ).to.equal( undefined, 'After disabling engine debug (model/delta/delta)' );
  82. expect( ViewElement.prototype.printTree ).to.equal( undefined, 'After disabling engine debug (view/element)' );
  83. expect( Model.prototype.createReplayer ).to.equal( undefined, 'After disabling engine debug (model/document)' );
  84. expect( Editor.prototype.logDocuments ).to.equal( undefined, 'After disabling engine debug (core~editor/editor)' );
  85. } );
  86. } );
  87. describe( 'debug tools', () => {
  88. let DebugPlugin, log, error;
  89. before( () => {
  90. log = sinon.spy();
  91. error = sinon.spy();
  92. DebugPlugin = enableEngineDebug( { log, error } );
  93. } );
  94. after( () => {
  95. disableEngineDebug();
  96. } );
  97. afterEach( () => {
  98. log.reset();
  99. } );
  100. describe( 'should provide logging tools', () => {
  101. let model, modelDoc, modelRoot, modelElement, modelDocFrag;
  102. beforeEach( () => {
  103. model = new Model();
  104. modelDoc = model.document;
  105. modelRoot = modelDoc.createRoot();
  106. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  107. modelDocFrag = new ModelDocumentFragment( [ new ModelText( 'bar' ) ] );
  108. } );
  109. it( 'for ModelText', () => {
  110. const foo = new ModelText( 'foo', { foo: 'bar' } );
  111. expect( foo.toString() ).to.equal( '#foo' );
  112. foo.log();
  113. expect( log.calledWithExactly( 'ModelText: #foo' ) ).to.be.true;
  114. foo.logExtended();
  115. expect( log.calledWithExactly( 'ModelText: #foo, attrs: {"foo":"bar"}' ) ).to.be.true;
  116. } );
  117. it( 'for ModelTextProxy', () => {
  118. const foo = new ModelText( 'foo', { foo: 'bar' } );
  119. const proxy = new ModelTextProxy( foo, 1, 1 );
  120. expect( proxy.toString() ).to.equal( '#o' );
  121. proxy.log();
  122. expect( log.calledWithExactly( 'ModelTextProxy: #o' ) ).to.be.true;
  123. proxy.logExtended();
  124. expect( log.calledWithExactly( 'ModelTextProxy: #o, attrs: {"foo":"bar"}' ) ).to.be.true;
  125. } );
  126. it( 'for ModelElement', () => {
  127. const paragraph = new ModelElement( 'paragraph', { foo: 'bar' }, new ModelText( 'foo' ) );
  128. expect( paragraph.toString() ).to.equal( '<paragraph>' );
  129. paragraph.log();
  130. expectLog( 'ModelElement: <paragraph>' );
  131. paragraph.logExtended();
  132. expectLog( 'ModelElement: <paragraph>, 1 children, attrs: {"foo":"bar"}' );
  133. sinon.spy( paragraph, 'logExtended' );
  134. paragraph.logAll();
  135. expect( paragraph.logExtended.called ).to.be.true;
  136. expectLog( 'ModelText: #foo' );
  137. } );
  138. it( 'for ModelRootElement', () => {
  139. modelRoot.log();
  140. expectLog( 'ModelRootElement: main' );
  141. } );
  142. it( 'for ModelDocumentFragment', () => {
  143. modelDocFrag.log();
  144. expectLog( 'ModelDocumentFragment: documentFragment' );
  145. } );
  146. it( 'for ModelPosition', () => {
  147. const posInRoot = new ModelPosition( modelRoot, [ 0, 1, 0 ] );
  148. const posInElement = new ModelPosition( modelElement, [ 0 ] );
  149. const posInDocFrag = new ModelPosition( modelDocFrag, [ 2, 3 ] );
  150. expect( posInRoot.toString() ).to.equal( 'main [ 0, 1, 0 ]' );
  151. expect( posInElement.toString() ).to.equal( '<paragraph> [ 0 ]' );
  152. expect( posInDocFrag.toString() ).to.equal( 'documentFragment [ 2, 3 ]' );
  153. posInRoot.log();
  154. expectLog( 'ModelPosition: main [ 0, 1, 0 ]' );
  155. } );
  156. it( 'for ModelRange', () => {
  157. const rangeInRoot = ModelRange.createIn( modelRoot );
  158. const rangeInElement = ModelRange.createIn( modelElement );
  159. const rangeInDocFrag = ModelRange.createIn( modelDocFrag );
  160. expect( rangeInRoot.toString() ).to.equal( 'main [ 0 ] - [ 0 ]' );
  161. expect( rangeInElement.toString() ).to.equal( '<paragraph> [ 0 ] - [ 3 ]' );
  162. expect( rangeInDocFrag.toString() ).to.equal( 'documentFragment [ 0 ] - [ 3 ]' );
  163. rangeInRoot.log();
  164. expectLog( 'ModelRange: main [ 0 ] - [ 0 ]' );
  165. } );
  166. it( 'for ViewText', () => {
  167. const foo = new ViewText( 'foo' );
  168. expect( foo.toString() ).to.equal( '#foo' );
  169. foo.log();
  170. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  171. foo.logExtended();
  172. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  173. } );
  174. it( 'for ViewTextProxy', () => {
  175. const foo = new ViewText( 'foo', { foo: 'bar' } );
  176. const proxy = new ViewTextProxy( foo, 1, 1 );
  177. expect( proxy.toString() ).to.equal( '#o' );
  178. proxy.log();
  179. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  180. proxy.logExtended();
  181. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  182. } );
  183. describe( 'for operations', () => {
  184. beforeEach( () => {
  185. modelRoot.appendChildren( [ new ModelText( 'foobar' ) ] );
  186. } );
  187. it( 'AttributeOperation', () => {
  188. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  189. expect( op.toString() ).to.equal( 'AttributeOperation( 0 ): "key": null -> {"foo":"bar"}, main [ 0 ] - [ 6 ]' );
  190. op.log();
  191. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  192. } );
  193. it( 'DetachOperation (text node)', () => {
  194. const op = new DetachOperation( ModelPosition.createAt( modelRoot, 0 ), 3 );
  195. expect( op.toString() ).to.equal( 'DetachOperation( null ): #foo -> main [ 0 ] - [ 3 ]' );
  196. op.log();
  197. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  198. } );
  199. it( 'DetachOperation (element)', () => {
  200. const element = new ModelElement( 'element' );
  201. modelRoot.insertChildren( 0, element );
  202. const op = new DetachOperation( ModelPosition.createBefore( element ), 1 );
  203. expect( op.toString() ).to.equal( 'DetachOperation( null ): <element> -> main [ 0 ] - [ 1 ]' );
  204. op.log();
  205. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  206. } );
  207. it( 'DetachOperation (multiple nodes)', () => {
  208. const element = new ModelElement( 'element' );
  209. modelRoot.insertChildren( 0, element );
  210. const op = new DetachOperation( ModelPosition.createBefore( element ), 2 );
  211. expect( op.toString() ).to.equal( 'DetachOperation( null ): [ 2 ] -> main [ 0 ] - [ 2 ]' );
  212. op.log();
  213. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  214. } );
  215. it( 'InsertOperation (text node)', () => {
  216. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  217. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): #abc -> main [ 3 ]' );
  218. op.log();
  219. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  220. } );
  221. it( 'InsertOperation (element)', () => {
  222. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelElement( 'paragraph' ) ], 0 );
  223. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): <paragraph> -> main [ 3 ]' );
  224. op.log();
  225. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  226. } );
  227. it( 'InsertOperation (multiple nodes)', () => {
  228. const nodes = [ new ModelText( 'x' ), new ModelElement( 'y' ), new ModelText( 'z' ) ];
  229. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), nodes, 0 );
  230. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): [ 3 ] -> main [ 3 ]' );
  231. op.log();
  232. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  233. } );
  234. it( 'MarkerOperation', () => {
  235. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  236. expect( op.toString() ).to.equal( 'MarkerOperation( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  237. op.log();
  238. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  239. } );
  240. it( 'MoveOperation', () => {
  241. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  242. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ]' );
  243. op.log();
  244. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  245. } );
  246. it( 'MoveOperation sticky', () => {
  247. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  248. op.isSticky = true;
  249. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ] (sticky)' );
  250. op.log();
  251. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  252. } );
  253. it( 'NoOperation', () => {
  254. const op = new NoOperation( 0 );
  255. expect( op.toString() ).to.equal( 'NoOperation( 0 )' );
  256. op.log();
  257. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  258. } );
  259. it( 'RenameOperation', () => {
  260. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  261. expect( op.toString() ).to.equal( 'RenameOperation( 0 ): main [ 1 ]: "old" -> "new"' );
  262. op.log();
  263. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  264. } );
  265. it( 'RootAttributeOperation', () => {
  266. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  267. expect( op.toString() ).to.equal( 'RootAttributeOperation( 0 ): "key": "old" -> null, main' );
  268. op.log();
  269. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  270. } );
  271. } );
  272. describe( 'for deltas', () => {
  273. it( 'Delta', () => {
  274. const delta = new Delta();
  275. const op = { log: sinon.spy() };
  276. delta.addOperation( op );
  277. sinon.spy( delta, 'log' );
  278. delta.logAll();
  279. expect( op.log.called ).to.be.true;
  280. expect( delta.log.called ).to.be.true;
  281. } );
  282. it( 'AttributeDelta', () => {
  283. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  284. const delta = new AttributeDelta();
  285. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  286. delta.addOperation( op );
  287. expect( delta.toString() ).to.equal( 'AttributeDelta( 0 ): "key": -> {"foo":"bar"}, main [ 0 ] - [ 6 ], 1 ops' );
  288. delta.log();
  289. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  290. } );
  291. it( 'InsertDelta (text node)', () => {
  292. const delta = new InsertDelta();
  293. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  294. delta.addOperation( op );
  295. expect( delta.toString() ).to.equal( 'InsertDelta( 0 ): #abc -> main [ 3 ]' );
  296. delta.log();
  297. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  298. } );
  299. it( 'InsertDelta (element)', () => {
  300. const delta = new InsertDelta();
  301. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelElement( 'paragraph' ) ], 0 );
  302. delta.addOperation( op );
  303. expect( delta.toString() ).to.equal( 'InsertDelta( 0 ): <paragraph> -> main [ 3 ]' );
  304. delta.log();
  305. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  306. } );
  307. it( 'InsertDelta (multiple nodes)', () => {
  308. const delta = new InsertDelta();
  309. const nodes = [ new ModelText( 'x' ), new ModelElement( 'y' ), new ModelText( 'z' ) ];
  310. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), nodes, 0 );
  311. delta.addOperation( op );
  312. expect( delta.toString() ).to.equal( 'InsertDelta( 0 ): [ 3 ] -> main [ 3 ]' );
  313. delta.log();
  314. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  315. } );
  316. it( 'MarkerDelta', () => {
  317. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  318. const delta = new MarkerDelta();
  319. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  320. delta.addOperation( op );
  321. expect( delta.toString() ).to.equal( 'MarkerDelta( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  322. delta.log();
  323. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  324. } );
  325. it( 'MergeDelta', () => {
  326. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  327. const firstEle = new ModelElement( 'paragraph' );
  328. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  329. otherRoot.appendChildren( [ firstEle, removedEle ] );
  330. const graveyard = modelDoc.graveyard;
  331. const delta = new MergeDelta();
  332. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  333. const remove = new RemoveOperation(
  334. ModelPosition.createBefore( removedEle ),
  335. 1,
  336. ModelPosition.createAt( graveyard, 0 ),
  337. 1
  338. );
  339. delta.addOperation( move );
  340. delta.addOperation( remove );
  341. expect( delta.toString() ).to.equal( 'MergeDelta( 0 ): otherRoot [ 1 ]' );
  342. delta.log();
  343. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  344. } );
  345. it( 'MergeDelta - NoOperation as second operation', () => {
  346. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  347. const firstEle = new ModelElement( 'paragraph' );
  348. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  349. otherRoot.appendChildren( [ firstEle, removedEle ] );
  350. const delta = new MergeDelta();
  351. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  352. delta.addOperation( move );
  353. delta.addOperation( new NoOperation( 1 ) );
  354. expect( delta.toString() ).to.equal( 'MergeDelta( 0 ): (move from otherRoot [ 1, 0 ])' );
  355. delta.log();
  356. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  357. } );
  358. it( 'MoveDelta', () => {
  359. const delta = new MoveDelta();
  360. const move1 = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 1, ModelPosition.createAt( modelRoot, 3 ), 0 );
  361. const move2 = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 1, ModelPosition.createAt( modelRoot, 6 ), 0 );
  362. delta.addOperation( move1 );
  363. delta.addOperation( move2 );
  364. expect( delta.toString() ).to.equal( 'MoveDelta( 0 ): main [ 0 ] - [ 1 ] -> main [ 3 ]; main [ 1 ] - [ 2 ] -> main [ 6 ]' );
  365. delta.log();
  366. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  367. } );
  368. it( 'RenameDelta', () => {
  369. const delta = new RenameDelta();
  370. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  371. delta.addOperation( op );
  372. expect( delta.toString() ).to.equal( 'RenameDelta( 0 ): main [ 1 ]: "old" -> "new"' );
  373. delta.log();
  374. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  375. } );
  376. it( 'RootAttributeDelta', () => {
  377. const delta = new RootAttributeDelta();
  378. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  379. delta.addOperation( op );
  380. expect( delta.toString() ).to.equal( 'RootAttributeDelta( 0 ): "key": "old" -> null, main' );
  381. delta.log();
  382. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  383. } );
  384. it( 'SplitDelta', () => {
  385. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  386. const splitEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  387. otherRoot.appendChildren( [ splitEle ] );
  388. const delta = new SplitDelta();
  389. const insert = new InsertOperation( ModelPosition.createAt( otherRoot, 1 ), [ new ModelElement( 'paragraph' ) ], 0 );
  390. const move = new MoveOperation( ModelPosition.createAt( splitEle, 1 ), 2, new ModelPosition( otherRoot, [ 1, 0 ] ), 1 );
  391. delta.addOperation( insert );
  392. delta.addOperation( move );
  393. expect( delta.toString() ).to.equal( 'SplitDelta( 0 ): otherRoot [ 0, 1 ]' );
  394. delta.log();
  395. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  396. } );
  397. it( 'SplitDelta - NoOperation as second operation', () => {
  398. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  399. const splitEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  400. otherRoot.appendChildren( [ splitEle ] );
  401. const delta = new SplitDelta();
  402. const insert = new InsertOperation( ModelPosition.createAt( otherRoot, 1 ), [ new ModelElement( 'paragraph' ) ], 0 );
  403. const move = new NoOperation( 1 );
  404. delta.addOperation( insert );
  405. delta.addOperation( move );
  406. expect( delta.toString() ).to.equal( 'SplitDelta( 0 ): (clone to otherRoot [ 1 ])' );
  407. delta.log();
  408. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  409. } );
  410. it( 'SplitDelta - NoOperation as second operation, MoveOperation as first operation', () => {
  411. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  412. const delta = new SplitDelta();
  413. const insert = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 1, ModelPosition.createAt( otherRoot, 1 ), 0 );
  414. const move = new NoOperation( 1 );
  415. delta.addOperation( insert );
  416. delta.addOperation( move );
  417. expect( delta.toString() ).to.equal( 'SplitDelta( 0 ): (clone to otherRoot [ 1 ])' );
  418. delta.log();
  419. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  420. } );
  421. it( 'UnwrapDelta', () => {
  422. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  423. const unwrapEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  424. otherRoot.appendChildren( [ unwrapEle ] );
  425. const graveyard = modelDoc.graveyard;
  426. const delta = new UnwrapDelta();
  427. const move = new MoveOperation( ModelPosition.createAt( unwrapEle, 0 ), 3, ModelPosition.createAt( otherRoot, 0 ), 0 );
  428. const remove = new RemoveOperation( ModelPosition.createAt( otherRoot, 3 ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  429. delta.addOperation( move );
  430. delta.addOperation( remove );
  431. expect( delta.toString() ).to.equal( 'UnwrapDelta( 0 ): otherRoot [ 0 ]' );
  432. delta.log();
  433. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  434. } );
  435. it( 'WrapDelta', () => {
  436. const delta = new WrapDelta();
  437. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 6 ), new ModelElement( 'paragraph' ), 0 );
  438. const move = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 6, new ModelPosition( modelRoot, [ 1, 0 ] ), 1 );
  439. delta.addOperation( insert );
  440. delta.addOperation( move );
  441. expect( delta.toString() ).to.equal( 'WrapDelta( 0 ): main [ 0 ] - [ 6 ] -> <paragraph>' );
  442. delta.log();
  443. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  444. } );
  445. } );
  446. it( 'for applied operations', () => {
  447. const delta = new InsertDelta();
  448. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), [ new ModelText( 'foo' ) ], 0 );
  449. delta.addOperation( op );
  450. model.applyOperation( op );
  451. expect( log.calledWithExactly( 'Applying InsertOperation( 0 ): #foo -> main [ 0 ]' ) ).to.be.true;
  452. } );
  453. } );
  454. describe( 'should provide tree printing tools', () => {
  455. it( 'for model', () => {
  456. const model = new Model();
  457. const modelDoc = model.document;
  458. const modelRoot = modelDoc.createRoot();
  459. modelRoot.appendChildren( [
  460. new ModelElement( 'paragraph', { foo: 'bar' }, [
  461. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  462. ] ),
  463. new ModelElement( 'listItem', { type: 'numbered', indent: 0 }, new ModelText( 'One.' ) ),
  464. ] );
  465. const modelRootTree = modelRoot.printTree();
  466. expect( modelRootTree ).to.equal(
  467. '<main>' +
  468. '\n\t<paragraph foo="bar">' +
  469. '\n\t\tThis is ' +
  470. '\n\t\t<$text bold=true>bold</$text>' +
  471. '\n\t\t.' +
  472. '\n\t</paragraph>' +
  473. '\n\t<listItem type="numbered" indent=0>' +
  474. '\n\t\tOne.' +
  475. '\n\t</listItem>' +
  476. '\n</main>'
  477. );
  478. modelRoot.logTree();
  479. expect( log.calledWithExactly( modelRootTree ) ).to.be.true;
  480. const modelParagraph = modelRoot.getChild( 0 );
  481. const modelParagraphTree = modelParagraph.printTree();
  482. expect( modelParagraphTree ).to.equal(
  483. '<paragraph foo="bar">' +
  484. '\n\tThis is ' +
  485. '\n\t<$text bold=true>bold</$text>' +
  486. '\n\t.' +
  487. '\n</paragraph>'
  488. );
  489. log.reset();
  490. modelParagraph.logTree();
  491. expect( log.calledWithExactly( modelParagraphTree ) ).to.be.true;
  492. const modelDocFrag = new ModelDocumentFragment( [
  493. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' ),
  494. new ModelElement( 'paragraph', { foo: 'bar' }, [
  495. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  496. ] )
  497. ] );
  498. const modelDocFragTree = modelDocFrag.printTree();
  499. expect( modelDocFragTree ).to.equal(
  500. 'ModelDocumentFragment: [' +
  501. '\n\tThis is ' +
  502. '\n\t<$text bold=true>bold</$text>' +
  503. '\n\t.' +
  504. '\n\t<paragraph foo="bar">' +
  505. '\n\t\tThis is ' +
  506. '\n\t\t<$text bold=true>bold</$text>' +
  507. '\n\t\t.' +
  508. '\n\t</paragraph>' +
  509. '\n]'
  510. );
  511. log.reset();
  512. modelDocFrag.logTree();
  513. expect( log.calledWithExactly( modelDocFragTree ) ).to.be.true;
  514. } );
  515. it( 'for view', () => {
  516. const viewDoc = new ViewDocument();
  517. const viewRoot = createViewRoot( viewDoc );
  518. viewRoot.appendChildren( [
  519. new ViewContainerElement( 'p', { foo: 'bar' }, [
  520. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  521. ] ),
  522. new ViewContainerElement( 'ol', null, [
  523. new ViewContainerElement( 'li', null, new ViewText( 'One.' ) )
  524. ] )
  525. ] );
  526. const viewRootTree = viewRoot.printTree();
  527. expect( viewRootTree ).to.equal(
  528. '<div>' +
  529. '\n\t<p foo="bar">' +
  530. '\n\t\tThis is ' +
  531. '\n\t\t<b>' +
  532. '\n\t\t\tbold' +
  533. '\n\t\t</b>' +
  534. '\n\t\t.' +
  535. '\n\t</p>' +
  536. '\n\t<ol>' +
  537. '\n\t\t<li>' +
  538. '\n\t\t\tOne.' +
  539. '\n\t\t</li>' +
  540. '\n\t</ol>' +
  541. '\n</div>'
  542. );
  543. viewRoot.logTree();
  544. expect( log.calledWithExactly( viewRootTree ) ).to.be.true;
  545. const viewParagraph = viewRoot.getChild( 0 );
  546. const viewParagraphTree = viewParagraph.printTree();
  547. expect( viewParagraphTree ).to.equal(
  548. '<p foo="bar">' +
  549. '\n\tThis is ' +
  550. '\n\t<b>' +
  551. '\n\t\tbold' +
  552. '\n\t</b>' +
  553. '\n\t.' +
  554. '\n</p>'
  555. );
  556. log.reset();
  557. viewParagraph.logTree();
  558. expect( log.calledWithExactly( viewParagraphTree ) ).to.be.true;
  559. const viewDocFrag = new ViewDocumentFragment( [
  560. new ViewText( 'Text.' ),
  561. new ViewContainerElement( 'p', { foo: 'bar' }, [
  562. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  563. ] )
  564. ] );
  565. const viewDocFragTree = viewDocFrag.printTree();
  566. expect( viewDocFragTree ).to.equal(
  567. 'ViewDocumentFragment: [' +
  568. '\n\tText.' +
  569. '\n\t<p foo="bar">' +
  570. '\n\t\tThis is ' +
  571. '\n\t\t<b>' +
  572. '\n\t\t\tbold' +
  573. '\n\t\t</b>' +
  574. '\n\t\t.' +
  575. '\n\t</p>' +
  576. '\n]'
  577. );
  578. log.reset();
  579. viewDocFrag.logTree();
  580. expect( log.calledWithExactly( viewDocFragTree ) ).to.be.true;
  581. } );
  582. } );
  583. describe( 'should store model and view trees state', () => {
  584. let editor;
  585. beforeEach( () => {
  586. return VirtualTestEditor.create( {
  587. plugins: [ DebugPlugin ]
  588. } ).then( _editor => {
  589. editor = _editor;
  590. } );
  591. } );
  592. it( 'should store model and view state after each applied operation', () => {
  593. const model = editor.model;
  594. const modelDoc = model.document;
  595. const modelRoot = modelDoc.getRoot();
  596. const viewDoc = editor.editing.view;
  597. model.change( () => {
  598. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), 0 );
  599. model.applyOperation( wrapInDelta( insert ) );
  600. const graveyard = modelDoc.graveyard;
  601. const remove = new RemoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( graveyard, 0 ), 1 );
  602. model.applyOperation( wrapInDelta( remove ) );
  603. } );
  604. log.reset();
  605. modelDoc.log( 0 );
  606. expectLog(
  607. '<$graveyard></$graveyard>' +
  608. '\n<main></main>'
  609. );
  610. modelDoc.log( 1 );
  611. expectLog(
  612. '<$graveyard></$graveyard>' +
  613. '\n<main>' +
  614. '\n\tfoobar' +
  615. '\n</main>'
  616. );
  617. modelDoc.log( 2 );
  618. expectLog(
  619. '<$graveyard>' +
  620. '\n\too' +
  621. '\n</$graveyard>' +
  622. '\n<main>' +
  623. '\n\tfbar' +
  624. '\n</main>'
  625. );
  626. modelDoc.log();
  627. expectLog(
  628. '<$graveyard>' +
  629. '\n\too' +
  630. '\n</$graveyard>' +
  631. '\n<main>' +
  632. '\n\tfbar' +
  633. '\n</main>'
  634. );
  635. viewDoc.log( 0 );
  636. expectLog(
  637. '<$root></$root>'
  638. );
  639. viewDoc.log( 2 );
  640. expectLog(
  641. '<$root>' +
  642. '\n\tfbar' +
  643. '\n</$root>'
  644. );
  645. sinon.spy( modelDoc, 'log' );
  646. sinon.spy( viewDoc, 'log' );
  647. editor.logModel( 1 );
  648. expect( modelDoc.log.calledWithExactly( 1 ), 1 ).to.be.true;
  649. editor.logView( 2 );
  650. expect( viewDoc.log.calledWithExactly( 2 ), 2 ).to.be.true;
  651. modelDoc.log.reset();
  652. viewDoc.log.reset();
  653. editor.logModel();
  654. expect( modelDoc.log.calledWithExactly( 2 ), 3 ).to.be.true;
  655. modelDoc.log.reset();
  656. viewDoc.log.reset();
  657. editor.logDocuments();
  658. expect( modelDoc.log.calledWithExactly( 2 ), 4 ).to.be.true;
  659. expect( viewDoc.log.calledWithExactly( 2 ), 5 ).to.be.true;
  660. modelDoc.log.reset();
  661. viewDoc.log.reset();
  662. editor.logDocuments( 1 );
  663. expect( modelDoc.log.calledWithExactly( 1 ), 6 ).to.be.true;
  664. expect( viewDoc.log.calledWithExactly( 1 ), 7 ).to.be.true;
  665. } );
  666. it( 'should remove old states', () => {
  667. const model = editor.model;
  668. const modelDoc = model.document;
  669. const modelRoot = model.document.getRoot();
  670. for ( let i = 0; i < 25; i++ ) {
  671. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), modelDoc.version );
  672. model.applyOperation( wrapInDelta( insert ) );
  673. }
  674. modelDoc.log( 0 );
  675. expectLog( 'Tree log unavailable for given version: 0' );
  676. } );
  677. } );
  678. describe( 'should provide methods for delta replayer', () => {
  679. it( 'getAppliedDeltas()', () => {
  680. const model = new Model();
  681. const modelDoc = model.document;
  682. expect( model.getAppliedDeltas() ).to.equal( '' );
  683. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  684. const firstEle = new ModelElement( 'paragraph' );
  685. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  686. otherRoot.appendChildren( [ firstEle, removedEle ] );
  687. const delta = new MergeDelta();
  688. const graveyard = modelDoc.graveyard;
  689. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  690. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  691. delta.addOperation( move );
  692. delta.addOperation( remove );
  693. model.applyOperation( move );
  694. model.applyOperation( remove );
  695. const stringifiedDeltas = model.getAppliedDeltas();
  696. expect( stringifiedDeltas ).to.equal( JSON.stringify( delta.toJSON() ) );
  697. } );
  698. it( 'createReplayer()', () => {
  699. const model = new Model();
  700. const modelDoc = model.document;
  701. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  702. const firstEle = new ModelElement( 'paragraph' );
  703. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  704. otherRoot.appendChildren( [ firstEle, removedEle ] );
  705. const delta = new MergeDelta();
  706. const graveyard = modelDoc.graveyard;
  707. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  708. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  709. delta.addOperation( move );
  710. delta.addOperation( remove );
  711. const stringifiedDeltas = JSON.stringify( delta.toJSON() );
  712. const deltaReplayer = model.createReplayer( stringifiedDeltas );
  713. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ JSON.parse( stringifiedDeltas ) ] );
  714. } );
  715. } );
  716. describe( 'should provide debug tools for delta transformation', () => {
  717. let model, document, root, otherRoot;
  718. beforeEach( () => {
  719. model = new Model();
  720. document = model.document;
  721. root = document.createRoot();
  722. otherRoot = document.createRoot( 'other', 'other' );
  723. } );
  724. it( 'Delta#_saveHistory()', () => {
  725. const insertDeltaA = new InsertDelta();
  726. const insertOpA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  727. insertDeltaA.addOperation( insertOpA );
  728. const insertDeltaB = new InsertDelta();
  729. const insertOpB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  730. insertDeltaB.addOperation( insertOpB );
  731. const insertDeltaFinalA = new InsertDelta();
  732. const insertOpFinalA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  733. insertDeltaFinalA.addOperation( insertOpFinalA );
  734. const insertDeltaFinalAJsonWithoutHistory = JSON.stringify( insertDeltaFinalA );
  735. insertDeltaFinalA._saveHistory( {
  736. before: insertDeltaA,
  737. transformedBy: insertDeltaB,
  738. wasImportant: true,
  739. resultIndex: 0,
  740. resultsTotal: 1
  741. } );
  742. const insertDeltaC = new InsertDelta();
  743. const insertOpC = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  744. insertDeltaC.addOperation( insertOpC );
  745. const insertDeltaFinalB = new InsertDelta();
  746. const insertOpFinalB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 2 );
  747. insertDeltaFinalB.addOperation( insertOpFinalB );
  748. insertDeltaFinalB._saveHistory( {
  749. before: insertDeltaFinalA,
  750. transformedBy: insertDeltaC,
  751. wasImportant: false,
  752. resultIndex: 1,
  753. resultsTotal: 3
  754. } );
  755. expect( insertDeltaA.history ).to.be.undefined;
  756. expect( insertDeltaB.history ).to.be.undefined;
  757. expect( insertDeltaFinalA.history ).not.to.be.undefined;
  758. expect( insertDeltaFinalA.history.length ).to.equal( 1 );
  759. expect( insertDeltaFinalA.history[ 0 ] ).to.deep.equal( {
  760. before: JSON.stringify( insertDeltaA ),
  761. transformedBy: JSON.stringify( insertDeltaB ),
  762. wasImportant: true,
  763. resultIndex: 0,
  764. resultsTotal: 1
  765. } );
  766. expect( insertDeltaFinalB.history ).not.to.be.undefined;
  767. expect( insertDeltaFinalB.history.length ).to.equal( 2 );
  768. expect( insertDeltaFinalB.history[ 0 ] ).to.deep.equal( {
  769. before: JSON.stringify( insertDeltaA ),
  770. transformedBy: JSON.stringify( insertDeltaB ),
  771. wasImportant: true,
  772. resultIndex: 0,
  773. resultsTotal: 1
  774. } );
  775. expect( insertDeltaFinalB.history[ 1 ] ).to.deep.equal( {
  776. before: insertDeltaFinalAJsonWithoutHistory,
  777. transformedBy: JSON.stringify( insertDeltaC ),
  778. wasImportant: false,
  779. resultIndex: 1,
  780. resultsTotal: 3
  781. } );
  782. } );
  783. it( 'save delta history on deltaTransform.transform', () => {
  784. const deltaA = new MoveDelta();
  785. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  786. deltaA.addOperation( opA );
  787. const deltaB = new InsertDelta();
  788. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  789. deltaB.addOperation( opB );
  790. const deltaC = new MoveDelta();
  791. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  792. deltaC.addOperation( opC );
  793. let result = deltaTransform.transform( deltaA, deltaB, { document, wasAffected: new Map() } );
  794. expect( result[ 0 ].history ).not.to.be.undefined;
  795. expect( result[ 0 ].history.length ).to.equal( 1 );
  796. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  797. before: JSON.stringify( deltaA ),
  798. transformedBy: JSON.stringify( deltaB ),
  799. wasImportant: false,
  800. resultIndex: 0,
  801. resultsTotal: 1
  802. } );
  803. const firstResultWithoutHistory = result[ 0 ].clone();
  804. delete firstResultWithoutHistory.history;
  805. result = deltaTransform.transform( result[ 0 ], deltaC, {
  806. isStrong: true,
  807. document,
  808. wasAffected: new Map()
  809. } );
  810. expect( result[ 0 ].history ).not.to.be.undefined;
  811. expect( result[ 0 ].history.length ).to.equal( 2 );
  812. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  813. before: JSON.stringify( deltaA ),
  814. transformedBy: JSON.stringify( deltaB ),
  815. wasImportant: false,
  816. resultIndex: 0,
  817. resultsTotal: 1
  818. } );
  819. expect( result[ 0 ].history[ 1 ] ).to.deep.equal( {
  820. before: JSON.stringify( firstResultWithoutHistory ),
  821. transformedBy: JSON.stringify( deltaC ),
  822. wasImportant: true,
  823. resultIndex: 0,
  824. resultsTotal: 2
  825. } );
  826. } );
  827. it( 'recreate delta using Delta#history', () => {
  828. const deltaA = new MoveDelta();
  829. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  830. deltaA.addOperation( opA );
  831. const deltaB = new InsertDelta();
  832. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  833. deltaB.addOperation( opB );
  834. const deltaC = new MoveDelta();
  835. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  836. deltaC.addOperation( opC );
  837. let original = deltaTransform.transform( deltaA, deltaB, { document, wasAffected: new Map() } );
  838. original = deltaTransform.transform( original[ 0 ], deltaC, {
  839. isStrong: true,
  840. document,
  841. wasAffected: new Map()
  842. } )[ 0 ];
  843. const history = original.history;
  844. let recreated = deltaTransform.transform(
  845. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].before ), document ),
  846. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].transformedBy ), document ),
  847. { isStrong: history[ 0 ].wasImportant, document, wasAffected: new Map() }
  848. );
  849. recreated = recreated[ history[ 0 ].resultIndex ];
  850. recreated = deltaTransform.transform(
  851. recreated,
  852. DeltaFactory.fromJSON( JSON.parse( history[ 1 ].transformedBy ), document ),
  853. { isStrong: history[ 1 ].wasImportant, document, wasAffected: new Map() }
  854. );
  855. recreated = recreated[ history[ 1 ].resultIndex ];
  856. delete original.history;
  857. delete recreated.history;
  858. expect( JSON.stringify( recreated ) ).to.equal( JSON.stringify( original ) );
  859. } );
  860. describe( 'provide additional logging when transformation crashes', () => {
  861. it( 'with more important delta A', () => {
  862. const deltaA = new MoveDelta();
  863. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  864. deltaA.addOperation( opA );
  865. const deltaB = new InsertDelta();
  866. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  867. deltaB.addOperation( opB );
  868. testUtils.sinon.stub( deltaTransform, 'defaultTransform' ).throws( new Error() );
  869. expect( () => deltaTransform.transform( deltaA, deltaB, { isStrong: true } ) ).to.throw( Error );
  870. expect( error.calledWith( deltaA.toString() + ' (important)' ) ).to.be.true;
  871. expect( error.calledWith( deltaB.toString() ) ).to.be.true;
  872. } );
  873. it( 'with more important delta B', () => {
  874. const deltaA = new MoveDelta();
  875. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  876. deltaA.addOperation( opA );
  877. const deltaB = new InsertDelta();
  878. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  879. deltaB.addOperation( opB );
  880. testUtils.sinon.stub( deltaTransform, 'defaultTransform' ).throws( new Error() );
  881. expect( () => deltaTransform.transform( deltaA, deltaB, { isStrong: false } ) ).to.throw( Error );
  882. expect( error.calledWith( deltaA.toString() ) ).to.be.true;
  883. expect( error.calledWith( deltaB.toString() + ' (important)' ) ).to.be.true;
  884. } );
  885. } );
  886. } );
  887. function expectLog( expectedLogMsg ) {
  888. expect( log.calledWithExactly( expectedLogMsg ) ).to.be.true;
  889. log.reset();
  890. }
  891. function wrapInDelta( op ) {
  892. const delta = new Delta();
  893. delta.addOperation( op );
  894. return op;
  895. }
  896. } );