enableenginedebug.js 32 KB

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