enableenginedebug.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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.resetHistory();
  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._appendChild( [ 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._insertChild( 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._insertChild( 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._appendChild( 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._appendChild( 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._appendChild( [ 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._appendChild( [ 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._appendChild( [ 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._appendChild( [ 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._appendChild( [ 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._appendChild( [
  460. new ModelElement( 'paragraph', { foo: 'bar' }, [
  461. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  462. ] ),
  463. new ModelElement( 'listItem', { listType: 'numbered', listIndent: 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 listType="numbered" listIndent=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.resetHistory();
  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.resetHistory();
  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._appendChild( [
  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.resetHistory();
  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.resetHistory();
  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 view = editor.editing.view;
  597. const viewDoc = view.document;
  598. model.change( () => {
  599. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), 0 );
  600. model.applyOperation( wrapInDelta( insert ) );
  601. const graveyard = modelDoc.graveyard;
  602. const remove = new RemoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( graveyard, 0 ), 1 );
  603. model.applyOperation( wrapInDelta( remove ) );
  604. } );
  605. log.resetHistory();
  606. modelDoc.log( 0 );
  607. expectLog(
  608. '<$graveyard></$graveyard>' +
  609. '\n<main></main>'
  610. );
  611. modelDoc.log( 1 );
  612. expectLog(
  613. '<$graveyard></$graveyard>' +
  614. '\n<main>' +
  615. '\n\tfoobar' +
  616. '\n</main>'
  617. );
  618. modelDoc.log( 2 );
  619. expectLog(
  620. '<$graveyard>' +
  621. '\n\too' +
  622. '\n</$graveyard>' +
  623. '\n<main>' +
  624. '\n\tfbar' +
  625. '\n</main>'
  626. );
  627. modelDoc.log();
  628. expectLog(
  629. '<$graveyard>' +
  630. '\n\too' +
  631. '\n</$graveyard>' +
  632. '\n<main>' +
  633. '\n\tfbar' +
  634. '\n</main>'
  635. );
  636. viewDoc.log( 0 );
  637. expectLog(
  638. '<$root></$root>'
  639. );
  640. viewDoc.log( 2 );
  641. expectLog(
  642. '<$root>' +
  643. '\n\tfbar' +
  644. '\n</$root>'
  645. );
  646. sinon.spy( modelDoc, 'log' );
  647. sinon.spy( viewDoc, 'log' );
  648. editor.logModel( 1 );
  649. expect( modelDoc.log.calledWithExactly( 1 ), 1 ).to.be.true;
  650. editor.logView( 2 );
  651. expect( viewDoc.log.calledWithExactly( 2 ), 2 ).to.be.true;
  652. modelDoc.log.resetHistory();
  653. viewDoc.log.resetHistory();
  654. editor.logModel();
  655. expect( modelDoc.log.calledWithExactly( 2 ), 3 ).to.be.true;
  656. modelDoc.log.resetHistory();
  657. viewDoc.log.resetHistory();
  658. editor.logDocuments();
  659. expect( modelDoc.log.calledWithExactly( 2 ), 4 ).to.be.true;
  660. expect( viewDoc.log.calledWithExactly( 2 ), 5 ).to.be.true;
  661. modelDoc.log.resetHistory();
  662. viewDoc.log.resetHistory();
  663. editor.logDocuments( 1 );
  664. expect( modelDoc.log.calledWithExactly( 1 ), 6 ).to.be.true;
  665. expect( viewDoc.log.calledWithExactly( 1 ), 7 ).to.be.true;
  666. } );
  667. it( 'should remove old states', () => {
  668. const model = editor.model;
  669. const modelDoc = model.document;
  670. const modelRoot = model.document.getRoot();
  671. for ( let i = 0; i < 25; i++ ) {
  672. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), modelDoc.version );
  673. model.applyOperation( wrapInDelta( insert ) );
  674. }
  675. modelDoc.log( 0 );
  676. expectLog( 'Tree log unavailable for given version: 0' );
  677. } );
  678. } );
  679. describe( 'should provide methods for delta replayer', () => {
  680. it( 'getAppliedDeltas()', () => {
  681. const model = new Model();
  682. const modelDoc = model.document;
  683. expect( model.getAppliedDeltas() ).to.equal( '' );
  684. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  685. const firstEle = new ModelElement( 'paragraph' );
  686. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  687. otherRoot._appendChild( [ firstEle, removedEle ] );
  688. const delta = new MergeDelta();
  689. const graveyard = modelDoc.graveyard;
  690. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  691. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  692. delta.addOperation( move );
  693. delta.addOperation( remove );
  694. model.applyOperation( move );
  695. model.applyOperation( remove );
  696. const stringifiedDeltas = model.getAppliedDeltas();
  697. expect( stringifiedDeltas ).to.equal( JSON.stringify( delta.toJSON() ) );
  698. } );
  699. it( 'createReplayer()', () => {
  700. const model = new Model();
  701. const modelDoc = model.document;
  702. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  703. const firstEle = new ModelElement( 'paragraph' );
  704. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  705. otherRoot._appendChild( [ firstEle, removedEle ] );
  706. const delta = new MergeDelta();
  707. const graveyard = modelDoc.graveyard;
  708. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  709. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  710. delta.addOperation( move );
  711. delta.addOperation( remove );
  712. const stringifiedDeltas = JSON.stringify( delta.toJSON() );
  713. const deltaReplayer = model.createReplayer( stringifiedDeltas );
  714. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ JSON.parse( stringifiedDeltas ) ] );
  715. } );
  716. } );
  717. describe( 'should provide debug tools for delta transformation', () => {
  718. let model, document, root, otherRoot;
  719. beforeEach( () => {
  720. model = new Model();
  721. document = model.document;
  722. root = document.createRoot();
  723. otherRoot = document.createRoot( 'other', 'other' );
  724. } );
  725. it( 'Delta#_saveHistory()', () => {
  726. const insertDeltaA = new InsertDelta();
  727. const insertOpA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  728. insertDeltaA.addOperation( insertOpA );
  729. const insertDeltaB = new InsertDelta();
  730. const insertOpB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  731. insertDeltaB.addOperation( insertOpB );
  732. const insertDeltaFinalA = new InsertDelta();
  733. const insertOpFinalA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  734. insertDeltaFinalA.addOperation( insertOpFinalA );
  735. const insertDeltaFinalAJsonWithoutHistory = JSON.stringify( insertDeltaFinalA );
  736. insertDeltaFinalA._saveHistory( {
  737. before: insertDeltaA,
  738. transformedBy: insertDeltaB,
  739. wasImportant: true,
  740. resultIndex: 0,
  741. resultsTotal: 1
  742. } );
  743. const insertDeltaC = new InsertDelta();
  744. const insertOpC = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  745. insertDeltaC.addOperation( insertOpC );
  746. const insertDeltaFinalB = new InsertDelta();
  747. const insertOpFinalB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 2 );
  748. insertDeltaFinalB.addOperation( insertOpFinalB );
  749. insertDeltaFinalB._saveHistory( {
  750. before: insertDeltaFinalA,
  751. transformedBy: insertDeltaC,
  752. wasImportant: false,
  753. resultIndex: 1,
  754. resultsTotal: 3
  755. } );
  756. expect( insertDeltaA.history ).to.be.undefined;
  757. expect( insertDeltaB.history ).to.be.undefined;
  758. expect( insertDeltaFinalA.history ).not.to.be.undefined;
  759. expect( insertDeltaFinalA.history.length ).to.equal( 1 );
  760. expect( insertDeltaFinalA.history[ 0 ] ).to.deep.equal( {
  761. before: JSON.stringify( insertDeltaA ),
  762. transformedBy: JSON.stringify( insertDeltaB ),
  763. wasImportant: true,
  764. resultIndex: 0,
  765. resultsTotal: 1
  766. } );
  767. expect( insertDeltaFinalB.history ).not.to.be.undefined;
  768. expect( insertDeltaFinalB.history.length ).to.equal( 2 );
  769. expect( insertDeltaFinalB.history[ 0 ] ).to.deep.equal( {
  770. before: JSON.stringify( insertDeltaA ),
  771. transformedBy: JSON.stringify( insertDeltaB ),
  772. wasImportant: true,
  773. resultIndex: 0,
  774. resultsTotal: 1
  775. } );
  776. expect( insertDeltaFinalB.history[ 1 ] ).to.deep.equal( {
  777. before: insertDeltaFinalAJsonWithoutHistory,
  778. transformedBy: JSON.stringify( insertDeltaC ),
  779. wasImportant: false,
  780. resultIndex: 1,
  781. resultsTotal: 3
  782. } );
  783. } );
  784. it( 'save delta history on deltaTransform.transform', () => {
  785. const deltaA = new MoveDelta();
  786. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  787. deltaA.addOperation( opA );
  788. const deltaB = new InsertDelta();
  789. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  790. deltaB.addOperation( opB );
  791. const deltaC = new MoveDelta();
  792. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  793. deltaC.addOperation( opC );
  794. let result = deltaTransform.transform( deltaA, deltaB, { document, wasAffected: new Map() } );
  795. expect( result[ 0 ].history ).not.to.be.undefined;
  796. expect( result[ 0 ].history.length ).to.equal( 1 );
  797. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  798. before: JSON.stringify( deltaA ),
  799. transformedBy: JSON.stringify( deltaB ),
  800. wasImportant: false,
  801. resultIndex: 0,
  802. resultsTotal: 1
  803. } );
  804. const firstResultWithoutHistory = result[ 0 ].clone();
  805. delete firstResultWithoutHistory.history;
  806. result = deltaTransform.transform( result[ 0 ], deltaC, {
  807. isStrong: true,
  808. document,
  809. wasAffected: new Map()
  810. } );
  811. expect( result[ 0 ].history ).not.to.be.undefined;
  812. expect( result[ 0 ].history.length ).to.equal( 2 );
  813. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  814. before: JSON.stringify( deltaA ),
  815. transformedBy: JSON.stringify( deltaB ),
  816. wasImportant: false,
  817. resultIndex: 0,
  818. resultsTotal: 1
  819. } );
  820. expect( result[ 0 ].history[ 1 ] ).to.deep.equal( {
  821. before: JSON.stringify( firstResultWithoutHistory ),
  822. transformedBy: JSON.stringify( deltaC ),
  823. wasImportant: true,
  824. resultIndex: 0,
  825. resultsTotal: 2
  826. } );
  827. } );
  828. it( 'recreate delta using Delta#history', () => {
  829. const deltaA = new MoveDelta();
  830. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  831. deltaA.addOperation( opA );
  832. const deltaB = new InsertDelta();
  833. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  834. deltaB.addOperation( opB );
  835. const deltaC = new MoveDelta();
  836. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  837. deltaC.addOperation( opC );
  838. let original = deltaTransform.transform( deltaA, deltaB, { document, wasAffected: new Map() } );
  839. original = deltaTransform.transform( original[ 0 ], deltaC, {
  840. isStrong: true,
  841. document,
  842. wasAffected: new Map()
  843. } )[ 0 ];
  844. const history = original.history;
  845. let recreated = deltaTransform.transform(
  846. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].before ), document ),
  847. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].transformedBy ), document ),
  848. { isStrong: history[ 0 ].wasImportant, document, wasAffected: new Map() }
  849. );
  850. recreated = recreated[ history[ 0 ].resultIndex ];
  851. recreated = deltaTransform.transform(
  852. recreated,
  853. DeltaFactory.fromJSON( JSON.parse( history[ 1 ].transformedBy ), document ),
  854. { isStrong: history[ 1 ].wasImportant, document, wasAffected: new Map() }
  855. );
  856. recreated = recreated[ history[ 1 ].resultIndex ];
  857. delete original.history;
  858. delete recreated.history;
  859. expect( JSON.stringify( recreated ) ).to.equal( JSON.stringify( original ) );
  860. } );
  861. describe( 'provide additional logging when transformation crashes', () => {
  862. it( 'with more important delta A', () => {
  863. const deltaA = new MoveDelta();
  864. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  865. deltaA.addOperation( opA );
  866. const deltaB = new InsertDelta();
  867. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  868. deltaB.addOperation( opB );
  869. testUtils.sinon.stub( deltaTransform, 'defaultTransform' ).throws( new Error() );
  870. expect( () => deltaTransform.transform( deltaA, deltaB, { isStrong: true } ) ).to.throw( Error );
  871. expect( error.calledWith( deltaA.toString() + ' (important)' ) ).to.be.true;
  872. expect( error.calledWith( deltaB.toString() ) ).to.be.true;
  873. } );
  874. it( 'with more important delta B', () => {
  875. const deltaA = new MoveDelta();
  876. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  877. deltaA.addOperation( opA );
  878. const deltaB = new InsertDelta();
  879. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  880. deltaB.addOperation( opB );
  881. testUtils.sinon.stub( deltaTransform, 'defaultTransform' ).throws( new Error() );
  882. expect( () => deltaTransform.transform( deltaA, deltaB, { isStrong: false } ) ).to.throw( Error );
  883. expect( error.calledWith( deltaA.toString() ) ).to.be.true;
  884. expect( error.calledWith( deltaB.toString() + ' (important)' ) ).to.be.true;
  885. } );
  886. } );
  887. } );
  888. function expectLog( expectedLogMsg ) {
  889. expect( log.calledWithExactly( expectedLogMsg ) ).to.be.true;
  890. log.resetHistory();
  891. }
  892. function wrapInDelta( op ) {
  893. const delta = new Delta();
  894. delta.addOperation( op );
  895. return op;
  896. }
  897. } );