8
0

enableenginedebug.js 31 KB

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