8
0

enableenginedebug.js 35 KB

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