position.js 11 KB

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