unwrapdelta.js 4.6 KB

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