8
0

position.js 11 KB

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