enableenginedebug.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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', () => {
  164. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  165. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): [ 1 ] -> main [ 3 ]' );
  166. op.log();
  167. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  168. } );
  169. it( 'MarkerOperation', () => {
  170. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  171. expect( op.toString() ).to.equal( 'MarkerOperation( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  172. op.log();
  173. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  174. } );
  175. it( 'MoveOperation', () => {
  176. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  177. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ]' );
  178. op.log();
  179. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  180. } );
  181. it( 'NoOperation', () => {
  182. const op = new NoOperation( 0 );
  183. expect( op.toString() ).to.equal( 'NoOperation( 0 )' );
  184. op.log();
  185. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  186. } );
  187. it( 'RenameOperation', () => {
  188. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  189. expect( op.toString() ).to.equal( 'RenameOperation( 0 ): main [ 1 ]: "old" -> "new"' );
  190. op.log();
  191. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  192. } );
  193. it( 'RootAttributeOperation', () => {
  194. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  195. expect( op.toString() ).to.equal( 'RootAttributeOperation( 0 ): "key": "old" -> null, main' );
  196. op.log();
  197. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  198. } );
  199. } );
  200. describe( 'for deltas', () => {
  201. it( 'Delta', () => {
  202. const delta = new Delta();
  203. const op = { log: sinon.spy() };
  204. delta.addOperation( op );
  205. sinon.spy( delta, 'log' );
  206. delta.logAll();
  207. expect( op.log.called ).to.be.true;
  208. expect( delta.log.called ).to.be.true;
  209. } );
  210. it( 'AttributeDelta', () => {
  211. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  212. const delta = new AttributeDelta();
  213. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  214. delta.addOperation( op );
  215. expect( delta.toString() ).to.equal( 'AttributeDelta( 0 ): "key": -> {"foo":"bar"}, main [ 0 ] - [ 6 ], 1 ops' );
  216. delta.log();
  217. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  218. } );
  219. it( 'InsertDelta', () => {
  220. const delta = new InsertDelta();
  221. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  222. delta.addOperation( op );
  223. expect( delta.toString() ).to.equal( 'InsertDelta( 0 ): [ 1 ] -> main [ 3 ]' );
  224. delta.log();
  225. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  226. } );
  227. it( 'MarkerDelta', () => {
  228. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  229. const delta = new MarkerDelta();
  230. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  231. delta.addOperation( op );
  232. expect( delta.toString() ).to.equal( 'MarkerDelta( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  233. delta.log();
  234. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  235. } );
  236. it( 'MergeDelta', () => {
  237. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  238. const firstEle = new ModelElement( 'paragraph' );
  239. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  240. otherRoot.appendChildren( [ firstEle, removedEle ] );
  241. const graveyard = modelDoc.graveyard;
  242. const delta = new MergeDelta();
  243. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  244. const remove = new RemoveOperation(
  245. ModelPosition.createBefore( removedEle ),
  246. 1,
  247. ModelPosition.createAt( graveyard, 0 ),
  248. 1
  249. );
  250. delta.addOperation( move );
  251. delta.addOperation( remove );
  252. expect( delta.toString() ).to.equal( 'MergeDelta( 0 ): otherRoot [ 1 ]' );
  253. delta.log();
  254. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  255. } );
  256. it( 'MoveDelta', () => {
  257. const delta = new MoveDelta();
  258. const move1 = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 1, ModelPosition.createAt( modelRoot, 3 ), 0 );
  259. const move2 = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 1, ModelPosition.createAt( modelRoot, 6 ), 0 );
  260. delta.addOperation( move1 );
  261. delta.addOperation( move2 );
  262. expect( delta.toString() ).to.equal( 'MoveDelta( 0 ): main [ 0 ] - [ 1 ] -> main [ 3 ]; main [ 1 ] - [ 2 ] -> main [ 6 ]' );
  263. delta.log();
  264. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  265. } );
  266. it( 'RenameDelta', () => {
  267. const delta = new RenameDelta();
  268. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  269. delta.addOperation( op );
  270. expect( delta.toString() ).to.equal( 'RenameDelta( 0 ): main [ 1 ]: "old" -> "new"' );
  271. delta.log();
  272. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  273. } );
  274. it( 'RootAttributeDelta', () => {
  275. const delta = new RootAttributeDelta();
  276. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  277. delta.addOperation( op );
  278. expect( delta.toString() ).to.equal( 'RootAttributeDelta( 0 ): "key": "old" -> null, main' );
  279. delta.log();
  280. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  281. } );
  282. it( 'SplitDelta', () => {
  283. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  284. const splitEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  285. otherRoot.appendChildren( [ splitEle ] );
  286. const delta = new SplitDelta();
  287. const insert = new InsertOperation( ModelPosition.createAt( otherRoot, 1 ), [ new ModelElement( 'paragraph' ) ], 0 );
  288. const move = new MoveOperation( ModelPosition.createAt( splitEle, 1 ), 2, new ModelPosition( otherRoot, [ 1, 0 ] ), 1 );
  289. delta.addOperation( insert );
  290. delta.addOperation( move );
  291. expect( delta.toString() ).to.equal( 'SplitDelta( 0 ): otherRoot [ 0, 1 ]' );
  292. delta.log();
  293. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  294. } );
  295. it( 'UnwrapDelta', () => {
  296. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  297. const unwrapEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  298. otherRoot.appendChildren( [ unwrapEle ] );
  299. const graveyard = modelDoc.graveyard;
  300. const delta = new UnwrapDelta();
  301. const move = new MoveOperation( ModelPosition.createAt( unwrapEle, 0 ), 3, ModelPosition.createAt( otherRoot, 0 ), 0 );
  302. const remove = new RemoveOperation( ModelPosition.createAt( otherRoot, 3 ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  303. delta.addOperation( move );
  304. delta.addOperation( remove );
  305. expect( delta.toString() ).to.equal( 'UnwrapDelta( 0 ): otherRoot [ 0 ]' );
  306. delta.log();
  307. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  308. } );
  309. it( 'WrapDelta', () => {
  310. const delta = new WrapDelta();
  311. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 6 ), new ModelElement( 'paragraph' ), 0 );
  312. const move = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 6, new ModelPosition( modelRoot, [ 1, 0 ] ), 1 );
  313. delta.addOperation( insert );
  314. delta.addOperation( move );
  315. expect( delta.toString() ).to.equal( 'WrapDelta( 0 ): main [ 0 ] - [ 6 ] -> <paragraph>' );
  316. delta.log();
  317. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  318. } );
  319. } );
  320. it( 'for applied operations', () => {
  321. const delta = new InsertDelta();
  322. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), [ new ModelText( 'foo' ) ], 0 );
  323. delta.addOperation( op );
  324. modelDoc.applyOperation( op );
  325. expect( log.calledWithExactly( 'Applying InsertOperation( 0 ): [ 1 ] -> main [ 0 ]' ) ).to.be.true;
  326. } );
  327. } );
  328. describe( 'should provide tree printing tools', () => {
  329. it( 'for model', () => {
  330. const modelDoc = new ModelDocument();
  331. const modelRoot = modelDoc.createRoot();
  332. modelRoot.appendChildren( [
  333. new ModelElement( 'paragraph', { foo: 'bar' }, [
  334. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  335. ] ),
  336. new ModelElement( 'listItem', { type: 'numbered', indent: 0 }, new ModelText( 'One.' ) ),
  337. ] );
  338. const modelRootTree = modelRoot.printTree();
  339. expect( modelRootTree ).to.equal(
  340. '<main>' +
  341. '\n\t<paragraph foo="bar">' +
  342. '\n\t\tThis is ' +
  343. '\n\t\t<$text bold=true>bold</$text>' +
  344. '\n\t\t.' +
  345. '\n\t</paragraph>' +
  346. '\n\t<listItem type="numbered" indent=0>' +
  347. '\n\t\tOne.' +
  348. '\n\t</listItem>' +
  349. '\n</main>'
  350. );
  351. modelRoot.logTree();
  352. expect( log.calledWithExactly( modelRootTree ) ).to.be.true;
  353. const modelParagraph = modelRoot.getChild( 0 );
  354. const modelParagraphTree = modelParagraph.printTree();
  355. expect( modelParagraphTree ).to.equal(
  356. '<paragraph foo="bar">' +
  357. '\n\tThis is ' +
  358. '\n\t<$text bold=true>bold</$text>' +
  359. '\n\t.' +
  360. '\n</paragraph>'
  361. );
  362. log.reset();
  363. modelParagraph.logTree();
  364. expect( log.calledWithExactly( modelParagraphTree ) ).to.be.true;
  365. const modelDocFrag = new ModelDocumentFragment( [
  366. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' ),
  367. new ModelElement( 'paragraph', { foo: 'bar' }, [
  368. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  369. ] )
  370. ] );
  371. const modelDocFragTree = modelDocFrag.printTree();
  372. expect( modelDocFragTree ).to.equal(
  373. 'ModelDocumentFragment: [' +
  374. '\n\tThis is ' +
  375. '\n\t<$text bold=true>bold</$text>' +
  376. '\n\t.' +
  377. '\n\t<paragraph foo="bar">' +
  378. '\n\t\tThis is ' +
  379. '\n\t\t<$text bold=true>bold</$text>' +
  380. '\n\t\t.' +
  381. '\n\t</paragraph>' +
  382. '\n]'
  383. );
  384. log.reset();
  385. modelDocFrag.logTree();
  386. expect( log.calledWithExactly( modelDocFragTree ) ).to.be.true;
  387. } );
  388. it( 'for view', () => {
  389. const viewDoc = new ViewDocument();
  390. const viewRoot = viewDoc.createRoot( 'div' );
  391. viewRoot.appendChildren( [
  392. new ViewContainerElement( 'p', { foo: 'bar' }, [
  393. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  394. ] ),
  395. new ViewContainerElement( 'ol', null, [
  396. new ViewContainerElement( 'li', null, new ViewText( 'One.' ) )
  397. ] )
  398. ] );
  399. const viewRootTree = viewRoot.printTree();
  400. expect( viewRootTree ).to.equal(
  401. '<div>' +
  402. '\n\t<p foo="bar">' +
  403. '\n\t\tThis is ' +
  404. '\n\t\t<b>' +
  405. '\n\t\t\tbold' +
  406. '\n\t\t</b>' +
  407. '\n\t\t.' +
  408. '\n\t</p>' +
  409. '\n\t<ol>' +
  410. '\n\t\t<li>' +
  411. '\n\t\t\tOne.' +
  412. '\n\t\t</li>' +
  413. '\n\t</ol>' +
  414. '\n</div>'
  415. );
  416. viewRoot.logTree();
  417. expect( log.calledWithExactly( viewRootTree ) ).to.be.true;
  418. const viewParagraph = viewRoot.getChild( 0 );
  419. const viewParagraphTree = viewParagraph.printTree();
  420. expect( viewParagraphTree ).to.equal(
  421. '<p foo="bar">' +
  422. '\n\tThis is ' +
  423. '\n\t<b>' +
  424. '\n\t\tbold' +
  425. '\n\t</b>' +
  426. '\n\t.' +
  427. '\n</p>'
  428. );
  429. log.reset();
  430. viewParagraph.logTree();
  431. expect( log.calledWithExactly( viewParagraphTree ) ).to.be.true;
  432. const viewDocFrag = new ViewDocumentFragment( [
  433. new ViewText( 'Text.' ),
  434. new ViewContainerElement( 'p', { foo: 'bar' }, [
  435. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  436. ] )
  437. ] );
  438. const viewDocFragTree = viewDocFrag.printTree();
  439. expect( viewDocFragTree ).to.equal(
  440. 'ViewDocumentFragment: [' +
  441. '\n\tText.' +
  442. '\n\t<p foo="bar">' +
  443. '\n\t\tThis is ' +
  444. '\n\t\t<b>' +
  445. '\n\t\t\tbold' +
  446. '\n\t\t</b>' +
  447. '\n\t\t.' +
  448. '\n\t</p>' +
  449. '\n]'
  450. );
  451. log.reset();
  452. viewDocFrag.logTree();
  453. expect( log.calledWithExactly( viewDocFragTree ) ).to.be.true;
  454. } );
  455. } );
  456. describe( 'should store model and view trees state', () => {
  457. let editor;
  458. beforeEach( () => {
  459. const div = document.createElement( 'div' );
  460. return TestEditor.create( div, {
  461. plugins: [ DebugPlugin ]
  462. } ).then( _editor => {
  463. editor = _editor;
  464. } );
  465. } );
  466. it( 'should store model and view state after each applied operation', () => {
  467. const model = editor.document;
  468. const modelRoot = model.getRoot();
  469. const view = editor.editing.view;
  470. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), 0 );
  471. model.applyOperation( wrapInDelta( insert ) );
  472. const graveyard = model.graveyard;
  473. const remove = new RemoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( graveyard, 0 ), 1 );
  474. model.applyOperation( wrapInDelta( remove ) );
  475. log.reset();
  476. model.log( 0 );
  477. expectLog(
  478. '<$graveyard></$graveyard>' +
  479. '\n<main></main>'
  480. );
  481. model.log( 1 );
  482. expectLog(
  483. '<$graveyard></$graveyard>' +
  484. '\n<main>' +
  485. '\n\tfoobar' +
  486. '\n</main>'
  487. );
  488. model.log( 2 );
  489. expectLog(
  490. '<$graveyard>' +
  491. '\n\too' +
  492. '\n</$graveyard>' +
  493. '\n<main>' +
  494. '\n\tfbar' +
  495. '\n</main>'
  496. );
  497. model.log();
  498. expectLog(
  499. '<$graveyard>' +
  500. '\n\too' +
  501. '\n</$graveyard>' +
  502. '\n<main>' +
  503. '\n\tfbar' +
  504. '\n</main>'
  505. );
  506. view.log( 0 );
  507. expectLog(
  508. '<div></div>'
  509. );
  510. view.log( 1 );
  511. expectLog(
  512. '<div>' +
  513. '\n\tfoobar' +
  514. '\n</div>'
  515. );
  516. view.log( 2 );
  517. expectLog(
  518. '<div>' +
  519. '\n\tfbar' +
  520. '\n</div>'
  521. );
  522. sinon.spy( model, 'log' );
  523. sinon.spy( view, 'log' );
  524. editor.logModel( 1 );
  525. expect( model.log.calledWithExactly( 1 ) ).to.be.true;
  526. editor.logView( 2 );
  527. expect( view.log.calledWithExactly( 2 ) ).to.be.true;
  528. model.log.reset();
  529. view.log.reset();
  530. editor.logModel();
  531. expect( model.log.calledWithExactly( 2 ) ).to.be.true;
  532. model.log.reset();
  533. view.log.reset();
  534. editor.logDocuments();
  535. expect( model.log.calledWithExactly( 2 ) ).to.be.true;
  536. expect( view.log.calledWithExactly( 2 ) ).to.be.true;
  537. model.log.reset();
  538. view.log.reset();
  539. editor.logDocuments( 1 );
  540. expect( model.log.calledWithExactly( 1 ) ).to.be.true;
  541. expect( view.log.calledWithExactly( 1 ) ).to.be.true;
  542. } );
  543. it( 'should remove old states', () => {
  544. const model = editor.document;
  545. const modelRoot = model.getRoot();
  546. for ( let i = 0; i < 25; i++ ) {
  547. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), model.version );
  548. model.applyOperation( wrapInDelta( insert ) );
  549. }
  550. model.log( 0 );
  551. expectLog( 'Tree log unavailable for given version: 0' );
  552. } );
  553. } );
  554. describe( 'should provide methods for delta replayer', () => {
  555. it( 'getAppliedDeltas()', () => {
  556. const modelDoc = new ModelDocument();
  557. expect( modelDoc.getAppliedDeltas() ).to.equal( '' );
  558. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  559. const firstEle = new ModelElement( 'paragraph' );
  560. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  561. otherRoot.appendChildren( [ firstEle, removedEle ] );
  562. const delta = new MergeDelta();
  563. const graveyard = modelDoc.graveyard;
  564. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  565. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  566. delta.addOperation( move );
  567. delta.addOperation( remove );
  568. modelDoc.applyOperation( move );
  569. modelDoc.applyOperation( remove );
  570. const stringifiedDeltas = modelDoc.getAppliedDeltas();
  571. expect( stringifiedDeltas ).to.equal( JSON.stringify( delta.toJSON() ) );
  572. } );
  573. it( 'createReplayer()', () => {
  574. const modelDoc = new ModelDocument();
  575. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  576. const firstEle = new ModelElement( 'paragraph' );
  577. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  578. otherRoot.appendChildren( [ firstEle, removedEle ] );
  579. const delta = new MergeDelta();
  580. const graveyard = modelDoc.graveyard;
  581. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  582. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, ModelPosition.createAt( graveyard, 0 ), 1 );
  583. delta.addOperation( move );
  584. delta.addOperation( remove );
  585. const stringifiedDeltas = JSON.stringify( delta.toJSON() );
  586. const deltaReplayer = modelDoc.createReplayer( stringifiedDeltas );
  587. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ JSON.parse( stringifiedDeltas ) ] );
  588. } );
  589. } );
  590. describe( 'should provide debug tools for delta transformation', () => {
  591. let document, root, otherRoot;
  592. beforeEach( () => {
  593. document = new ModelDocument();
  594. root = document.createRoot();
  595. otherRoot = document.createRoot( 'other', 'other' );
  596. } );
  597. it( 'Delta#_saveHistory()', () => {
  598. const insertDeltaA = new InsertDelta();
  599. const insertOpA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  600. insertDeltaA.addOperation( insertOpA );
  601. const insertDeltaB = new InsertDelta();
  602. const insertOpB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  603. insertDeltaB.addOperation( insertOpB );
  604. const insertDeltaFinalA = new InsertDelta();
  605. const insertOpFinalA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  606. insertDeltaFinalA.addOperation( insertOpFinalA );
  607. const insertDeltaFinalAJsonWithoutHistory = JSON.stringify( insertDeltaFinalA );
  608. insertDeltaFinalA._saveHistory( {
  609. before: insertDeltaA,
  610. transformedBy: insertDeltaB,
  611. wasImportant: true,
  612. resultIndex: 0,
  613. resultsTotal: 1
  614. } );
  615. const insertDeltaC = new InsertDelta();
  616. const insertOpC = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  617. insertDeltaC.addOperation( insertOpC );
  618. const insertDeltaFinalB = new InsertDelta();
  619. const insertOpFinalB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 2 );
  620. insertDeltaFinalB.addOperation( insertOpFinalB );
  621. insertDeltaFinalB._saveHistory( {
  622. before: insertDeltaFinalA,
  623. transformedBy: insertDeltaC,
  624. wasImportant: false,
  625. resultIndex: 1,
  626. resultsTotal: 3
  627. } );
  628. expect( insertDeltaA.history ).to.be.undefined;
  629. expect( insertDeltaB.history ).to.be.undefined;
  630. expect( insertDeltaFinalA.history ).not.to.be.undefined;
  631. expect( insertDeltaFinalA.history.length ).to.equal( 1 );
  632. expect( insertDeltaFinalA.history[ 0 ] ).to.deep.equal( {
  633. before: JSON.stringify( insertDeltaA ),
  634. transformedBy: JSON.stringify( insertDeltaB ),
  635. wasImportant: true,
  636. resultIndex: 0,
  637. resultsTotal: 1
  638. } );
  639. expect( insertDeltaFinalB.history ).not.to.be.undefined;
  640. expect( insertDeltaFinalB.history.length ).to.equal( 2 );
  641. expect( insertDeltaFinalB.history[ 0 ] ).to.deep.equal( {
  642. before: JSON.stringify( insertDeltaA ),
  643. transformedBy: JSON.stringify( insertDeltaB ),
  644. wasImportant: true,
  645. resultIndex: 0,
  646. resultsTotal: 1
  647. } );
  648. expect( insertDeltaFinalB.history[ 1 ] ).to.deep.equal( {
  649. before: insertDeltaFinalAJsonWithoutHistory,
  650. transformedBy: JSON.stringify( insertDeltaC ),
  651. wasImportant: false,
  652. resultIndex: 1,
  653. resultsTotal: 3
  654. } );
  655. } );
  656. it( 'save delta history on deltaTransform.transform', () => {
  657. const deltaA = new MoveDelta();
  658. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  659. deltaA.addOperation( opA );
  660. const deltaB = new InsertDelta();
  661. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  662. deltaB.addOperation( opB );
  663. const deltaC = new MoveDelta();
  664. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  665. deltaC.addOperation( opC );
  666. let result = deltaTransform.transform( deltaA, deltaB, false );
  667. expect( result[ 0 ].history ).not.to.be.undefined;
  668. expect( result[ 0 ].history.length ).to.equal( 1 );
  669. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  670. before: JSON.stringify( deltaA ),
  671. transformedBy: JSON.stringify( deltaB ),
  672. wasImportant: false,
  673. resultIndex: 0,
  674. resultsTotal: 1
  675. } );
  676. const firstResultWithoutHistory = result[ 0 ].clone();
  677. delete firstResultWithoutHistory.history;
  678. result = deltaTransform.transform( result[ 0 ], deltaC, true );
  679. expect( result[ 0 ].history ).not.to.be.undefined;
  680. expect( result[ 0 ].history.length ).to.equal( 2 );
  681. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  682. before: JSON.stringify( deltaA ),
  683. transformedBy: JSON.stringify( deltaB ),
  684. wasImportant: false,
  685. resultIndex: 0,
  686. resultsTotal: 1
  687. } );
  688. expect( result[ 0 ].history[ 1 ] ).to.deep.equal( {
  689. before: JSON.stringify( firstResultWithoutHistory ),
  690. transformedBy: JSON.stringify( deltaC ),
  691. wasImportant: true,
  692. resultIndex: 0,
  693. resultsTotal: 2
  694. } );
  695. } );
  696. it( 'recreate delta using Delta#history', () => {
  697. const deltaA = new MoveDelta();
  698. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  699. deltaA.addOperation( opA );
  700. const deltaB = new InsertDelta();
  701. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  702. deltaB.addOperation( opB );
  703. const deltaC = new MoveDelta();
  704. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  705. deltaC.addOperation( opC );
  706. let original = deltaTransform.transform( deltaA, deltaB, false );
  707. original = deltaTransform.transform( original[ 0 ], deltaC, true )[ 0 ];
  708. const history = original.history;
  709. let recreated = deltaTransform.transform(
  710. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].before ), document ),
  711. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].transformedBy ), document ),
  712. history[ 0 ].wasImportant
  713. );
  714. recreated = recreated[ history[ 0 ].resultIndex ];
  715. recreated = deltaTransform.transform(
  716. recreated,
  717. DeltaFactory.fromJSON( JSON.parse( history[ 1 ].transformedBy ), document ),
  718. history[ 1 ].wasImportant
  719. );
  720. recreated = recreated[ history[ 1 ].resultIndex ];
  721. delete original.history;
  722. delete recreated.history;
  723. expect( JSON.stringify( recreated ) ).to.equal( JSON.stringify( original ) );
  724. } );
  725. describe( 'provide additional logging when transformation crashes', () => {
  726. it( 'with more important delta A', () => {
  727. const deltaA = new MoveDelta();
  728. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  729. deltaA.addOperation( opA );
  730. const deltaB = new InsertDelta();
  731. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  732. deltaB.addOperation( opB );
  733. deltaTransform.defaultTransform = () => {
  734. throw new Error();
  735. };
  736. expect( () => deltaTransform.transform( deltaA, deltaB, true ) ).to.throw( Error );
  737. expect( error.calledWith( deltaA.toString() + ' (important)' ) ).to.be.true;
  738. expect( error.calledWith( deltaB.toString() ) ).to.be.true;
  739. } );
  740. it( 'with more important delta B', () => {
  741. const deltaA = new MoveDelta();
  742. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  743. deltaA.addOperation( opA );
  744. const deltaB = new InsertDelta();
  745. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  746. deltaB.addOperation( opB );
  747. deltaTransform.defaultTransform = () => {
  748. throw new Error();
  749. };
  750. expect( () => deltaTransform.transform( deltaA, deltaB, false ) ).to.throw( Error );
  751. expect( error.calledWith( deltaA.toString() ) ).to.be.true;
  752. expect( error.calledWith( deltaB.toString() + ' (important)' ) ).to.be.true;
  753. } );
  754. } );
  755. } );
  756. function expectLog( expectedLogMsg ) {
  757. expect( log.calledWithExactly( expectedLogMsg ) ).to.be.true;
  758. log.reset();
  759. }
  760. function wrapInDelta( op ) {
  761. const delta = new Delta();
  762. delta.addOperation( op );
  763. return op;
  764. }
  765. } );