unwrapdelta.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Element from '../../../src/model/element';
  7. import Text from '../../../src/model/text';
  8. import Position from '../../../src/model/position';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import UnwrapDelta from '../../../src/model/delta/unwrapdelta';
  11. import WrapDelta from '../../../src/model/delta/wrapdelta';
  12. import MoveOperation from '../../../src/model/operation/moveoperation';
  13. import RemoveOperation from '../../../src/model/operation/removeoperation';
  14. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  15. describe( 'Batch', () => {
  16. let doc, root, p;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. p = new Element( 'p', [], new Text( 'xyz' ) );
  21. root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  22. } );
  23. describe( 'unwrap', () => {
  24. it( 'should unwrap given element', () => {
  25. doc.batch().unwrap( p );
  26. expect( root.maxOffset ).to.equal( 5 );
  27. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  28. } );
  29. it( 'should throw if element to unwrap has no parent', () => {
  30. const element = new Element( 'p' );
  31. expect( () => {
  32. doc.batch().unwrap( element );
  33. } ).to.throw( CKEditorError, /^batch-unwrap-element-no-parent/ );
  34. } );
  35. it( 'should be chainable', () => {
  36. const batch = doc.batch();
  37. const chain = batch.unwrap( p );
  38. expect( chain ).to.equal( batch );
  39. } );
  40. it( 'should add delta to batch and operation to delta before applying operation', () => {
  41. sinon.spy( doc, 'applyOperation' );
  42. const batch = doc.batch().unwrap( p );
  43. const correctDeltaMatcher = sinon.match( operation => {
  44. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  45. } );
  46. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  47. } );
  48. } );
  49. } );
  50. describe( 'UnwrapDelta', () => {
  51. let unwrapDelta, doc, root;
  52. beforeEach( () => {
  53. doc = new Document();
  54. root = doc.createRoot();
  55. unwrapDelta = new UnwrapDelta();
  56. } );
  57. describe( 'constructor()', () => {
  58. it( 'should create unwrap delta with no operations added', () => {
  59. expect( unwrapDelta.operations.length ).to.equal( 0 );
  60. } );
  61. } );
  62. describe( 'type', () => {
  63. it( 'should be equal to unwrap', () => {
  64. expect( unwrapDelta.type ).to.equal( 'unwrap' );
  65. } );
  66. } );
  67. describe( 'position', () => {
  68. it( 'should be null if there are no operations in delta', () => {
  69. expect( unwrapDelta.position ).to.be.null;
  70. } );
  71. it( 'should be equal to the position before unwrapped node', () => {
  72. unwrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 2 ] ) ) );
  73. unwrapDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 6 ] ), 1, new Position( doc.graveyard, [ 0 ] ) ) );
  74. expect( unwrapDelta.position.root ).to.equal( root );
  75. expect( unwrapDelta.position.path ).to.deep.equal( [ 1, 2 ] );
  76. } );
  77. } );
  78. describe( 'getReversed', () => {
  79. it( 'should return empty WrapDelta if there are no operations in delta', () => {
  80. const reversed = unwrapDelta.getReversed();
  81. expect( reversed ).to.be.instanceof( WrapDelta );
  82. expect( reversed.operations.length ).to.equal( 0 );
  83. } );
  84. it( 'should return correct WrapDelta', () => {
  85. unwrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 2, 0 ] ), 4, new Position( root, [ 1, 2 ] ) ) );
  86. unwrapDelta.operations.push( new RemoveOperation( new Position( root, [ 1, 6 ] ), 1, new Position( doc.graveyard, [ 0 ] ) ) );
  87. const reversed = unwrapDelta.getReversed();
  88. expect( reversed ).to.be.instanceof( WrapDelta );
  89. expect( reversed.operations.length ).to.equal( 2 );
  90. // WrapDelta which is an effect of reversing UnwrapDelta has ReinsertOperation instead of InsertOperation.
  91. // This is because we will "wrap" nodes into the element in which they were in the first place.
  92. // That element has been removed so we reinsert it from the graveyard.
  93. expect( reversed.operations[ 0 ] ).to.be.instanceof( ReinsertOperation );
  94. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  95. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 6 ] );
  96. expect( reversed.operations[ 1 ] ).to.be.instanceof( MoveOperation );
  97. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  98. expect( reversed.operations[ 1 ].howMany ).to.equal( 4 );
  99. expect( reversed.operations[ 1 ].targetPosition.path ).to.deep.equal( [ 1, 6, 0 ] );
  100. } );
  101. } );
  102. it( 'should provide proper className', () => {
  103. expect( UnwrapDelta.className ).to.equal( 'engine.model.delta.UnwrapDelta' );
  104. } );
  105. } );