mergedelta.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Position from '/ckeditor5/core/treemodel/position.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. import MergeDelta from '/ckeditor5/core/treemodel/delta/mergedelta.js';
  12. import SplitDelta from '/ckeditor5/core/treemodel/delta/splitdelta.js';
  13. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  14. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  15. import ReinsertOperation from '/ckeditor5/core/treemodel/operation/reinsertoperation.js';
  16. describe( 'Batch', () => {
  17. let doc, root, p1, p2;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot( 'root' );
  21. p1 = new Element( 'p', { key1: 'value1' }, 'foo' );
  22. p2 = new Element( 'p', { key2: 'value2' }, 'bar' );
  23. root.insertChildren( 0, [ p1, p2 ] );
  24. } );
  25. describe( 'merge', () => {
  26. it( 'should merge foo and bar into foobar', () => {
  27. doc.batch().merge( new Position( root, [ 1 ] ) );
  28. expect( root.getChildCount() ).to.equal( 1 );
  29. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  30. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  31. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  32. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  33. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  34. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  35. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  36. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  37. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  38. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  39. } );
  40. it( 'should throw if there is no element after', () => {
  41. expect( () => {
  42. doc.batch().merge( new Position( root, [ 2 ] ) );
  43. } ).to.throw( CKEditorError, /^batch-merge-no-element-after/ );
  44. } );
  45. it( 'should throw if there is no element before', () => {
  46. expect( () => {
  47. doc.batch().merge( new Position( root, [ 0, 2 ] ) );
  48. } ).to.throw( CKEditorError, /^batch-merge-no-element-before/ );
  49. } );
  50. it( 'should be chainable', () => {
  51. const batch = doc.batch();
  52. const chain = batch.merge( new Position( root, [ 1 ] ) );
  53. expect( chain ).to.equal( batch );
  54. } );
  55. } );
  56. } );
  57. describe( 'MergeDelta', () => {
  58. let mergeDelta, doc, root;
  59. beforeEach( () => {
  60. doc = new Document();
  61. root = doc.createRoot( 'root' );
  62. mergeDelta = new MergeDelta();
  63. } );
  64. describe( 'constructor', () => {
  65. it( 'should create merge delta with no operations added', () => {
  66. expect( mergeDelta.operations.length ).to.equal( 0 );
  67. } );
  68. } );
  69. describe( 'position', () => {
  70. it( 'should be null if there are no operations in delta', () => {
  71. expect( mergeDelta.position ).to.be.null;
  72. } );
  73. it( 'should be equal to the position between merged nodes', () => {
  74. mergeDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 1, 4 ] ) ) );
  75. mergeDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 2, 0 ] ), 1 ) );
  76. expect( mergeDelta.position.root ).to.equal( root );
  77. expect( mergeDelta.position.path ).to.deep.equal( [ 1, 2, 0 ] );
  78. } );
  79. } );
  80. describe( 'getReversed', () => {
  81. it( 'should return empty SplitDelta if there are no operations in delta', () => {
  82. let reversed = mergeDelta.getReversed();
  83. expect( reversed ).to.be.instanceof( SplitDelta );
  84. expect( reversed.operations.length ).to.equal( 0 );
  85. } );
  86. it( 'should return correct SplitDelta', () => {
  87. mergeDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 1, 4 ] ) ) );
  88. mergeDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 2, 0 ] ), 1 ) );
  89. let reversed = mergeDelta.getReversed();
  90. expect( reversed ).to.be.instanceof( SplitDelta );
  91. expect( reversed.operations.length ).to.equal( 2 );
  92. expect( reversed.operations[ 0 ] ).to.be.instanceof( ReinsertOperation );
  93. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  94. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  95. expect( reversed.operations[ 1 ] ).to.be.instanceof( MoveOperation );
  96. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  97. expect( reversed.operations[ 1 ].howMany ).to.equal( 4 );
  98. expect( reversed.operations[ 1 ].targetPosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  99. } );
  100. } );
  101. } );