8
0

position.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import { parse } from '/tests/engine/_utils/view.js';
  13. describe( 'Position', () => {
  14. const parentMock = {};
  15. describe( 'constructor', () => {
  16. it( 'should create element without attributes', () => {
  17. const elem = new Position( parentMock, 5 );
  18. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  19. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  20. } );
  21. } );
  22. describe( 'nodeBefore', () => {
  23. it( 'should equal to node that is before position', () => {
  24. const b1 = new Element( 'b' );
  25. const el = new Element( 'p', null, [ b1 ] );
  26. const position = new Position( el, 1 );
  27. expect( position.nodeBefore ).to.equal( b1 );
  28. } );
  29. it( 'should equal null if there is no node before', () => {
  30. const b1 = new Element( 'b' );
  31. const el = new Element( 'p', null, [ b1 ] );
  32. const position = new Position( el, 0 );
  33. expect( position.nodeBefore ).to.be.null;
  34. } );
  35. it( 'should equal null if position is located inside text node', () => {
  36. const text = new Text( 'foobar' );
  37. const position = new Position( text, 3 );
  38. expect( position.nodeBefore ).to.be.null;
  39. } );
  40. } );
  41. describe( 'nodeAfter', () => {
  42. it( 'should equal to node that is after position', () => {
  43. const b1 = new Element( 'b' );
  44. const el = new Element( 'p', null, [ b1 ] );
  45. const position = new Position( el, 0 );
  46. expect( position.nodeAfter ).to.equal( b1 );
  47. } );
  48. it( 'should equal null if there is no node before', () => {
  49. const b1 = new Element( 'b' );
  50. const el = new Element( 'p', null, [ b1 ] );
  51. const position = new Position( el, 1 );
  52. expect( position.nodeAfter ).to.be.null;
  53. } );
  54. it( 'should equal null if position is located inside text node', () => {
  55. const text = new Text( 'foobar' );
  56. const position = new Position( text, 3 );
  57. expect( position.nodeAfter ).to.be.null;
  58. } );
  59. } );
  60. describe( 'getShiftedBy', () => {
  61. it( 'returns new instance with shifted offset', () => {
  62. const position = new Position( parentMock, 10 );
  63. const shifted = position.getShiftedBy( 12 );
  64. expect( shifted.offset ).to.equal( 22 );
  65. } );
  66. it( 'accepts negative values', () => {
  67. const position = new Position( parentMock, 10 );
  68. const shifted = position.getShiftedBy( -5 );
  69. expect( shifted.offset ).to.equal( 5 );
  70. } );
  71. it( 'prevents offset to be a negative value', () => {
  72. const position = new Position( parentMock, 10 );
  73. const shifted = position.getShiftedBy( -20 );
  74. expect( shifted.offset ).to.equal( 0 );
  75. } );
  76. } );
  77. describe( 'createFromPosition', () => {
  78. it( 'creates new Position with same parent and offset', () => {
  79. const offset = 50;
  80. const position = new Position( parentMock, offset );
  81. const newPosition = Position.createFromPosition( position );
  82. expect( position ).to.not.equal( newPosition );
  83. expect( position.offset ).to.equal( offset );
  84. expect( position.parent ).to.equal( parentMock );
  85. } );
  86. } );
  87. describe( 'isEqual', () => {
  88. it( 'should return true for same object', () => {
  89. const position = new Position( {}, 12 );
  90. expect( position.isEqual( position ) ).to.be.true;
  91. } );
  92. it( 'should return true for positions with same parent and offset', () => {
  93. const parentMock = {};
  94. const position1 = new Position( parentMock, 12 );
  95. const position2 = new Position( parentMock, 12 );
  96. expect( position1.isEqual( position2 ) ).to.be.true;
  97. } );
  98. it( 'should return false for positions with different parents', () => {
  99. const position1 = new Position( {}, 12 );
  100. const position2 = new Position( {}, 12 );
  101. expect( position1.isEqual( position2 ) ).to.be.false;
  102. } );
  103. it( 'should return false for positions with different positions', () => {
  104. const parentMock = {};
  105. const position1 = new Position( parentMock, 12 );
  106. const position2 = new Position( parentMock, 2 );
  107. expect( position1.isEqual( position2 ) ).to.be.false;
  108. } );
  109. } );
  110. describe( 'isBefore', () => {
  111. it( 'should return false for same positions', () => {
  112. const node = new Node();
  113. const position1 = new Position( node, 10 );
  114. const position2 = new Position( node, 10 );
  115. expect( position1.isBefore( position1 ) ).to.be.false;
  116. expect( position1.isBefore( position2 ) ).to.be.false;
  117. expect( position2.isBefore( position1 ) ).to.be.false;
  118. } );
  119. it( 'should return false if no common ancestor is found', () => {
  120. const t1 = new Text( 'foo' );
  121. const t2 = new Text( 'bar' );
  122. const e1 = new Element( 'p', null, [ t1 ] );
  123. const e2 = new Element( 'p', null, [ t2 ] );
  124. const position1 = new Position( e1, 0 );
  125. const position2 = new Position( e2, 1 );
  126. expect( position1.isBefore( position2 ) );
  127. expect( position2.isBefore( position1 ) );
  128. } );
  129. it( 'should return true if position is before in same node', () => {
  130. const node = new Node();
  131. const p1 = new Position( node, 10 );
  132. const p2 = new Position( node, 5 );
  133. expect( p2.isBefore( p1 ) ).to.be.true;
  134. expect( p1.isBefore( p2 ) ).to.be.false;
  135. } );
  136. it( 'should compare positions that have common parent', () => {
  137. const t1 = new Text( 'foo' );
  138. const t2 = new Text( 'bar' );
  139. const root = new Element( 'p', null, [ t1, t2 ] );
  140. const position1 = new Position( t1, 2 );
  141. const position2 = new Position( t2, 0 );
  142. const position3 = new Position( root, 0 );
  143. const position4 = new Position( root, 2 );
  144. const position5 = new Position( t1, 0 );
  145. const position6 = new Position( root, 1 );
  146. expect( position1.isBefore( position2 ) ).to.be.true;
  147. expect( position2.isBefore( position1 ) ).to.be.false;
  148. expect( position3.isBefore( position1 ) ).to.be.true;
  149. expect( position3.isBefore( position2 ) ).to.be.true;
  150. expect( position1.isBefore( position3 ) ).to.be.false;
  151. expect( position2.isBefore( position3 ) ).to.be.false;
  152. expect( position4.isBefore( position1 ) ).to.be.false;
  153. expect( position4.isBefore( position3 ) ).to.be.false;
  154. expect( position3.isBefore( position4 ) ).to.be.true;
  155. expect( position3.isBefore( position5 ) ).to.be.true;
  156. expect( position6.isBefore( position2 ) ).to.be.true;
  157. expect( position1.isBefore( position6 ) ).to.be.true;
  158. } );
  159. } );
  160. describe( 'isAfter', () => {
  161. it( 'should return false for same positions', () => {
  162. const node = new Node();
  163. const position1 = new Position( node, 10 );
  164. const position2 = new Position( node, 10 );
  165. expect( position1.isAfter( position1 ) ).to.be.false;
  166. expect( position1.isAfter( position2 ) ).to.be.false;
  167. expect( position2.isAfter( position1 ) ).to.be.false;
  168. } );
  169. it( 'should return false if no common ancestor is found', () => {
  170. const t1 = new Text( 'foo' );
  171. const t2 = new Text( 'bar' );
  172. const e1 = new Element( 'p', null, [ t1 ] );
  173. const e2 = new Element( 'p', null, [ t2 ] );
  174. const position1 = new Position( e1, 0 );
  175. const position2 = new Position( e2, 1 );
  176. expect( position1.isAfter( position2 ) );
  177. expect( position2.isAfter( position1 ) );
  178. } );
  179. it( 'should return true if position is after in same node', () => {
  180. const node = new Node();
  181. const p1 = new Position( node, 10 );
  182. const p2 = new Position( node, 5 );
  183. expect( p2.isAfter( p1 ) ).to.be.false;
  184. expect( p1.isAfter( p2 ) ).to.be.true;
  185. } );
  186. it( 'should compare positions that have common parent', () => {
  187. const t1 = new Text( 'foo' );
  188. const t2 = new Text( 'bar' );
  189. const root = new Element( 'p', null, [ t1, t2 ] );
  190. const position1 = new Position( t1, 2 );
  191. const position2 = new Position( t2, 0 );
  192. const position3 = new Position( root, 0 );
  193. const position4 = new Position( root, 2 );
  194. const position5 = new Position( t1, 0 );
  195. const position6 = new Position( root, 1 );
  196. expect( position1.isAfter( position2 ) ).to.be.false;
  197. expect( position2.isAfter( position1 ) ).to.be.true;
  198. expect( position3.isAfter( position1 ) ).to.be.false;
  199. expect( position3.isAfter( position2 ) ).to.be.false;
  200. expect( position1.isAfter( position3 ) ).to.be.true;
  201. expect( position2.isAfter( position3 ) ).to.be.true;
  202. expect( position4.isAfter( position1 ) ).to.be.true;
  203. expect( position4.isAfter( position3 ) ).to.be.true;
  204. expect( position3.isAfter( position4 ) ).to.be.false;
  205. expect( position5.isAfter( position3 ) ).to.be.true;
  206. expect( position2.isAfter( position6 ) ).to.be.true;
  207. } );
  208. } );
  209. describe( 'compareWith', () => {
  210. it( 'should return SAME if positions are same', () => {
  211. const root = new Node();
  212. const position = new Position( root, 0 );
  213. const compared = new Position( root, 0 );
  214. expect( position.compareWith( compared ) ).to.equal( 'SAME' );
  215. } );
  216. it( 'should return BEFORE if the position is before compared one', () => {
  217. const root = new Node();
  218. const position = new Position( root, 0 );
  219. const compared = new Position( root, 1 );
  220. expect( position.compareWith( compared ) ).to.equal( 'BEFORE' );
  221. } );
  222. it( 'should return AFTER if the position is after compared one', () => {
  223. const root = new Node();
  224. const position = new Position( root, 4 );
  225. const compared = new Position( root, 1 );
  226. expect( position.compareWith( compared ) ).to.equal( 'AFTER' );
  227. } );
  228. it( 'should return DIFFERENT if positions are in different roots', () => {
  229. const root1 = new Node();
  230. const root2 = new Node();
  231. const position = new Position( root1, 4 );
  232. const compared = new Position( root2, 1 );
  233. expect( position.compareWith( compared ) ).to.equal( 'DIFFERENT' );
  234. } );
  235. } );
  236. describe( 'createBefore', () => {
  237. it( 'should create positions before nodes', () => {
  238. const { selection } = parse( '<p>[]<b></b></p>' );
  239. const position = selection.getFirstPosition();
  240. const nodeAfter = position.nodeAfter;
  241. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  242. } );
  243. it( 'should throw error if one try to create positions before root', () => {
  244. expect( () => {
  245. Position.createBefore( parse( '<p></p>' ) );
  246. } ).to.throw( CKEditorError, /position-before-root/ );
  247. } );
  248. } );
  249. describe( 'createAfter', () => {
  250. it( 'should create positions after nodes', () => {
  251. const { selection } = parse( '<p><b></b>[]</p>' );
  252. const position = selection.getFirstPosition();
  253. const nodeBefore = position.nodeBefore;
  254. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  255. } );
  256. it( 'should throw error if one try to create positions after root', () => {
  257. expect( () => {
  258. Position.createAfter( parse( '<p></p>' ) );
  259. } ).to.throw( CKEditorError, /position-after-root/ );
  260. } );
  261. } );
  262. } );