enableenginedebug.js 23 KB

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