position.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Position from '/ckeditor5/engine/treeview/position.js';
  8. import Node from '/ckeditor5/engine/treeview/node.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import Text from '/ckeditor5/engine/treeview/text.js';
  11. describe( 'Position', () => {
  12. const parentMock = {};
  13. describe( 'constructor', () => {
  14. it( 'should create element without attributes', () => {
  15. const elem = new Position( parentMock, 5 );
  16. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  17. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  18. } );
  19. } );
  20. describe( 'getShiftedBy', () => {
  21. it( 'returns new instance with shifted offset', () => {
  22. const position = new Position( parentMock, 10 );
  23. const shifted = position.getShiftedBy( 12 );
  24. expect( shifted.offset ).to.equal( 22 );
  25. } );
  26. it( 'accepts negative values', () => {
  27. const position = new Position( parentMock, 10 );
  28. const shifted = position.getShiftedBy( -5 );
  29. expect( shifted.offset ).to.equal( 5 );
  30. } );
  31. it( 'prevents offset to be a negative value', () => {
  32. const position = new Position( parentMock, 10 );
  33. const shifted = position.getShiftedBy( -20 );
  34. expect( shifted.offset ).to.equal( 0 );
  35. } );
  36. } );
  37. describe( 'createFromPosition', () => {
  38. it( 'creates new Position with same parent and offset', () => {
  39. const offset = 50;
  40. const position = new Position( parentMock, offset );
  41. const newPosition = Position.createFromPosition( position );
  42. expect( position ).to.not.equal( newPosition );
  43. expect( position.offset ).to.equal( offset );
  44. expect( position.parent ).to.equal( parentMock );
  45. } );
  46. } );
  47. describe( 'isEqual', () => {
  48. it( 'should return true for same object', () => {
  49. const position = new Position( {}, 12 );
  50. expect( position.isEqual( position ) ).to.be.true;
  51. } );
  52. it( 'should return true for positions with same parent and offset', () => {
  53. const parentMock = {};
  54. const position1 = new Position( parentMock, 12 );
  55. const position2 = new Position( parentMock, 12 );
  56. expect( position1.isEqual( position2 ) ).to.be.true;
  57. } );
  58. it( 'should return false for positions with different parents', () => {
  59. const position1 = new Position( {}, 12 );
  60. const position2 = new Position( {}, 12 );
  61. expect( position1.isEqual( position2 ) ).to.be.false;
  62. } );
  63. it( 'should return false for positions with different positions', () => {
  64. const parentMock = {};
  65. const position1 = new Position( parentMock, 12 );
  66. const position2 = new Position( parentMock, 2 );
  67. expect( position1.isEqual( position2 ) ).to.be.false;
  68. } );
  69. } );
  70. describe( 'isBefore', () => {
  71. it( 'should return false for same positions', () => {
  72. const node = new Node();
  73. const position1 = new Position( node, 10 );
  74. const position2 = new Position( node, 10 );
  75. expect( position1.isBefore( position1 ) ).to.be.false;
  76. expect( position1.isBefore( position2 ) ).to.be.false;
  77. expect( position2.isBefore( position1 ) ).to.be.false;
  78. } );
  79. it( 'should return false if no common ancestor is found', () => {
  80. const t1 = new Text( 'foo' );
  81. const t2 = new Text( 'bar' );
  82. const e1 = new Element( 'p', null, [ t1 ] );
  83. const e2 = new Element( 'p', null, [ t2 ] );
  84. const position1 = new Position( e1, 0 );
  85. const position2 = new Position( e2, 1 );
  86. expect( position1.isBefore( position2 ) );
  87. expect( position2.isBefore( position1 ) );
  88. } );
  89. it( 'should return true if position is before in same node', () => {
  90. const node = new Node();
  91. const p1 = new Position( node, 10 );
  92. const p2 = new Position( node, 5 );
  93. expect( p2.isBefore( p1 ) ).to.be.true;
  94. expect( p1.isBefore( p2 ) ).to.be.false;
  95. } );
  96. it( 'should compare positions that have common parent', () => {
  97. const t1 = new Text( 'foo' );
  98. const t2 = new Text( 'bar' );
  99. const root = new Element( 'p', null, [ t1, t2 ] );
  100. const position1 = new Position( t1, 2 );
  101. const position2 = new Position( t2, 0 );
  102. const position3 = new Position( root, 0 );
  103. const position4 = new Position( root, 2 );
  104. const position5 = new Position( t1, 0 );
  105. const position6 = new Position( root, 1 );
  106. expect( position1.isBefore( position2 ) ).to.be.true;
  107. expect( position2.isBefore( position1 ) ).to.be.false;
  108. expect( position3.isBefore( position1 ) ).to.be.true;
  109. expect( position3.isBefore( position2 ) ).to.be.true;
  110. expect( position1.isBefore( position3 ) ).to.be.false;
  111. expect( position2.isBefore( position3 ) ).to.be.false;
  112. expect( position4.isBefore( position1 ) ).to.be.false;
  113. expect( position4.isBefore( position3 ) ).to.be.false;
  114. expect( position3.isBefore( position4 ) ).to.be.true;
  115. expect( position3.isBefore( position5 ) ).to.be.true;
  116. expect( position6.isBefore( position2 ) ).to.be.true;
  117. expect( position1.isBefore( position6 ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'isAfter', () => {
  121. it( 'should return false for same positions', () => {
  122. const node = new Node();
  123. const position1 = new Position( node, 10 );
  124. const position2 = new Position( node, 10 );
  125. expect( position1.isAfter( position1 ) ).to.be.false;
  126. expect( position1.isAfter( position2 ) ).to.be.false;
  127. expect( position2.isAfter( position1 ) ).to.be.false;
  128. } );
  129. it( 'should return false if no common ancestor is found', () => {
  130. const t1 = new Text( 'foo' );
  131. const t2 = new Text( 'bar' );
  132. const e1 = new Element( 'p', null, [ t1 ] );
  133. const e2 = new Element( 'p', null, [ t2 ] );
  134. const position1 = new Position( e1, 0 );
  135. const position2 = new Position( e2, 1 );
  136. expect( position1.isAfter( position2 ) );
  137. expect( position2.isAfter( position1 ) );
  138. } );
  139. it( 'should return true if position is after in same node', () => {
  140. const node = new Node();
  141. const p1 = new Position( node, 10 );
  142. const p2 = new Position( node, 5 );
  143. expect( p2.isAfter( p1 ) ).to.be.false;
  144. expect( p1.isAfter( p2 ) ).to.be.true;
  145. } );
  146. it( 'should compare positions that have common parent', () => {
  147. const t1 = new Text( 'foo' );
  148. const t2 = new Text( 'bar' );
  149. const root = new Element( 'p', null, [ t1, t2 ] );
  150. const position1 = new Position( t1, 2 );
  151. const position2 = new Position( t2, 0 );
  152. const position3 = new Position( root, 0 );
  153. const position4 = new Position( root, 2 );
  154. const position5 = new Position( t1, 0 );
  155. const position6 = new Position( root, 1 );
  156. expect( position1.isAfter( position2 ) ).to.be.false;
  157. expect( position2.isAfter( position1 ) ).to.be.true;
  158. expect( position3.isAfter( position1 ) ).to.be.false;
  159. expect( position3.isAfter( position2 ) ).to.be.false;
  160. expect( position1.isAfter( position3 ) ).to.be.true;
  161. expect( position2.isAfter( position3 ) ).to.be.true;
  162. expect( position4.isAfter( position1 ) ).to.be.true;
  163. expect( position4.isAfter( position3 ) ).to.be.true;
  164. expect( position3.isAfter( position4 ) ).to.be.false;
  165. expect( position5.isAfter( position3 ) ).to.be.true;
  166. expect( position2.isAfter( position6 ) ).to.be.true;
  167. } );
  168. } );
  169. describe( 'compareWith', () => {
  170. it( 'should return SAME if positions are same', () => {
  171. const root = new Node();
  172. const position = new Position( root, 0 );
  173. const compared = new Position( root, 0 );
  174. expect( position.compareWith( compared ) ).to.equal( 'SAME' );
  175. } );
  176. it( 'should return BEFORE if the position is before compared one', () => {
  177. const root = new Node();
  178. const position = new Position( root, 0 );
  179. const compared = new Position( root, 1 );
  180. expect( position.compareWith( compared ) ).to.equal( 'BEFORE' );
  181. } );
  182. it( 'should return AFTER if the position is after compared one', () => {
  183. const root = new Node();
  184. const position = new Position( root, 4 );
  185. const compared = new Position( root, 1 );
  186. expect( position.compareWith( compared ) ).to.equal( 'AFTER' );
  187. } );
  188. it( 'should return DIFFERENT if positions are in different roots', () => {
  189. const root1 = new Node();
  190. const root2 = new Node();
  191. const position = new Position( root1, 4 );
  192. const compared = new Position( root2, 1 );
  193. expect( position.compareWith( compared ) ).to.equal( 'DIFFERENT' );
  194. } );
  195. } );
  196. } );