position.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. import Position from '/ckeditor5/engine/view/position.js';
  7. import Node from '/ckeditor5/engine/view/node.js';
  8. import Element from '/ckeditor5/engine/view/element.js';
  9. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.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 TextProxy from '/ckeditor5/engine/view/textproxy.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. import { parse } from '/tests/engine/_utils/view.js';
  16. describe( 'Position', () => {
  17. const parentMock = {};
  18. describe( 'constructor', () => {
  19. it( 'should create element without attributes', () => {
  20. const elem = new Position( parentMock, 5 );
  21. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  22. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  23. } );
  24. } );
  25. describe( 'nodeBefore', () => {
  26. it( 'should equal to node that is before position', () => {
  27. const b1 = new Element( 'b' );
  28. const el = new Element( 'p', null, [ b1 ] );
  29. const position = new Position( el, 1 );
  30. expect( position.nodeBefore ).to.equal( b1 );
  31. } );
  32. it( 'should equal null if there is no node before', () => {
  33. const b1 = new Element( 'b' );
  34. const el = new Element( 'p', null, [ b1 ] );
  35. const position = new Position( el, 0 );
  36. expect( position.nodeBefore ).to.be.null;
  37. } );
  38. it( 'should equal null if position is located inside text node', () => {
  39. const text = new Text( 'foobar' );
  40. const position = new Position( text, 3 );
  41. expect( position.nodeBefore ).to.be.null;
  42. } );
  43. } );
  44. describe( 'nodeAfter', () => {
  45. it( 'should equal to node that is after position', () => {
  46. const b1 = new Element( 'b' );
  47. const el = new Element( 'p', null, [ b1 ] );
  48. const position = new Position( el, 0 );
  49. expect( position.nodeAfter ).to.equal( b1 );
  50. } );
  51. it( 'should equal null if there is no node before', () => {
  52. const b1 = new Element( 'b' );
  53. const el = new Element( 'p', null, [ b1 ] );
  54. const position = new Position( el, 1 );
  55. expect( position.nodeAfter ).to.be.null;
  56. } );
  57. it( 'should equal null if position is located inside text node', () => {
  58. const text = new Text( 'foobar' );
  59. const position = new Position( text, 3 );
  60. expect( position.nodeAfter ).to.be.null;
  61. } );
  62. } );
  63. describe( 'getShiftedBy', () => {
  64. it( 'returns new instance with shifted offset', () => {
  65. const position = new Position( parentMock, 10 );
  66. const shifted = position.getShiftedBy( 12 );
  67. expect( shifted.offset ).to.equal( 22 );
  68. } );
  69. it( 'accepts negative values', () => {
  70. const position = new Position( parentMock, 10 );
  71. const shifted = position.getShiftedBy( -5 );
  72. expect( shifted.offset ).to.equal( 5 );
  73. } );
  74. it( 'prevents offset to be a negative value', () => {
  75. const position = new Position( parentMock, 10 );
  76. const shifted = position.getShiftedBy( -20 );
  77. expect( shifted.offset ).to.equal( 0 );
  78. } );
  79. } );
  80. describe( 'getRoot', () => {
  81. it( 'should return it\'s parent root', () => {
  82. const foo = new Text( 'foo' );
  83. const docFrag = new DocumentFragment( foo );
  84. expect( new Position( foo, 1 ).root ).to.equal( docFrag );
  85. const bar = new Text( 'bar' );
  86. const p = new Element( 'p', null, bar );
  87. expect( new Position( bar, 2 ).root ).to.equal( p );
  88. expect( new Position( p, 0 ).root ).to.equal( p );
  89. } );
  90. } );
  91. describe( 'getAncestors', () => {
  92. it( 'should return it\'s parent and all it\'s ancestors', () => {
  93. const foo = new Text( 'foo' );
  94. const p = new Element( 'p', null, foo );
  95. const div = new Element( 'div', null, p );
  96. const docFrag = new DocumentFragment( div );
  97. expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ docFrag, div, p, foo ] );
  98. } );
  99. } );
  100. describe( 'createAt', () => {
  101. it( 'should create positions from positions', () => {
  102. const spy = sinon.spy( Position, 'createFromPosition' );
  103. const p = new Element( 'p' );
  104. const position = new Position( p, 0 );
  105. const created = Position.createAt( position );
  106. expect( created.isEqual( position ) ).to.be.true;
  107. expect( spy.calledOnce ).to.be.true;
  108. } );
  109. it( 'should create positions from node and offset', () => {
  110. const foo = new Text( 'foo' );
  111. const p = new Element( 'p', null, foo );
  112. expect( Position.createAt( foo ).parent ).to.equal( foo );
  113. expect( Position.createAt( foo ).offset ).to.equal( 0 );
  114. expect( Position.createAt( foo, 2 ).parent ).to.equal( foo );
  115. expect( Position.createAt( foo, 2 ).offset ).to.equal( 2 );
  116. expect( Position.createAt( p, 1 ).parent ).to.equal( p );
  117. expect( Position.createAt( p, 1 ).offset ).to.equal( 1 );
  118. } );
  119. it( 'should create positions from node and flag', () => {
  120. const foo = new Text( 'foo' );
  121. const p = new Element( 'p', null, foo );
  122. const fooEnd = Position.createAt( foo, 'end' );
  123. const fooBefore = Position.createAt( foo, 'before' );
  124. const fooAfter = Position.createAt( foo, 'after' );
  125. const pEnd = Position.createAt( p, 'end' );
  126. // pBefore and pAfter would throw.
  127. expect( fooEnd.parent ).to.equal( foo );
  128. expect( fooEnd.offset ).to.equal( 3 );
  129. expect( fooBefore.parent ).to.equal( p );
  130. expect( fooBefore.offset ).to.equal( 0 );
  131. expect( fooAfter.parent ).to.equal( p );
  132. expect( fooAfter.offset ).to.equal( 1 );
  133. expect( pEnd.parent ).to.equal( p );
  134. expect( pEnd.offset ).to.equal( 1 );
  135. } );
  136. } );
  137. describe( 'createFromPosition', () => {
  138. it( 'creates new Position with same parent and offset', () => {
  139. const offset = 50;
  140. const position = new Position( parentMock, offset );
  141. const newPosition = Position.createFromPosition( position );
  142. expect( position ).to.not.equal( newPosition );
  143. expect( position.offset ).to.equal( offset );
  144. expect( position.parent ).to.equal( parentMock );
  145. } );
  146. } );
  147. describe( 'isEqual', () => {
  148. it( 'should return true for same object', () => {
  149. const position = new Position( {}, 12 );
  150. expect( position.isEqual( position ) ).to.be.true;
  151. } );
  152. it( 'should return true for positions with same parent and offset', () => {
  153. const parentMock = {};
  154. const position1 = new Position( parentMock, 12 );
  155. const position2 = new Position( parentMock, 12 );
  156. expect( position1.isEqual( position2 ) ).to.be.true;
  157. } );
  158. it( 'should return false for positions with different parents', () => {
  159. const position1 = new Position( {}, 12 );
  160. const position2 = new Position( {}, 12 );
  161. expect( position1.isEqual( position2 ) ).to.be.false;
  162. } );
  163. it( 'should return false for positions with different positions', () => {
  164. const parentMock = {};
  165. const position1 = new Position( parentMock, 12 );
  166. const position2 = new Position( parentMock, 2 );
  167. expect( position1.isEqual( position2 ) ).to.be.false;
  168. } );
  169. } );
  170. describe( 'isBefore', () => {
  171. it( 'should return false for same positions', () => {
  172. const node = new Node();
  173. const position1 = new Position( node, 10 );
  174. const position2 = new Position( node, 10 );
  175. expect( position1.isBefore( position1 ) ).to.be.false;
  176. expect( position1.isBefore( position2 ) ).to.be.false;
  177. expect( position2.isBefore( position1 ) ).to.be.false;
  178. } );
  179. it( 'should return false if no common ancestor is found', () => {
  180. const t1 = new Text( 'foo' );
  181. const t2 = new Text( 'bar' );
  182. const e1 = new Element( 'p', null, [ t1 ] );
  183. const e2 = new Element( 'p', null, [ t2 ] );
  184. const position1 = new Position( e1, 0 );
  185. const position2 = new Position( e2, 1 );
  186. expect( position1.isBefore( position2 ) );
  187. expect( position2.isBefore( position1 ) );
  188. } );
  189. it( 'should return true if position is before in same node', () => {
  190. const node = new Node();
  191. const p1 = new Position( node, 10 );
  192. const p2 = new Position( node, 5 );
  193. expect( p2.isBefore( p1 ) ).to.be.true;
  194. expect( p1.isBefore( p2 ) ).to.be.false;
  195. } );
  196. it( 'should compare positions that have common parent', () => {
  197. const t1 = new Text( 'foo' );
  198. const t2 = new Text( 'bar' );
  199. const root = new Element( 'p', null, [ t1, t2 ] );
  200. const position1 = new Position( t1, 2 );
  201. const position2 = new Position( t2, 0 );
  202. const position3 = new Position( root, 0 );
  203. const position4 = new Position( root, 2 );
  204. const position5 = new Position( t1, 0 );
  205. const position6 = new Position( root, 1 );
  206. expect( position1.isBefore( position2 ) ).to.be.true;
  207. expect( position2.isBefore( position1 ) ).to.be.false;
  208. expect( position3.isBefore( position1 ) ).to.be.true;
  209. expect( position3.isBefore( position2 ) ).to.be.true;
  210. expect( position1.isBefore( position3 ) ).to.be.false;
  211. expect( position2.isBefore( position3 ) ).to.be.false;
  212. expect( position4.isBefore( position1 ) ).to.be.false;
  213. expect( position4.isBefore( position3 ) ).to.be.false;
  214. expect( position3.isBefore( position4 ) ).to.be.true;
  215. expect( position3.isBefore( position5 ) ).to.be.true;
  216. expect( position6.isBefore( position2 ) ).to.be.true;
  217. expect( position1.isBefore( position6 ) ).to.be.true;
  218. } );
  219. } );
  220. describe( 'isAfter', () => {
  221. it( 'should return false for same positions', () => {
  222. const node = new Node();
  223. const position1 = new Position( node, 10 );
  224. const position2 = new Position( node, 10 );
  225. expect( position1.isAfter( position1 ) ).to.be.false;
  226. expect( position1.isAfter( position2 ) ).to.be.false;
  227. expect( position2.isAfter( position1 ) ).to.be.false;
  228. } );
  229. it( 'should return false if no common ancestor is found', () => {
  230. const t1 = new Text( 'foo' );
  231. const t2 = new Text( 'bar' );
  232. const e1 = new Element( 'p', null, [ t1 ] );
  233. const e2 = new Element( 'p', null, [ t2 ] );
  234. const position1 = new Position( e1, 0 );
  235. const position2 = new Position( e2, 1 );
  236. expect( position1.isAfter( position2 ) );
  237. expect( position2.isAfter( position1 ) );
  238. } );
  239. it( 'should return true if position is after in same node', () => {
  240. const node = new Node();
  241. const p1 = new Position( node, 10 );
  242. const p2 = new Position( node, 5 );
  243. expect( p2.isAfter( p1 ) ).to.be.false;
  244. expect( p1.isAfter( p2 ) ).to.be.true;
  245. } );
  246. it( 'should compare positions that have common parent', () => {
  247. const t1 = new Text( 'foo' );
  248. const t2 = new Text( 'bar' );
  249. const root = new Element( 'p', null, [ t1, t2 ] );
  250. const position1 = new Position( t1, 2 );
  251. const position2 = new Position( t2, 0 );
  252. const position3 = new Position( root, 0 );
  253. const position4 = new Position( root, 2 );
  254. const position5 = new Position( t1, 0 );
  255. const position6 = new Position( root, 1 );
  256. expect( position1.isAfter( position2 ) ).to.be.false;
  257. expect( position2.isAfter( position1 ) ).to.be.true;
  258. expect( position3.isAfter( position1 ) ).to.be.false;
  259. expect( position3.isAfter( position2 ) ).to.be.false;
  260. expect( position1.isAfter( position3 ) ).to.be.true;
  261. expect( position2.isAfter( position3 ) ).to.be.true;
  262. expect( position4.isAfter( position1 ) ).to.be.true;
  263. expect( position4.isAfter( position3 ) ).to.be.true;
  264. expect( position3.isAfter( position4 ) ).to.be.false;
  265. expect( position5.isAfter( position3 ) ).to.be.true;
  266. expect( position2.isAfter( position6 ) ).to.be.true;
  267. } );
  268. } );
  269. describe( 'isAtStart', () => {
  270. it( 'should return true if it is at the start of it\'s parent', () => {
  271. const foo = new Text( 'foo' );
  272. const position = new Position( foo, 0 );
  273. expect( position.isAtStart ).to.be.true;
  274. } );
  275. it( 'should return false if it is not at the start of it\'s parent', () => {
  276. const foo = new Text( 'foo' );
  277. const position = new Position( foo, 1 );
  278. expect( position.isAtStart ).to.be.false;
  279. } );
  280. } );
  281. describe( 'isAtEnd', () => {
  282. it( 'should return true if it is at the end of it\'s parent', () => {
  283. const foo = new Text( 'foo' );
  284. const p = new Element( 'p', null, foo );
  285. expect( new Position( foo, 3 ).isAtEnd ).to.be.true;
  286. expect( new Position( p, 1 ).isAtEnd ).to.be.true;
  287. } );
  288. it( 'should return false if it is not at the end of it\'s parent', () => {
  289. const foo = new Text( 'foo' );
  290. const p = new Element( 'p', null, foo );
  291. expect( new Position( foo, 2 ).isAtEnd ).to.be.false;
  292. expect( new Position( p, 0 ).isAtEnd ).to.be.false;
  293. } );
  294. } );
  295. describe( 'compareWith', () => {
  296. it( 'should return same if positions are same', () => {
  297. const root = new Node();
  298. const position = new Position( root, 0 );
  299. const compared = new Position( root, 0 );
  300. expect( position.compareWith( compared ) ).to.equal( 'same' );
  301. } );
  302. it( 'should return before if the position is before compared one', () => {
  303. const root = new Node();
  304. const position = new Position( root, 0 );
  305. const compared = new Position( root, 1 );
  306. expect( position.compareWith( compared ) ).to.equal( 'before' );
  307. } );
  308. it( 'should return after if the position is after compared one', () => {
  309. const root = new Node();
  310. const position = new Position( root, 4 );
  311. const compared = new Position( root, 1 );
  312. expect( position.compareWith( compared ) ).to.equal( 'after' );
  313. } );
  314. it( 'should return different if positions are in different roots', () => {
  315. const root1 = new Node();
  316. const root2 = new Node();
  317. const position = new Position( root1, 4 );
  318. const compared = new Position( root2, 1 );
  319. expect( position.compareWith( compared ) ).to.equal( 'different' );
  320. } );
  321. } );
  322. describe( 'createBefore', () => {
  323. it( 'should throw error if one try to create positions before root', () => {
  324. expect( () => {
  325. Position.createBefore( parse( '<p></p>' ) );
  326. } ).to.throw( CKEditorError, /view-position-before-root/ );
  327. } );
  328. it( 'should create positions before `Node`', () => {
  329. const { selection } = parse( '<p>[]<b></b></p>' );
  330. const position = selection.getFirstPosition();
  331. const nodeAfter = position.nodeAfter;
  332. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  333. } );
  334. it( 'should create positions before `TextProxy`', () => {
  335. const text = new Text( 'abc' );
  336. const textProxy = new TextProxy( text, 1, 1 );
  337. const position = new Position( text, 1 );
  338. expect( Position.createBefore( textProxy ) ).deep.equal( position );
  339. } );
  340. } );
  341. describe( 'createAfter', () => {
  342. it( 'should throw error if one try to create positions after root', () => {
  343. expect( () => {
  344. Position.createAfter( parse( '<p></p>' ) );
  345. } ).to.throw( CKEditorError, /view-position-after-root/ );
  346. } );
  347. it( 'should create positions after `Node`', () => {
  348. const { selection } = parse( '<p><b></b>[]</p>' );
  349. const position = selection.getFirstPosition();
  350. const nodeBefore = position.nodeBefore;
  351. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  352. } );
  353. it( 'should create positions after `TextProxy`', () => {
  354. const text = new Text( 'abcd' );
  355. const textProxy = new TextProxy( text, 1, 2 );
  356. const position = new Position( text, 3 );
  357. expect( Position.createAfter( textProxy ) ).deep.equal( position );
  358. } );
  359. } );
  360. describe( 'getEditableElement', () => {
  361. it( 'should return null if position is not inside EditableElement', () => {
  362. const position = new Position( new Element( 'p' ), 0 );
  363. expect( position.editableElement ).to.be.null;
  364. } );
  365. it( 'should return EditableElement when position is placed inside', () => {
  366. const document = new Document();
  367. const p = new Element( 'p' );
  368. const editable = new EditableElement( document, 'div', null, p );
  369. const position = new Position( p, 0 );
  370. expect( position.editableElement ).to.equal( editable );
  371. } );
  372. } );
  373. } );