enableenginedebug.js 24 KB

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