enableenginedebug.js 37 KB

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