unwrapdelta.js 4.8 KB

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