8
0

enableenginedebug.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 DeltaFactory from '../../src/model/delta/deltafactory';
  22. import Delta from '../../src/model/delta/delta';
  23. import { default as AttributeDelta, 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 deltaTransform from '../../src/model/delta/transform';
  33. import ModelDocument from '../../src/model/document';
  34. import ModelDocumentFragment from '../../src/model/documentfragment';
  35. import ViewDocument from '../../src/view/document';
  36. import ViewAttributeElement from '../../src/view/attributeelement';
  37. import ViewContainerElement from '../../src/view/containerelement';
  38. import ViewText from '../../src/view/text';
  39. import ViewTextProxy from '../../src/view/textproxy';
  40. import ViewDocumentFragment from '../../src/view/documentfragment';
  41. /* global document */
  42. describe( 'enableEngineDebug', () => {
  43. it( 'should return plugin class', () => {
  44. const result = enableEngineDebug();
  45. expect( result.prototype ).to.be.instanceof( Plugin );
  46. } );
  47. it( 'should not throw when called multiple times', () => {
  48. enableEngineDebug();
  49. enableEngineDebug();
  50. const result = enableEngineDebug();
  51. expect( result.prototype ).to.be.instanceof( Plugin );
  52. } );
  53. } );
  54. describe( 'debug tools', () => {
  55. let DebugPlugin, log;
  56. class TestEditor extends StandardEditor {
  57. constructor( ...args ) {
  58. super( ...args );
  59. this.document.createRoot( 'main' );
  60. this.editing.createRoot( this.element, 'main' );
  61. }
  62. }
  63. before( () => {
  64. log = sinon.spy();
  65. DebugPlugin = enableEngineDebug( log );
  66. } );
  67. afterEach( () => {
  68. log.reset();
  69. } );
  70. describe( 'should provide logging tools', () => {
  71. let modelDoc, modelRoot, modelElement, modelDocFrag;
  72. beforeEach( () => {
  73. modelDoc = new ModelDocument();
  74. modelRoot = modelDoc.createRoot();
  75. modelElement = new ModelElement( 'paragraph', null, new ModelText( 'foo' ) );
  76. modelDocFrag = new ModelDocumentFragment( [ new ModelText( 'bar' ) ] );
  77. } );
  78. it( 'for ModelText', () => {
  79. const foo = new ModelText( 'foo', { foo: 'bar' } );
  80. expect( foo.toString() ).to.equal( '#foo' );
  81. foo.log();
  82. expect( log.calledWithExactly( 'ModelText: #foo' ) ).to.be.true;
  83. foo.logExtended();
  84. expect( log.calledWithExactly( 'ModelText: #foo, attrs: {"foo":"bar"}' ) ).to.be.true;
  85. } );
  86. it( 'for ModelTextProxy', () => {
  87. const foo = new ModelText( 'foo', { foo: 'bar' } );
  88. const proxy = new ModelTextProxy( foo, 1, 1 );
  89. expect( proxy.toString() ).to.equal( '#o' );
  90. proxy.log();
  91. expect( log.calledWithExactly( 'ModelTextProxy: #o' ) ).to.be.true;
  92. proxy.logExtended();
  93. expect( log.calledWithExactly( 'ModelTextProxy: #o, attrs: {"foo":"bar"}' ) ).to.be.true;
  94. } );
  95. it( 'for ModelElement', () => {
  96. const paragraph = new ModelElement( 'paragraph', { foo: 'bar' }, new ModelText( 'foo' ) );
  97. expect( paragraph.toString() ).to.equal( '<paragraph>' );
  98. paragraph.log();
  99. expectLog( 'ModelElement: <paragraph>' );
  100. paragraph.logExtended();
  101. expectLog( 'ModelElement: <paragraph>, 1 children, attrs: {"foo":"bar"}' );
  102. sinon.spy( paragraph, 'logExtended' );
  103. paragraph.logAll();
  104. expect( paragraph.logExtended.called ).to.be.true;
  105. expectLog( 'ModelText: #foo' );
  106. } );
  107. it( 'for ModelRootElement', () => {
  108. modelRoot.log();
  109. expectLog( 'ModelRootElement: main' );
  110. } );
  111. it( 'for ModelDocumentFragment', () => {
  112. modelDocFrag.log();
  113. expectLog( 'ModelDocumentFragment: documentFragment' );
  114. } );
  115. it( 'for ModelPosition', () => {
  116. const posInRoot = new ModelPosition( modelRoot, [ 0, 1, 0 ] );
  117. const posInElement = new ModelPosition( modelElement, [ 0 ] );
  118. const posInDocFrag = new ModelPosition( modelDocFrag, [ 2, 3 ] );
  119. expect( posInRoot.toString() ).to.equal( 'main [ 0, 1, 0 ]' );
  120. expect( posInElement.toString() ).to.equal( '<paragraph> [ 0 ]' );
  121. expect( posInDocFrag.toString() ).to.equal( 'documentFragment [ 2, 3 ]' );
  122. posInRoot.log();
  123. expectLog( 'ModelPosition: main [ 0, 1, 0 ]' );
  124. } );
  125. it( 'for ModelRange', () => {
  126. const rangeInRoot = ModelRange.createIn( modelRoot );
  127. const rangeInElement = ModelRange.createIn( modelElement );
  128. const rangeInDocFrag = ModelRange.createIn( modelDocFrag );
  129. expect( rangeInRoot.toString() ).to.equal( 'main [ 0 ] - [ 0 ]' );
  130. expect( rangeInElement.toString() ).to.equal( '<paragraph> [ 0 ] - [ 3 ]' );
  131. expect( rangeInDocFrag.toString() ).to.equal( 'documentFragment [ 0 ] - [ 3 ]' );
  132. rangeInRoot.log();
  133. expectLog( 'ModelRange: main [ 0 ] - [ 0 ]' );
  134. } );
  135. it( 'for ViewText', () => {
  136. const foo = new ViewText( 'foo' );
  137. expect( foo.toString() ).to.equal( '#foo' );
  138. foo.log();
  139. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  140. foo.logExtended();
  141. expect( log.calledWithExactly( 'ViewText: #foo' ) ).to.be.true;
  142. } );
  143. it( 'for ViewTextProxy', () => {
  144. const foo = new ViewText( 'foo', { foo: 'bar' } );
  145. const proxy = new ViewTextProxy( foo, 1, 1 );
  146. expect( proxy.toString() ).to.equal( '#o' );
  147. proxy.log();
  148. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  149. proxy.logExtended();
  150. expect( log.calledWithExactly( 'ViewTextProxy: #o' ) ).to.be.true;
  151. } );
  152. describe( 'for operations', () => {
  153. beforeEach( () => {
  154. modelRoot.appendChildren( [ new ModelText( 'foobar' ) ] );
  155. } );
  156. it( 'AttributeOperation', () => {
  157. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  158. expect( op.toString() ).to.equal( 'AttributeOperation( 0 ): "key": null -> {"foo":"bar"}, main [ 0 ] - [ 6 ]' );
  159. op.log();
  160. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  161. } );
  162. it( 'InsertOperation', () => {
  163. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  164. expect( op.toString() ).to.equal( 'InsertOperation( 0 ): [ 1 ] -> main [ 3 ]' );
  165. op.log();
  166. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  167. } );
  168. it( 'MarkerOperation', () => {
  169. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  170. expect( op.toString() ).to.equal( 'MarkerOperation( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  171. op.log();
  172. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  173. } );
  174. it( 'MoveOperation', () => {
  175. const op = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, ModelPosition.createAt( modelRoot, 6 ), 0 );
  176. expect( op.toString() ).to.equal( 'MoveOperation( 0 ): main [ 1 ] - [ 3 ] -> main [ 6 ]' );
  177. op.log();
  178. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  179. } );
  180. it( 'NoOperation', () => {
  181. const op = new NoOperation( 0 );
  182. expect( op.toString() ).to.equal( 'NoOperation( 0 )' );
  183. op.log();
  184. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  185. } );
  186. it( 'RenameOperation', () => {
  187. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  188. expect( op.toString() ).to.equal( 'RenameOperation( 0 ): main [ 1 ]: "old" -> "new"' );
  189. op.log();
  190. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  191. } );
  192. it( 'RootAttributeOperation', () => {
  193. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  194. expect( op.toString() ).to.equal( 'RootAttributeOperation( 0 ): "key": "old" -> null, main' );
  195. op.log();
  196. expect( log.calledWithExactly( op.toString() ) ).to.be.true;
  197. } );
  198. } );
  199. describe( 'for deltas', () => {
  200. it( 'Delta', () => {
  201. const delta = new Delta();
  202. const op = { log: sinon.spy() };
  203. delta.addOperation( op );
  204. sinon.spy( delta, 'log' );
  205. delta.logAll();
  206. expect( op.log.called ).to.be.true;
  207. expect( delta.log.called ).to.be.true;
  208. } );
  209. it( 'AttributeDelta', () => {
  210. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  211. const delta = new AttributeDelta();
  212. const op = new AttributeOperation( ModelRange.createIn( modelRoot ), 'key', null, { foo: 'bar' }, 0 );
  213. delta.addOperation( op );
  214. expect( delta.toString() ).to.equal( 'AttributeDelta( 0 ): "key": -> {"foo":"bar"}, main [ 0 ] - [ 6 ], 1 ops' );
  215. delta.log();
  216. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  217. } );
  218. it( 'InsertDelta', () => {
  219. const delta = new InsertDelta();
  220. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 3 ), [ new ModelText( 'abc' ) ], 0 );
  221. delta.addOperation( op );
  222. expect( delta.toString() ).to.equal( 'InsertDelta( 0 ): [ 1 ] -> main [ 3 ]' );
  223. delta.log();
  224. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  225. } );
  226. it( 'MarkerDelta', () => {
  227. modelRoot.appendChildren( new ModelText( 'foobar' ) );
  228. const delta = new MarkerDelta();
  229. const op = new MarkerOperation( 'marker', null, ModelRange.createIn( modelRoot ), modelDoc.markers, 0 );
  230. delta.addOperation( op );
  231. expect( delta.toString() ).to.equal( 'MarkerDelta( 0 ): "marker": null -> main [ 0 ] - [ 6 ]' );
  232. delta.log();
  233. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  234. } );
  235. it( 'MergeDelta', () => {
  236. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  237. const firstEle = new ModelElement( 'paragraph' );
  238. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  239. otherRoot.appendChildren( [ firstEle, removedEle ] );
  240. const delta = new MergeDelta();
  241. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  242. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, 1 );
  243. delta.addOperation( move );
  244. delta.addOperation( remove );
  245. expect( delta.toString() ).to.equal( 'MergeDelta( 0 ): otherRoot [ 1 ]' );
  246. delta.log();
  247. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  248. } );
  249. it( 'MoveDelta', () => {
  250. const delta = new MoveDelta();
  251. const move1 = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 1, ModelPosition.createAt( modelRoot, 3 ), 0 );
  252. const move2 = new MoveOperation( ModelPosition.createAt( modelRoot, 1 ), 1, ModelPosition.createAt( modelRoot, 6 ), 0 );
  253. delta.addOperation( move1 );
  254. delta.addOperation( move2 );
  255. expect( delta.toString() ).to.equal( 'MoveDelta( 0 ): main [ 0 ] - [ 1 ] -> main [ 3 ]; main [ 1 ] - [ 2 ] -> main [ 6 ]' );
  256. delta.log();
  257. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  258. } );
  259. it( 'RenameDelta', () => {
  260. const delta = new RenameDelta();
  261. const op = new RenameOperation( ModelPosition.createAt( modelRoot, 1 ), 'old', 'new', 0 );
  262. delta.addOperation( op );
  263. expect( delta.toString() ).to.equal( 'RenameDelta( 0 ): main [ 1 ]: "old" -> "new"' );
  264. delta.log();
  265. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  266. } );
  267. it( 'RootAttributeDelta', () => {
  268. const delta = new RootAttributeDelta();
  269. const op = new RootAttributeOperation( modelRoot, 'key', 'old', null, 0 );
  270. delta.addOperation( op );
  271. expect( delta.toString() ).to.equal( 'RootAttributeDelta( 0 ): "key": "old" -> null, main' );
  272. delta.log();
  273. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  274. } );
  275. it( 'SplitDelta', () => {
  276. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  277. const splitEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  278. otherRoot.appendChildren( [ splitEle ] );
  279. const delta = new SplitDelta();
  280. const insert = new InsertOperation( ModelPosition.createAt( otherRoot, 1 ), [ new ModelElement( 'paragraph' ) ], 0 );
  281. const move = new MoveOperation( ModelPosition.createAt( splitEle, 1 ), 2, new ModelPosition( otherRoot, [ 1, 0 ] ), 1 );
  282. delta.addOperation( insert );
  283. delta.addOperation( move );
  284. expect( delta.toString() ).to.equal( 'SplitDelta( 0 ): otherRoot [ 0, 1 ]' );
  285. delta.log();
  286. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  287. } );
  288. it( 'UnwrapDelta', () => {
  289. const otherRoot = modelDoc.createRoot( 'main', 'otherRoot' );
  290. const unwrapEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  291. otherRoot.appendChildren( [ unwrapEle ] );
  292. const delta = new UnwrapDelta();
  293. const move = new MoveOperation( ModelPosition.createAt( unwrapEle, 0 ), 3, ModelPosition.createAt( otherRoot, 0 ), 0 );
  294. const remove = new RemoveOperation( ModelPosition.createAt( otherRoot, 3 ), 1, 1 );
  295. delta.addOperation( move );
  296. delta.addOperation( remove );
  297. expect( delta.toString() ).to.equal( 'UnwrapDelta( 0 ): otherRoot [ 0 ]' );
  298. delta.log();
  299. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  300. } );
  301. it( 'WrapDelta', () => {
  302. const delta = new WrapDelta();
  303. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 6 ), new ModelElement( 'paragraph' ), 0 );
  304. const move = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 6, new ModelPosition( modelRoot, [ 1, 0 ] ), 1 );
  305. delta.addOperation( insert );
  306. delta.addOperation( move );
  307. expect( delta.toString() ).to.equal( 'WrapDelta( 0 ): main [ 0 ] - [ 6 ] -> <paragraph>' );
  308. delta.log();
  309. expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
  310. } );
  311. } );
  312. it( 'for applied operations', () => {
  313. const delta = new InsertDelta();
  314. const op = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), [ new ModelText( 'foo' ) ], 0 );
  315. delta.addOperation( op );
  316. modelDoc.applyOperation( op );
  317. expect( log.calledWithExactly( 'Applying InsertOperation( 0 ): [ 1 ] -> main [ 0 ]' ) ).to.be.true;
  318. } );
  319. } );
  320. describe( 'should provide tree printing tools', () => {
  321. it( 'for model', () => {
  322. const modelDoc = new ModelDocument();
  323. const modelRoot = modelDoc.createRoot();
  324. modelRoot.appendChildren( [
  325. new ModelElement( 'paragraph', { foo: 'bar' }, [
  326. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  327. ] ),
  328. new ModelElement( 'listItem', { type: 'numbered', indent: 0 }, new ModelText( 'One.' ) ),
  329. ] );
  330. const modelRootTree = modelRoot.printTree();
  331. expect( modelRootTree ).to.equal(
  332. '<main>' +
  333. '\n\t<paragraph foo="bar">' +
  334. '\n\t\tThis is ' +
  335. '\n\t\t<$text bold=true>bold</$text>' +
  336. '\n\t\t.' +
  337. '\n\t</paragraph>' +
  338. '\n\t<listItem type="numbered" indent=0>' +
  339. '\n\t\tOne.' +
  340. '\n\t</listItem>' +
  341. '\n</main>'
  342. );
  343. modelRoot.logTree();
  344. expect( log.calledWithExactly( modelRootTree ) ).to.be.true;
  345. const modelParagraph = modelRoot.getChild( 0 );
  346. const modelParagraphTree = modelParagraph.printTree();
  347. expect( modelParagraphTree ).to.equal(
  348. '<paragraph foo="bar">' +
  349. '\n\tThis is ' +
  350. '\n\t<$text bold=true>bold</$text>' +
  351. '\n\t.' +
  352. '\n</paragraph>'
  353. );
  354. log.reset();
  355. modelParagraph.logTree();
  356. expect( log.calledWithExactly( modelParagraphTree ) ).to.be.true;
  357. const modelDocFrag = new ModelDocumentFragment( [
  358. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' ),
  359. new ModelElement( 'paragraph', { foo: 'bar' }, [
  360. new ModelText( 'This is ' ), new ModelText( 'bold', { bold: true } ), new ModelText( '.' )
  361. ] )
  362. ] );
  363. const modelDocFragTree = modelDocFrag.printTree();
  364. expect( modelDocFragTree ).to.equal(
  365. 'ModelDocumentFragment: [' +
  366. '\n\tThis is ' +
  367. '\n\t<$text bold=true>bold</$text>' +
  368. '\n\t.' +
  369. '\n\t<paragraph foo="bar">' +
  370. '\n\t\tThis is ' +
  371. '\n\t\t<$text bold=true>bold</$text>' +
  372. '\n\t\t.' +
  373. '\n\t</paragraph>' +
  374. '\n]'
  375. );
  376. log.reset();
  377. modelDocFrag.logTree();
  378. expect( log.calledWithExactly( modelDocFragTree ) ).to.be.true;
  379. } );
  380. it( 'for view', () => {
  381. const viewDoc = new ViewDocument();
  382. const viewRoot = viewDoc.createRoot( 'div' );
  383. viewRoot.appendChildren( [
  384. new ViewContainerElement( 'p', { foo: 'bar' }, [
  385. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  386. ] ),
  387. new ViewContainerElement( 'ol', null, [
  388. new ViewContainerElement( 'li', null, new ViewText( 'One.' ) )
  389. ] )
  390. ] );
  391. const viewRootTree = viewRoot.printTree();
  392. expect( viewRootTree ).to.equal(
  393. '<div>' +
  394. '\n\t<p foo="bar">' +
  395. '\n\t\tThis is ' +
  396. '\n\t\t<b>' +
  397. '\n\t\t\tbold' +
  398. '\n\t\t</b>' +
  399. '\n\t\t.' +
  400. '\n\t</p>' +
  401. '\n\t<ol>' +
  402. '\n\t\t<li>' +
  403. '\n\t\t\tOne.' +
  404. '\n\t\t</li>' +
  405. '\n\t</ol>' +
  406. '\n</div>'
  407. );
  408. viewRoot.logTree();
  409. expect( log.calledWithExactly( viewRootTree ) ).to.be.true;
  410. const viewParagraph = viewRoot.getChild( 0 );
  411. const viewParagraphTree = viewParagraph.printTree();
  412. expect( viewParagraphTree ).to.equal(
  413. '<p foo="bar">' +
  414. '\n\tThis is ' +
  415. '\n\t<b>' +
  416. '\n\t\tbold' +
  417. '\n\t</b>' +
  418. '\n\t.' +
  419. '\n</p>'
  420. );
  421. log.reset();
  422. viewParagraph.logTree();
  423. expect( log.calledWithExactly( viewParagraphTree ) ).to.be.true;
  424. const viewDocFrag = new ViewDocumentFragment( [
  425. new ViewText( 'Text.' ),
  426. new ViewContainerElement( 'p', { foo: 'bar' }, [
  427. new ViewText( 'This is ' ), new ViewAttributeElement( 'b', null, new ViewText( 'bold' ) ), new ViewText( '.' )
  428. ] )
  429. ] );
  430. const viewDocFragTree = viewDocFrag.printTree();
  431. expect( viewDocFragTree ).to.equal(
  432. 'ViewDocumentFragment: [' +
  433. '\n\tText.' +
  434. '\n\t<p foo="bar">' +
  435. '\n\t\tThis is ' +
  436. '\n\t\t<b>' +
  437. '\n\t\t\tbold' +
  438. '\n\t\t</b>' +
  439. '\n\t\t.' +
  440. '\n\t</p>' +
  441. '\n]'
  442. );
  443. log.reset();
  444. viewDocFrag.logTree();
  445. expect( log.calledWithExactly( viewDocFragTree ) ).to.be.true;
  446. } );
  447. } );
  448. describe( 'should store model and view trees state', () => {
  449. let editor;
  450. beforeEach( () => {
  451. const div = document.createElement( 'div' );
  452. return TestEditor.create( div, {
  453. plugins: [ DebugPlugin ]
  454. } ).then( _editor => {
  455. editor = _editor;
  456. } );
  457. } );
  458. it( 'should store model and view state after each applied operation', () => {
  459. const model = editor.document;
  460. const modelRoot = model.getRoot();
  461. const view = editor.editing.view;
  462. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), 0 );
  463. model.applyOperation( wrapInDelta( insert ) );
  464. const remove = new RemoveOperation( ModelPosition.createAt( modelRoot, 1 ), 2, 1 );
  465. model.applyOperation( wrapInDelta( remove ) );
  466. log.reset();
  467. model.log( 0 );
  468. expectLog(
  469. '<$graveyard></$graveyard>' +
  470. '\n<main></main>'
  471. );
  472. model.log( 1 );
  473. expectLog(
  474. '<$graveyard></$graveyard>' +
  475. '\n<main>' +
  476. '\n\tfoobar' +
  477. '\n</main>'
  478. );
  479. model.log( 2 );
  480. expectLog(
  481. '<$graveyard>' +
  482. '\n\t<$graveyardHolder>' +
  483. '\n\t\too' +
  484. '\n\t</$graveyardHolder>' +
  485. '\n</$graveyard>' +
  486. '\n<main>' +
  487. '\n\tfbar' +
  488. '\n</main>'
  489. );
  490. model.log();
  491. expectLog(
  492. '<$graveyard>' +
  493. '\n\t<$graveyardHolder>' +
  494. '\n\t\too' +
  495. '\n\t</$graveyardHolder>' +
  496. '\n</$graveyard>' +
  497. '\n<main>' +
  498. '\n\tfbar' +
  499. '\n</main>'
  500. );
  501. view.log( 0 );
  502. expectLog(
  503. '<div></div>'
  504. );
  505. view.log( 1 );
  506. expectLog(
  507. '<div>' +
  508. '\n\tfoobar' +
  509. '\n</div>'
  510. );
  511. view.log( 2 );
  512. expectLog(
  513. '<div>' +
  514. '\n\tfbar' +
  515. '\n</div>'
  516. );
  517. sinon.spy( model, 'log' );
  518. sinon.spy( view, 'log' );
  519. editor.logModel( 1 );
  520. expect( model.log.calledWithExactly( 1 ) ).to.be.true;
  521. editor.logView( 2 );
  522. expect( view.log.calledWithExactly( 2 ) ).to.be.true;
  523. model.log.reset();
  524. view.log.reset();
  525. editor.logModel();
  526. expect( model.log.calledWithExactly( 2 ) ).to.be.true;
  527. model.log.reset();
  528. view.log.reset();
  529. editor.logDocuments();
  530. expect( model.log.calledWithExactly( 2 ) ).to.be.true;
  531. expect( view.log.calledWithExactly( 2 ) ).to.be.true;
  532. model.log.reset();
  533. view.log.reset();
  534. editor.logDocuments( 1 );
  535. expect( model.log.calledWithExactly( 1 ) ).to.be.true;
  536. expect( view.log.calledWithExactly( 1 ) ).to.be.true;
  537. } );
  538. it( 'should remove old states', () => {
  539. const model = editor.document;
  540. const modelRoot = model.getRoot();
  541. for ( let i = 0; i < 25; i++ ) {
  542. const insert = new InsertOperation( ModelPosition.createAt( modelRoot, 0 ), new ModelText( 'foobar' ), model.version );
  543. model.applyOperation( wrapInDelta( insert ) );
  544. }
  545. model.log( 0 );
  546. expectLog( 'Tree log unavailable for given version: 0' );
  547. } );
  548. } );
  549. describe( 'should provide methods for delta replayer', () => {
  550. it( 'getAppliedDeltas()', () => {
  551. const modelDoc = new ModelDocument();
  552. expect( modelDoc.getAppliedDeltas() ).to.equal( '' );
  553. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  554. const firstEle = new ModelElement( 'paragraph' );
  555. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  556. otherRoot.appendChildren( [ firstEle, removedEle ] );
  557. const delta = new MergeDelta();
  558. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  559. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, 1 );
  560. delta.addOperation( move );
  561. delta.addOperation( remove );
  562. modelDoc.applyOperation( move );
  563. modelDoc.applyOperation( remove );
  564. const stringifiedDeltas = modelDoc.getAppliedDeltas();
  565. expect( stringifiedDeltas ).to.equal( JSON.stringify( delta.toJSON() ) );
  566. } );
  567. it( 'createReplayer()', () => {
  568. const modelDoc = new ModelDocument();
  569. const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
  570. const firstEle = new ModelElement( 'paragraph' );
  571. const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
  572. otherRoot.appendChildren( [ firstEle, removedEle ] );
  573. const delta = new MergeDelta();
  574. const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
  575. const remove = new RemoveOperation( ModelPosition.createBefore( removedEle ), 1, 1 );
  576. delta.addOperation( move );
  577. delta.addOperation( remove );
  578. const stringifiedDeltas = JSON.stringify( delta.toJSON() );
  579. const deltaReplayer = modelDoc.createReplayer( stringifiedDeltas );
  580. expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ JSON.parse( stringifiedDeltas ) ] );
  581. } );
  582. } );
  583. describe( 'should provide means for saving delta history transformation', () => {
  584. let document, root, otherRoot;
  585. beforeEach( () => {
  586. document = new ModelDocument();
  587. root = document.createRoot();
  588. otherRoot = document.createRoot( 'other', 'other' );
  589. } );
  590. it( 'Delta#_saveHistory()', () => {
  591. const insertDeltaA = new InsertDelta();
  592. const insertOpA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  593. insertDeltaA.addOperation( insertOpA );
  594. const insertDeltaB = new InsertDelta();
  595. const insertOpB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  596. insertDeltaB.addOperation( insertOpB );
  597. const insertDeltaFinalA = new InsertDelta();
  598. const insertOpFinalA = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  599. insertDeltaFinalA.addOperation( insertOpFinalA );
  600. const insertDeltaFinalAJsonWithoutHistory = JSON.stringify( insertDeltaFinalA );
  601. insertDeltaFinalA._saveHistory( {
  602. before: insertDeltaA,
  603. transformedBy: insertDeltaB,
  604. wasImportant: true,
  605. resultIndex: 0,
  606. resultsTotal: 1
  607. } );
  608. const insertDeltaC = new InsertDelta();
  609. const insertOpC = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 1 );
  610. insertDeltaC.addOperation( insertOpC );
  611. const insertDeltaFinalB = new InsertDelta();
  612. const insertOpFinalB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 2 );
  613. insertDeltaFinalB.addOperation( insertOpFinalB );
  614. insertDeltaFinalB._saveHistory( {
  615. before: insertDeltaFinalA,
  616. transformedBy: insertDeltaC,
  617. wasImportant: false,
  618. resultIndex: 1,
  619. resultsTotal: 3
  620. } );
  621. expect( insertDeltaA.history ).to.be.undefined;
  622. expect( insertDeltaB.history ).to.be.undefined;
  623. expect( insertDeltaFinalA.history ).not.to.be.undefined;
  624. expect( insertDeltaFinalA.history.length ).to.equal( 1 );
  625. expect( insertDeltaFinalA.history[ 0 ] ).to.deep.equal( {
  626. before: JSON.stringify( insertDeltaA ),
  627. transformedBy: JSON.stringify( insertDeltaB ),
  628. wasImportant: true,
  629. resultIndex: 0,
  630. resultsTotal: 1
  631. } );
  632. expect( insertDeltaFinalB.history ).not.to.be.undefined;
  633. expect( insertDeltaFinalB.history.length ).to.equal( 2 );
  634. expect( insertDeltaFinalB.history[ 0 ] ).to.deep.equal( {
  635. before: JSON.stringify( insertDeltaA ),
  636. transformedBy: JSON.stringify( insertDeltaB ),
  637. wasImportant: true,
  638. resultIndex: 0,
  639. resultsTotal: 1
  640. } );
  641. expect( insertDeltaFinalB.history[ 1 ] ).to.deep.equal( {
  642. before: insertDeltaFinalAJsonWithoutHistory,
  643. transformedBy: JSON.stringify( insertDeltaC ),
  644. wasImportant: false,
  645. resultIndex: 1,
  646. resultsTotal: 3
  647. } );
  648. } );
  649. it( 'save delta history on deltaTransform.transform', () => {
  650. const deltaA = new MoveDelta();
  651. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  652. deltaA.addOperation( opA );
  653. const deltaB = new InsertDelta();
  654. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  655. deltaB.addOperation( opB );
  656. const deltaC = new MoveDelta();
  657. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  658. deltaC.addOperation( opC );
  659. let result = deltaTransform.transform( deltaA, deltaB, false );
  660. expect( result[ 0 ].history ).not.to.be.undefined;
  661. expect( result[ 0 ].history.length ).to.equal( 1 );
  662. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  663. before: JSON.stringify( deltaA ),
  664. transformedBy: JSON.stringify( deltaB ),
  665. wasImportant: false,
  666. resultIndex: 0,
  667. resultsTotal: 1
  668. } );
  669. const firstResultWithoutHistory = result[ 0 ].clone();
  670. delete firstResultWithoutHistory.history;
  671. result = deltaTransform.transform( result[ 0 ], deltaC, true );
  672. expect( result[ 0 ].history ).not.to.be.undefined;
  673. expect( result[ 0 ].history.length ).to.equal( 2 );
  674. expect( result[ 0 ].history[ 0 ] ).to.deep.equal( {
  675. before: JSON.stringify( deltaA ),
  676. transformedBy: JSON.stringify( deltaB ),
  677. wasImportant: false,
  678. resultIndex: 0,
  679. resultsTotal: 1
  680. } );
  681. expect( result[ 0 ].history[ 1 ] ).to.deep.equal( {
  682. before: JSON.stringify( firstResultWithoutHistory ),
  683. transformedBy: JSON.stringify( deltaC ),
  684. wasImportant: true,
  685. resultIndex: 0,
  686. resultsTotal: 1
  687. } );
  688. } );
  689. it( 'recreate delta using history', () => {
  690. const deltaA = new MoveDelta();
  691. const opA = new MoveOperation( ModelPosition.createAt( root, 4 ), 4, ModelPosition.createAt( otherRoot, 4 ), 0 );
  692. deltaA.addOperation( opA );
  693. const deltaB = new InsertDelta();
  694. const opB = new InsertOperation( ModelPosition.createAt( root, 0 ), new ModelText( 'a' ), 0 );
  695. deltaB.addOperation( opB );
  696. const deltaC = new MoveDelta();
  697. const opC = new MoveOperation( ModelPosition.createAt( root, 4 ), 2, ModelPosition.createAt( root, 0 ), 1 );
  698. deltaC.addOperation( opC );
  699. let original = deltaTransform.transform( deltaA, deltaB, false );
  700. original = deltaTransform.transform( original[ 0 ], deltaC, true )[ 0 ];
  701. const history = original.history;
  702. let recreated = deltaTransform.transform(
  703. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].before ), document ),
  704. DeltaFactory.fromJSON( JSON.parse( history[ 0 ].transformedBy ), document ),
  705. history[ 0 ].wasImportant
  706. );
  707. recreated = recreated[ history[ 0 ].resultIndex ];
  708. recreated = deltaTransform.transform(
  709. recreated,
  710. DeltaFactory.fromJSON( JSON.parse( history[ 1 ].transformedBy ), document ),
  711. history[ 1 ].wasImportant
  712. );
  713. recreated = recreated[ history[ 1 ].resultIndex ];
  714. delete original.history;
  715. delete recreated.history;
  716. expect( JSON.stringify( recreated ) ).to.equal( JSON.stringify( original ) );
  717. } );
  718. } );
  719. function expectLog( expectedLogMsg ) {
  720. expect( log.calledWithExactly( expectedLogMsg ) ).to.be.true;
  721. log.reset();
  722. }
  723. function wrapInDelta( op ) {
  724. const delta = new Delta();
  725. delta.addOperation( op );
  726. return op;
  727. }
  728. } );