8
0

enableenginedebug.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as enableEngineDebug, disableEngineDebug } from '../../src/dev-utils/enableenginedebug';
  6. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ModelPosition from '../../src/model/position';
  10. import ModelRange from '../../src/model/range';
  11. import ModelText from '../../src/model/text';
  12. import ModelTextProxy from '../../src/model/textproxy';
  13. import ModelElement from '../../src/model/element';
  14. import AttributeOperation from '../../src/model/operation/attributeoperation';
  15. import DetachOperation from '../../src/model/operation/detachoperation';
  16. import InsertOperation from '../../src/model/operation/insertoperation';
  17. import MarkerOperation from '../../src/model/operation/markeroperation';
  18. import MoveOperation from '../../src/model/operation/moveoperation';
  19. import NoOperation from '../../src/model/operation/nooperation';
  20. import RenameOperation from '../../src/model/operation/renameoperation';
  21. import RootAttributeOperation from '../../src/model/operation/rootattributeoperation';
  22. import Model from '../../src/model/model';
  23. import ModelDocumentFragment from '../../src/model/documentfragment';
  24. import ViewDocument from '../../src/view/document';
  25. import ViewAttributeElement from '../../src/view/attributeelement';
  26. import ViewContainerElement from '../../src/view/containerelement';
  27. import ViewText from '../../src/view/text';
  28. import ViewTextProxy from '../../src/view/textproxy';
  29. import ViewDocumentFragment from '../../src/view/documentfragment';
  30. import ViewElement from '../../src/view/element';
  31. import createViewRoot from '../view/_utils/createroot';
  32. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  33. describe( 'enableEngineDebug', () => {
  34. testUtils.createSinonSandbox();
  35. afterEach( () => {
  36. disableEngineDebug();
  37. } );
  38. it( 'should return plugin class', () => {
  39. const result = enableEngineDebug();
  40. expect( result.prototype ).to.be.instanceof( Plugin );
  41. } );
  42. it( 'should not throw when called multiple times', () => {
  43. enableEngineDebug();
  44. enableEngineDebug();
  45. const result = enableEngineDebug();
  46. expect( result.prototype ).to.be.instanceof( Plugin );
  47. } );
  48. } );
  49. describe( 'disableEngineDebug', () => {
  50. testUtils.createSinonSandbox();
  51. it( 'restores modified stubs', () => {
  52. expect( ModelPosition.prototype.log ).to.equal( undefined, 'Initial value (model/position)' );
  53. expect( ModelElement.prototype.printTree ).to.equal( undefined, 'Initial value (model/element)' );
  54. expect( ViewElement.prototype.printTree ).to.equal( undefined, 'Initial value (view/element)' );
  55. expect( Model.prototype.createReplayer ).to.equal( undefined, 'Initial value (model/document)' );
  56. expect( Editor.prototype.logDocuments ).to.equal( undefined, 'Initial value (core~editor/editor)' );
  57. enableEngineDebug();
  58. expect( ModelPosition.prototype.log ).to.be.a( 'function', 'After enabling engine debug (model/position)' );
  59. expect( ModelElement.prototype.printTree ).to.be.a( 'function', 'After enabling engine debug (model/element)' );
  60. expect( ViewElement.prototype.printTree ).to.be.a( 'function', 'After enabling engine debug (view/element)' );
  61. expect( Model.prototype.createReplayer ).to.be.a( 'function', 'After enabling engine debug (model/document)' );
  62. expect( Editor.prototype.logDocuments ).to.be.a( 'function', 'After enabling engine debug (core~editor/editor)' );
  63. disableEngineDebug();
  64. expect( ModelPosition.prototype.log ).to.equal( undefined, 'After disabling engine debug (model/position)' );
  65. expect( ModelElement.prototype.printTree ).to.equal( undefined, 'After disabling engine debug (model/element)' );
  66. expect( ViewElement.prototype.printTree ).to.equal( undefined, 'After disabling engine debug (view/element)' );
  67. expect( Model.prototype.createReplayer ).to.equal( undefined, 'After disabling engine debug (model/document)' );
  68. expect( Editor.prototype.logDocuments ).to.equal( undefined, 'After disabling engine debug (core~editor/editor)' );
  69. } );
  70. } );
  71. describe( 'debug tools', () => {
  72. let DebugPlugin, log, error;
  73. testUtils.createSinonSandbox();
  74. before( () => {
  75. log = sinon.spy();
  76. error = sinon.spy();
  77. DebugPlugin = enableEngineDebug( { log, error } );
  78. } );
  79. after( () => {
  80. disableEngineDebug();
  81. } );
  82. afterEach( () => {
  83. log.resetHistory();
  84. } );
  85. describe( 'should provide logging tools', () => {
  86. let model, modelDoc, modelRoot, modelElement, modelDocFrag;
  87. beforeEach( () => {
  88. model = new Model();
  89. modelDoc = model.document;
  90. modelRoot = modelDoc.createRoot();
  91. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  92. modelDocFrag = new ModelDocumentFragment( [ new ModelText( 'bar' ) ] );
  93. } );
  94. it( 'for ModelText', () => {
  95. const foo = new ModelText( 'foo', { foo: 'bar' } );
  96. expect( foo.toString() ).to.equal( '#foo' );
  97. foo.log();
  98. expect( log.calledWithExactly( 'ModelText: #foo' ) ).to.be.true;
  99. foo.logExtended();
  100. expect( log.calledWithExactly( 'ModelText: #foo, attrs: {"foo":"bar"}' ) ).to.be.true;
  101. } );
  102. it( 'for ModelTextProxy', () => {
  103. const foo = new ModelText( 'foo', { foo: 'bar' } );
  104. const proxy = new ModelTextProxy( foo, 1, 1 );
  105. expect( proxy.toString() ).to.equal( '#o' );
  106. proxy.log();
  107. expect( log.calledWithExactly( 'ModelTextProxy: #o' ) ).to.be.true;
  108. proxy.logExtended();
  109. expect( log.calledWithExactly( 'ModelTextProxy: #o, attrs: {"foo":"bar"}' ) ).to.be.true;
  110. } );
  111. it( 'for ModelElement', () => {
  112. const paragraph = new ModelElement( 'paragraph', { foo: 'bar' }, new ModelText( 'foo' ) );
  113. expect( paragraph.toString() ).to.equal( '<paragraph>' );
  114. paragraph.log();
  115. expectLog( 'ModelElement: <paragraph>' );
  116. paragraph.logExtended();
  117. expectLog( 'ModelElement: <paragraph>, 1 children, attrs: {"foo":"bar"}' );
  118. sinon.spy( paragraph, 'logExtended' );
  119. paragraph.logAll();
  120. expect( paragraph.logExtended.called ).to.be.true;
  121. expectLog( 'ModelText: #foo' );
  122. } );
  123. it( 'for ModelRootElement', () => {
  124. modelRoot.log();
  125. expectLog( 'ModelRootElement: main' );
  126. } );
  127. it( 'for ModelDocumentFragment', () => {
  128. modelDocFrag.log();
  129. expectLog( 'ModelDocumentFragment: documentFragment' );
  130. } );
  131. it( 'for ModelPosition', () => {
  132. const posInRoot = new ModelPosition( modelRoot, [ 0, 1, 0 ] );
  133. const posInElement = new ModelPosition( modelElement, [ 0 ] );
  134. const posInDocFrag = new ModelPosition( modelDocFrag, [ 2, 3 ] );
  135. expect( posInRoot.toString() ).to.equal( 'main [ 0, 1, 0 ]' );
  136. expect( posInElement.toString() ).to.equal( '<paragraph> [ 0 ]' );
  137. expect( posInDocFrag.toString() ).to.equal( 'documentFragment [ 2, 3 ]' );
  138. posInRoot.log();
  139. expectLog( 'ModelPosition: main [ 0, 1, 0 ]' );
  140. } );
  141. it( 'for ModelRange', () => {
  142. const rangeInRoot = ModelRange.createIn( modelRoot );
  143. const rangeInElement = ModelRange.createIn( modelElement );
  144. const rangeInDocFrag = ModelRange.createIn( modelDocFrag );
  145. expect( rangeInRoot.toString() ).to.equal( 'main [ 0 ] - [ 0 ]' );
  146. expect( rangeInElement.toString() ).to.equal( '<paragraph> [ 0 ] - [ 3 ]' );
  147. expect( rangeInDocFrag.toString() ).to.equal( 'documentFragment [ 0 ] - [ 3 ]' );
  148. rangeInRoot.log();
  149. expectLog( 'ModelRange: main [ 0 ] - [ 0 ]' );
  150. } );
  151. it( 'for ViewText', () => {
  152. const foo = new ViewText( 'foo' );
  153. expect( foo.toString() ).to.equal( '#foo' );
  154. foo.log();
  155. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  156. foo.logExtended();
  157. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  158. } );
  159. it( 'for ViewTextProxy', () => {
  160. const foo = new ViewText( 'foo', { foo: 'bar' } );
  161. const proxy = new ViewTextProxy( foo, 1, 1 );
  162. expect( proxy.toString() ).to.equal( '#o' );
  163. proxy.log();
  164. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  165. proxy.logExtended();
  166. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  167. } );
  168. describe( 'for operations', () => {
  169. beforeEach( () => {
  170. modelRoot._appendChild( [ new ModelText( 'foobar' ) ] );
  171. } );
  172. it( 'AttributeOperation', () => {
  173. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  174. expect( op.toString() ).to.equal( 'AttributeOperation( 0 ): "key": null -> {"foo":"bar"}, main [ 0 ] - [ 6 ]' );
  175. op.log();
  176. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  177. } );
  178. it( 'DetachOperation (text node)', () => {
  179. const op = new DetachOperation( ModelPosition.createAt( modelRoot, 0 ), 3 );
  180. expect( op.toString() ).to.equal( 'DetachOperation( null ): #foo -> main [ 0 ] - [ 3 ]' );
  181. op.log();
  182. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  183. } );
  184. it( 'DetachOperation (element)', () => {
  185. const element = new ModelElement( 'element' );
  186. modelRoot._insertChild( 0, element );
  187. const op = new DetachOperation( ModelPosition.createBefore( element ), 1 );
  188. expect( op.toString() ).to.equal( 'DetachOperation( null ): <element> -> main [ 0 ] - [ 1 ]' );
  189. op.log();
  190. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  191. } );
  192. it( 'DetachOperation (multiple nodes)', () => {
  193. const element = new ModelElement( 'element' );
  194. modelRoot._insertChild( 0, element );
  195. const op = new DetachOperation( ModelPosition.createBefore( element ), 2 );
  196. expect( op.toString() ).to.equal( 'DetachOperation( null ): [ 2 ] -> main [ 0 ] - [ 2 ]' );
  197. op.log();
  198. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  199. } );
  200. it( 'InsertOperation (text node)', () => {
  201. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  202. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): #abc -> main [ 3 ]' );
  203. op.log();
  204. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  205. } );
  206. it( 'InsertOperation (element)', () => {
  207. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelElement( 'paragraph' ) ], 0 );
  208. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): <paragraph> -> main [ 3 ]' );
  209. op.log();
  210. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  211. } );
  212. it( 'InsertOperation (multiple nodes)', () => {
  213. const nodes = [ new ModelText( 'x' ), new ModelElement( 'y' ), new ModelText( 'z' ) ];
  214. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), nodes, 0 );
  215. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): [ 3 ] -> main [ 3 ]' );
  216. op.log();
  217. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  218. } );
  219. it( 'MarkerOperation', () => {
  220. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, false, 0 );
  221. expect( op.toString() ).to.equal( 'MarkerOperation( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  222. op.log();
  223. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  224. } );
  225. it( 'MoveOperation', () => {
  226. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  227. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ]' );
  228. op.log();
  229. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  230. } );
  231. it( 'MoveOperation sticky', () => {
  232. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  233. op.isSticky = true;
  234. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ] (sticky)' );
  235. op.log();
  236. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  237. } );
  238. it( 'NoOperation', () => {
  239. const op = new NoOperation( 0 );
  240. expect( op.toString() ).to.equal( 'NoOperation( 0 )' );
  241. op.log();
  242. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  243. } );
  244. it( 'RenameOperation', () => {
  245. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  246. expect( op.toString() ).to.equal( 'RenameOperation( 0 ): main [ 1 ]: "old" -> "new"' );
  247. op.log();
  248. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  249. } );
  250. it( 'RootAttributeOperation', () => {
  251. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  252. expect( op.toString() ).to.equal( 'RootAttributeOperation( 0 ): "key": "old" -> null, main' );
  253. op.log();
  254. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  255. } );
  256. } );
  257. it( 'for applied operations', () => {
  258. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), [ new ModelText( 'foo' ) ], 0 );
  259. model.applyOperation( op );
  260. expect( log.calledWithExactly( 'Applying InsertOperation( 0 ): #foo -> main [ 0 ]' ) ).to.be.true;
  261. } );
  262. } );
  263. describe( 'should provide tree printing tools', () => {
  264. it( 'for model', () => {
  265. const model = new Model();
  266. const modelDoc = model.document;
  267. const modelRoot = modelDoc.createRoot();
  268. modelRoot._appendChild( [
  269. new ModelElement( 'paragraph', { foo: 'bar' }, [
  270. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  271. ] ),
  272. new ModelElement( 'listItem', { listType: 'numbered', listIndent: 0 }, new ModelText( 'One.' ) ),
  273. ] );
  274. const modelRootTree = modelRoot.printTree();
  275. expect( modelRootTree ).to.equal(
  276. '<main>' +
  277. '\n\t<paragraph foo="bar">' +
  278. '\n\t\tThis is ' +
  279. '\n\t\t<$text bold=true>bold</$text>' +
  280. '\n\t\t.' +
  281. '\n\t</paragraph>' +
  282. '\n\t<listItem listType="numbered" listIndent=0>' +
  283. '\n\t\tOne.' +
  284. '\n\t</listItem>' +
  285. '\n</main>'
  286. );
  287. modelRoot.logTree();
  288. expect( log.calledWithExactly( modelRootTree ) ).to.be.true;
  289. const modelParagraph = modelRoot.getChild( 0 );
  290. const modelParagraphTree = modelParagraph.printTree();
  291. expect( modelParagraphTree ).to.equal(
  292. '<paragraph foo="bar">' +
  293. '\n\tThis is ' +
  294. '\n\t<$text bold=true>bold</$text>' +
  295. '\n\t.' +
  296. '\n</paragraph>'
  297. );
  298. log.resetHistory();
  299. modelParagraph.logTree();
  300. expect( log.calledWithExactly( modelParagraphTree ) ).to.be.true;
  301. const modelDocFrag = new ModelDocumentFragment( [
  302. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' ),
  303. new ModelElement( 'paragraph', { foo: 'bar' }, [
  304. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  305. ] )
  306. ] );
  307. const modelDocFragTree = modelDocFrag.printTree();
  308. expect( modelDocFragTree ).to.equal(
  309. 'ModelDocumentFragment: [' +
  310. '\n\tThis is ' +
  311. '\n\t<$text bold=true>bold</$text>' +
  312. '\n\t.' +
  313. '\n\t<paragraph foo="bar">' +
  314. '\n\t\tThis is ' +
  315. '\n\t\t<$text bold=true>bold</$text>' +
  316. '\n\t\t.' +
  317. '\n\t</paragraph>' +
  318. '\n]'
  319. );
  320. log.resetHistory();
  321. modelDocFrag.logTree();
  322. expect( log.calledWithExactly( modelDocFragTree ) ).to.be.true;
  323. } );
  324. it( 'for view', () => {
  325. const viewDoc = new ViewDocument();
  326. const viewRoot = createViewRoot( viewDoc );
  327. viewRoot._appendChild( [
  328. new ViewContainerElement( 'p', { foo: 'bar' }, [
  329. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  330. ] ),
  331. new ViewContainerElement( 'ol', null, [
  332. new ViewContainerElement( 'li', null, new ViewText( 'One.' ) )
  333. ] )
  334. ] );
  335. const viewRootTree = viewRoot.printTree();
  336. expect( viewRootTree ).to.equal(
  337. '<div>' +
  338. '\n\t<p foo="bar">' +
  339. '\n\t\tThis is ' +
  340. '\n\t\t<b>' +
  341. '\n\t\t\tbold' +
  342. '\n\t\t</b>' +
  343. '\n\t\t.' +
  344. '\n\t</p>' +
  345. '\n\t<ol>' +
  346. '\n\t\t<li>' +
  347. '\n\t\t\tOne.' +
  348. '\n\t\t</li>' +
  349. '\n\t</ol>' +
  350. '\n</div>'
  351. );
  352. viewRoot.logTree();
  353. expect( log.calledWithExactly( viewRootTree ) ).to.be.true;
  354. const viewParagraph = viewRoot.getChild( 0 );
  355. const viewParagraphTree = viewParagraph.printTree();
  356. expect( viewParagraphTree ).to.equal(
  357. '<p foo="bar">' +
  358. '\n\tThis is ' +
  359. '\n\t<b>' +
  360. '\n\t\tbold' +
  361. '\n\t</b>' +
  362. '\n\t.' +
  363. '\n</p>'
  364. );
  365. log.resetHistory();
  366. viewParagraph.logTree();
  367. expect( log.calledWithExactly( viewParagraphTree ) ).to.be.true;
  368. const viewDocFrag = new ViewDocumentFragment( [
  369. new ViewText( 'Text.' ),
  370. new ViewContainerElement( 'p', { foo: 'bar' }, [
  371. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  372. ] )
  373. ] );
  374. const viewDocFragTree = viewDocFrag.printTree();
  375. expect( viewDocFragTree ).to.equal(
  376. 'ViewDocumentFragment: [' +
  377. '\n\tText.' +
  378. '\n\t<p foo="bar">' +
  379. '\n\t\tThis is ' +
  380. '\n\t\t<b>' +
  381. '\n\t\t\tbold' +
  382. '\n\t\t</b>' +
  383. '\n\t\t.' +
  384. '\n\t</p>' +
  385. '\n]'
  386. );
  387. log.resetHistory();
  388. viewDocFrag.logTree();
  389. expect( log.calledWithExactly( viewDocFragTree ) ).to.be.true;
  390. } );
  391. } );
  392. describe( 'should store model and view trees state', () => {
  393. let editor;
  394. beforeEach( () => {
  395. return VirtualTestEditor.create( {
  396. plugins: [ DebugPlugin ]
  397. } ).then( _editor => {
  398. editor = _editor;
  399. } );
  400. } );
  401. it( 'should store model and view state after each applied operation', () => {
  402. const model = editor.model;
  403. const modelDoc = model.document;
  404. const modelRoot = modelDoc.getRoot();
  405. const view = editor.editing.view;
  406. const viewDoc = view.document;
  407. model.change( () => {
  408. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), 0 );
  409. model.applyOperation( insert );
  410. const graveyard = modelDoc.graveyard;
  411. const move = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( graveyard, 0 ), 1 );
  412. model.applyOperation( move );
  413. } );
  414. log.resetHistory();
  415. modelDoc.log( 0 );
  416. expectLog(
  417. '<$graveyard></$graveyard>' +
  418. '\n<main></main>'
  419. );
  420. modelDoc.log( 1 );
  421. expectLog(
  422. '<$graveyard></$graveyard>' +
  423. '\n<main>' +
  424. '\n\tfoobar' +
  425. '\n</main>'
  426. );
  427. modelDoc.log( 2 );
  428. expectLog(
  429. '<$graveyard>' +
  430. '\n\too' +
  431. '\n</$graveyard>' +
  432. '\n<main>' +
  433. '\n\tfbar' +
  434. '\n</main>'
  435. );
  436. modelDoc.log();
  437. expectLog(
  438. '<$graveyard>' +
  439. '\n\too' +
  440. '\n</$graveyard>' +
  441. '\n<main>' +
  442. '\n\tfbar' +
  443. '\n</main>'
  444. );
  445. viewDoc.log( 0 );
  446. expectLog(
  447. '<$root></$root>'
  448. );
  449. viewDoc.log( 2 );
  450. expectLog(
  451. '<$root>' +
  452. '\n\tfbar' +
  453. '\n</$root>'
  454. );
  455. sinon.spy( modelDoc, 'log' );
  456. sinon.spy( viewDoc, 'log' );
  457. editor.logModel( 1 );
  458. expect( modelDoc.log.calledWithExactly( 1 ), 1 ).to.be.true;
  459. editor.logView( 2 );
  460. expect( viewDoc.log.calledWithExactly( 2 ), 2 ).to.be.true;
  461. modelDoc.log.resetHistory();
  462. viewDoc.log.resetHistory();
  463. editor.logModel();
  464. expect( modelDoc.log.calledWithExactly( 2 ), 3 ).to.be.true;
  465. modelDoc.log.resetHistory();
  466. viewDoc.log.resetHistory();
  467. editor.logDocuments();
  468. expect( modelDoc.log.calledWithExactly( 2 ), 4 ).to.be.true;
  469. expect( viewDoc.log.calledWithExactly( 2 ), 5 ).to.be.true;
  470. modelDoc.log.resetHistory();
  471. viewDoc.log.resetHistory();
  472. editor.logDocuments( 1 );
  473. expect( modelDoc.log.calledWithExactly( 1 ), 6 ).to.be.true;
  474. expect( viewDoc.log.calledWithExactly( 1 ), 7 ).to.be.true;
  475. } );
  476. it( 'should remove old states', () => {
  477. const model = editor.model;
  478. const modelDoc = model.document;
  479. const modelRoot = model.document.getRoot();
  480. for ( let i = 0; i < 25; i++ ) {
  481. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), modelDoc.version );
  482. model.applyOperation( insert );
  483. }
  484. modelDoc.log( 0 );
  485. expectLog( 'Tree log unavailable for given version: 0' );
  486. } );
  487. } );
  488. describe( 'should provide methods for operation replayer', () => {
  489. it( 'getAppliedOperations()', () => {
  490. const model = new Model();
  491. const modelDoc = model.document;
  492. expect( model.getAppliedOperations() ).to.equal( '' );
  493. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  494. const element = new ModelElement( 'paragraph' );
  495. otherRoot._appendChild( element );
  496. const insert = new InsertOperation( ModelPosition.createAt( element, 0 ), new ModelText( 'foo' ), 0 );
  497. model.applyOperation( insert );
  498. const stringifiedOperations = model.getAppliedOperations();
  499. expect( stringifiedOperations ).to.equal( JSON.stringify( insert ) );
  500. } );
  501. it( 'createReplayer()', () => {
  502. const model = new Model();
  503. const modelDoc = model.document;
  504. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  505. const element = new ModelElement( 'paragraph' );
  506. otherRoot._appendChild( element );
  507. const insert = new InsertOperation( ModelPosition.createAt( element, 0 ), new ModelText( 'foo' ), 0 );
  508. model.applyOperation( insert );
  509. const stringifiedOperations = model.getAppliedOperations();
  510. const operationReplayer = model.createReplayer( stringifiedOperations );
  511. expect( operationReplayer.getOperationsToReplay() ).to.deep.equal( [ JSON.parse( stringifiedOperations ) ] );
  512. } );
  513. } );
  514. function expectLog( expectedLogMsg ) {
  515. expect( log.calledWithExactly( expectedLogMsg ) ).to.be.true;
  516. log.resetHistory();
  517. }
  518. } );