8
0

unwrapdelta.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Element from '/ckeditor5/engine/model/element.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( '$root', 'root' );
  21. p = new Element( 'p', [], 'xyz' );
  22. root.insertChildren( 0, [ 'a', p, 'b' ] );
  23. } );
  24. describe( 'unwrap', () => {
  25. it( 'should unwrap given element', () => {
  26. doc.batch().unwrap( p );
  27. expect( root.getChildCount() ).to.equal( 5 );
  28. expect( root.getChild( 0 ).character ).to.equal( 'a' );
  29. expect( root.getChild( 1 ).character ).to.equal( 'x' );
  30. expect( root.getChild( 2 ).character ).to.equal( 'y' );
  31. expect( root.getChild( 3 ).character ).to.equal( 'z' );
  32. expect( root.getChild( 4 ).character ).to.equal( 'b' );
  33. } );
  34. it( 'should throw if element to unwrap has no parent', () => {
  35. let element = new Element( 'p' );
  36. expect( () => {
  37. doc.batch().unwrap( element );
  38. } ).to.throw( CKEditorError, /^batch-unwrap-element-no-parent/ );
  39. } );
  40. it( 'should be chainable', () => {
  41. const batch = doc.batch();
  42. const chain = batch.unwrap( p );
  43. expect( chain ).to.equal( batch );
  44. } );
  45. it( 'should add delta to batch and operation to delta before applying operation', () => {
  46. sinon.spy( doc, 'applyOperation' );
  47. const batch = doc.batch().unwrap( p );
  48. const correctDeltaMatcher = sinon.match( ( operation ) => {
  49. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  50. } );
  51. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  52. } );
  53. } );
  54. } );
  55. describe( 'UnwrapDelta', () => {
  56. let unwrapDelta, doc, root;
  57. beforeEach( () => {
  58. doc = new Document();
  59. root = doc.createRoot( '$root', 'root' );
  60. unwrapDelta = new UnwrapDelta();
  61. } );
  62. describe( 'constructor', () => {
  63. it( 'should create unwrap delta with no operations added', () => {
  64. expect( unwrapDelta.operations.length ).to.equal( 0 );
  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 ) );
  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. let 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 ) );
  87. let 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. } );