8
0

wrapdelta.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Range from '/ckeditor5/core/treemodel/range.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import WrapDelta from '/ckeditor5/core/treemodel/delta/wrapdelta.js';
  13. import UnwrapDelta from '/ckeditor5/core/treemodel/delta/unwrapdelta.js';
  14. import InsertOperation from '/ckeditor5/core/treemodel/operation/insertoperation.js';
  15. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  16. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  17. describe( 'Batch', () => {
  18. let doc, root, range;
  19. beforeEach( () => {
  20. doc = new Document();
  21. root = doc.createRoot( 'root' );
  22. root.insertChildren( 0, 'foobar' );
  23. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  24. } );
  25. describe( 'wrap', () => {
  26. it( 'should wrap flat range with given element', () => {
  27. let p = new Element( 'p' );
  28. doc.batch().wrap( range, p );
  29. expect( root.getChildCount() ).to.equal( 5 );
  30. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  31. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  32. expect( root.getChild( 2 ) ).to.equal( p );
  33. expect( p.getChild( 0 ).character ).to.equal( 'o' );
  34. expect( p.getChild( 1 ).character ).to.equal( 'b' );
  35. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  36. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  37. } );
  38. it( 'should wrap flat range with an element of given name', () => {
  39. doc.batch().wrap( range, 'p' );
  40. expect( root.getChildCount() ).to.equal( 5 );
  41. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  42. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  43. expect( root.getChild( 2 ).name ).to.equal( 'p' );
  44. expect( root.getChild( 2 ).getChild( 0 ).character ).to.equal( 'o' );
  45. expect( root.getChild( 2 ).getChild( 1 ).character ).to.equal( 'b' );
  46. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  47. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  48. } );
  49. it( 'should throw if range to wrap is not flat', () => {
  50. root.insertChildren( 6, [ new Element( 'p', [], 'xyz' ) ] );
  51. let notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  52. expect( () => {
  53. doc.batch().wrap( notFlatRange, 'p' );
  54. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  55. } );
  56. it( 'should throw if element to wrap with has children', () => {
  57. let p = new Element( 'p', [], 'a' );
  58. expect( () => {
  59. doc.batch().wrap( range, p );
  60. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  61. } );
  62. it( 'should throw if element to wrap with has children', () => {
  63. let p = new Element( 'p' );
  64. root.insertChildren( 0, p );
  65. expect( () => {
  66. doc.batch().wrap( range, p );
  67. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  68. } );
  69. it( 'should be chainable', () => {
  70. const batch = doc.batch();
  71. const chain = batch.wrap( range, 'p' );
  72. expect( chain ).to.equal( batch );
  73. } );
  74. } );
  75. } );
  76. describe( 'WrapDelta', () => {
  77. let wrapDelta, doc, root;
  78. beforeEach( () => {
  79. doc = new Document();
  80. root = doc.createRoot( 'root' );
  81. wrapDelta = new WrapDelta();
  82. } );
  83. describe( 'constructor', () => {
  84. it( 'should create wrap delta with no operations added', () => {
  85. expect( wrapDelta.operations.length ).to.equal( 0 );
  86. } );
  87. } );
  88. describe( 'range', () => {
  89. it( 'should be equal to null if there are no operations in delta', () => {
  90. expect( wrapDelta.range ).to.be.null;
  91. } );
  92. it( 'should be equal to wrapped range', () => {
  93. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  94. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  95. expect( wrapDelta.range.start.isEqual( new Position( root, [ 1, 1 ] ) ) ).to.be.true;
  96. expect( wrapDelta.range.end.isEqual( new Position( root, [ 1, 6 ] ) ) ).to.be.true;
  97. } );
  98. } );
  99. describe( 'howMany', () => {
  100. it( 'should be equal to 0 if there are no operations in delta', () => {
  101. expect( wrapDelta.howMany ).to.equal( 0 );
  102. } );
  103. it( 'should be equal to the number of wrapped elements', () => {
  104. let howMany = 5;
  105. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  106. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), howMany, new Position( root, [ 1, 6, 0 ] ) ) );
  107. expect( wrapDelta.howMany ).to.equal( 5 );
  108. } );
  109. } );
  110. describe( 'getReversed', () => {
  111. it( 'should return empty UnwrapDelta if there are no operations in delta', () => {
  112. let reversed = wrapDelta.getReversed();
  113. expect( reversed ).to.be.instanceof( UnwrapDelta );
  114. expect( reversed.operations.length ).to.equal( 0 );
  115. } );
  116. it( 'should return correct UnwrapDelta', () => {
  117. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  118. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  119. let reversed = wrapDelta.getReversed();
  120. expect( reversed ).to.be.instanceof( UnwrapDelta );
  121. expect( reversed.operations.length ).to.equal( 2 );
  122. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  123. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 1, 0 ] );
  124. expect( reversed.operations[ 0 ].howMany ).to.equal( 5 );
  125. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  126. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  127. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 6 ] );
  128. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  129. } );
  130. } );
  131. describe( '_insertOperation', () => {
  132. it( 'should be null if there are no operations in the delta', () => {
  133. expect( wrapDelta._insertOperation ).to.be.null;
  134. } );
  135. it( 'should be equal to the first operation in the delta', () => {
  136. let insertOperation = new InsertOperation( new Position( root, [ 1, 6 ] ), 1 );
  137. wrapDelta.operations.push( insertOperation );
  138. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  139. expect( wrapDelta._insertOperation ).to.equal( insertOperation );
  140. } );
  141. } );
  142. } );