8
0

enableenginedebug.js 24 KB

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