8
0

enableenginedebug.js 23 KB

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