selection.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. import Selection from '/ckeditor5/engine/view/selection.js';
  7. import Range from '/ckeditor5/engine/view/range.js';
  8. import Document from '/ckeditor5/engine/view/document.js';
  9. import Element from '/ckeditor5/engine/view/element.js';
  10. import Text from '/ckeditor5/engine/view/text.js';
  11. import Position from '/ckeditor5/engine/view/position.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. describe( 'Selection', () => {
  14. let selection;
  15. let el;
  16. let range1, range2, range3;
  17. beforeEach( () => {
  18. selection = new Selection();
  19. el = new Element( 'p' );
  20. range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  21. range2 = Range.createFromParentsAndOffsets( el, 1, el, 2 );
  22. range3 = Range.createFromParentsAndOffsets( el, 12, el, 14 );
  23. } );
  24. describe( 'anchor', () => {
  25. it( 'should return null if no ranges in selection', () => {
  26. expect( selection.anchor ).to.be.null;
  27. } );
  28. it( 'should return start of single range in selection', () => {
  29. selection.addRange( range1 );
  30. const anchor = selection.anchor;
  31. expect( anchor.isEqual( range1.start ) ).to.be.true;
  32. expect( anchor ).to.not.equal( range1.start );
  33. } );
  34. it( 'should return end of single range in selection when added as backward', () => {
  35. selection.addRange( range1, true );
  36. const anchor = selection.anchor;
  37. expect( anchor.isEqual( range1.end ) ).to.be.true;
  38. expect( anchor ).to.not.equal( range1.end );
  39. } );
  40. it( 'should get anchor from last inserted range', () => {
  41. selection.addRange( range1 );
  42. selection.addRange( range2 );
  43. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  44. } );
  45. } );
  46. describe( 'focus', () => {
  47. it( 'should return null if no ranges in selection', () => {
  48. expect( selection.focus ).to.be.null;
  49. } );
  50. it( 'should return end of single range in selection', () => {
  51. selection.addRange( range1 );
  52. const focus = selection.focus;
  53. expect( focus.isEqual( range1.end ) ).to.be.true;
  54. } );
  55. it( 'should return start of single range in selection when added as backward', () => {
  56. selection.addRange( range1, true );
  57. const focus = selection.focus;
  58. expect( focus.isEqual( range1.start ) ).to.be.true;
  59. expect( focus ).to.not.equal( range1.start );
  60. } );
  61. it( 'should get focus from last inserted range', () => {
  62. selection.addRange( range1 );
  63. selection.addRange( range2 );
  64. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  65. } );
  66. } );
  67. describe( 'isCollapsed', () => {
  68. it( 'should return true when there is single collapsed range', () => {
  69. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  70. selection.addRange( range );
  71. expect( selection.isCollapsed ).to.be.true;
  72. } );
  73. it( 'should return false when there are multiple ranges', () => {
  74. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  75. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  76. selection.addRange( range1 );
  77. selection.addRange( range2 );
  78. expect( selection.isCollapsed ).to.be.false;
  79. } );
  80. it( 'should return false when there is not collapsed range', () => {
  81. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  82. selection.addRange( range );
  83. expect( selection.isCollapsed ).to.be.false;
  84. } );
  85. } );
  86. describe( 'rangeCount', () => {
  87. it( 'should return proper range count', () => {
  88. expect( selection.rangeCount ).to.equal( 0 );
  89. selection.addRange( range1 );
  90. expect( selection.rangeCount ).to.equal( 1 );
  91. selection.addRange( range2 );
  92. expect( selection.rangeCount ).to.equal( 2 );
  93. } );
  94. } );
  95. describe( 'isBackward', () => {
  96. it( 'is defined by the last added range', () => {
  97. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  98. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  99. selection.addRange( range1, true );
  100. expect( selection ).to.have.property( 'isBackward', true );
  101. selection.addRange( range2 );
  102. expect( selection ).to.have.property( 'isBackward', false );
  103. } );
  104. it( 'is false when last range is collapsed', () => {
  105. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  106. selection.addRange( range, true );
  107. expect( selection.isBackward ).to.be.false;
  108. } );
  109. } );
  110. describe( 'addRange', () => {
  111. it( 'should throw an error when range is invalid', () => {
  112. expect( () => {
  113. selection.addRange( { invalid: 'range' } );
  114. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  115. } );
  116. it( 'should add range to selection ranges', () => {
  117. selection.addRange( range1 );
  118. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  119. } );
  120. it( 'should fire change event', ( done ) => {
  121. selection.once( 'change', () => {
  122. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  123. done();
  124. } );
  125. selection.addRange( range1 );
  126. } );
  127. it( 'should throw when range is intersecting with already added range', () => {
  128. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  129. selection.addRange( range1 );
  130. expect( () => {
  131. selection.addRange( range2 );
  132. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  133. expect( () => {
  134. selection.addRange( range1 );
  135. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  136. } );
  137. } );
  138. describe( 'getRanges', () => {
  139. it( 'should return iterator with copies of all ranges', () => {
  140. selection.addRange( range1 );
  141. selection.addRange( range2 );
  142. const iterable = selection.getRanges();
  143. const ranges = Array.from( iterable );
  144. expect( ranges.length ).to.equal( 2 );
  145. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  146. expect( ranges[ 0 ] ).to.not.equal( range1 );
  147. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  148. expect( ranges[ 1 ] ).to.not.equal( range2 );
  149. } );
  150. } );
  151. describe( 'getFirstRange', () => {
  152. it( 'should return copy of range with first position', () => {
  153. selection.addRange( range1 );
  154. selection.addRange( range2 );
  155. selection.addRange( range3 );
  156. const range = selection.getFirstRange();
  157. expect( range.isEqual( range2 ) ).to.be.true;
  158. expect( range ).to.not.equal( range2 );
  159. } );
  160. it( 'should return null if no ranges are present', () => {
  161. expect( selection.getFirstRange() ).to.be.null;
  162. } );
  163. } );
  164. describe( 'getLastRange', () => {
  165. it( 'should return copy of range with last position', () => {
  166. selection.addRange( range1 );
  167. selection.addRange( range2 );
  168. selection.addRange( range3 );
  169. const range = selection.getLastRange();
  170. expect( range.isEqual( range3 ) ).to.be.true;
  171. expect( range ).to.not.equal( range3 );
  172. } );
  173. it( 'should return null if no ranges are present', () => {
  174. expect( selection.getLastRange() ).to.be.null;
  175. } );
  176. } );
  177. describe( 'getFirstPosition', () => {
  178. it( 'should return copy of first position', () => {
  179. selection.addRange( range1 );
  180. selection.addRange( range2 );
  181. selection.addRange( range3 );
  182. const position = selection.getFirstPosition();
  183. expect( position.isEqual( range2.start ) ).to.be.true;
  184. expect( position ).to.not.equal( range2.start );
  185. } );
  186. it( 'should return null if no ranges are present', () => {
  187. expect( selection.getFirstPosition() ).to.be.null;
  188. } );
  189. } );
  190. describe( 'getLastPosition', () => {
  191. it( 'should return copy of range with last position', () => {
  192. selection.addRange( range1 );
  193. selection.addRange( range2 );
  194. selection.addRange( range3 );
  195. const position = selection.getLastPosition();
  196. expect( position.isEqual( range3.end ) ).to.be.true;
  197. expect( position ).to.not.equal( range3.end );
  198. } );
  199. it( 'should return null if no ranges are present', () => {
  200. expect( selection.getLastPosition() ).to.be.null;
  201. } );
  202. } );
  203. describe( 'isEqual', () => {
  204. it( 'should return true if selections equal', () => {
  205. selection.addRange( range1 );
  206. selection.addRange( range2 );
  207. const otherSelection = new Selection();
  208. otherSelection.addRange( range1 );
  209. otherSelection.addRange( range2 );
  210. expect( selection.isEqual( otherSelection ) ).to.be.true;
  211. } );
  212. it( 'should return true if backward selections equal', () => {
  213. selection.addRange( range1, true );
  214. const otherSelection = new Selection();
  215. otherSelection.addRange( range1, true );
  216. expect( selection.isEqual( otherSelection ) ).to.be.true;
  217. } );
  218. it( 'should return false if ranges count does not equal', () => {
  219. selection.addRange( range1 );
  220. selection.addRange( range2 );
  221. const otherSelection = new Selection();
  222. otherSelection.addRange( range1 );
  223. expect( selection.isEqual( otherSelection ) ).to.be.false;
  224. } );
  225. it( 'should return false if ranges do not equal', () => {
  226. selection.addRange( range1 );
  227. const otherSelection = new Selection();
  228. otherSelection.addRange( range2 );
  229. expect( selection.isEqual( otherSelection ) ).to.be.false;
  230. } );
  231. it( 'should return false if directions do not equal', () => {
  232. selection.addRange( range1 );
  233. const otherSelection = new Selection();
  234. otherSelection.addRange( range1, true );
  235. expect( selection.isEqual( otherSelection ) ).to.be.false;
  236. } );
  237. } );
  238. describe( 'removeAllRanges', () => {
  239. it( 'should remove all ranges and fire change event', ( done ) => {
  240. selection.addRange( range1 );
  241. selection.addRange( range2 );
  242. selection.once( 'change', () => {
  243. expect( selection.rangeCount ).to.equal( 0 );
  244. done();
  245. } );
  246. selection.removeAllRanges();
  247. } );
  248. it( 'should do nothing when no ranges are present', () => {
  249. const fireSpy = sinon.spy( selection, 'fire' );
  250. selection.removeAllRanges();
  251. fireSpy.restore();
  252. expect( fireSpy.notCalled ).to.be.true;
  253. } );
  254. } );
  255. describe( 'setRanges', () => {
  256. it( 'should throw an error when range is invalid', () => {
  257. expect( () => {
  258. selection.setRanges( [ { invalid: 'range' } ] );
  259. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  260. } );
  261. it( 'should add ranges and fire change event', ( done ) => {
  262. selection.addRange( range1 );
  263. selection.once( 'change', () => {
  264. expect( selection.rangeCount ).to.equal( 2 );
  265. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  266. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  267. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  268. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  269. done();
  270. } );
  271. selection.setRanges( [ range2, range3 ] );
  272. } );
  273. } );
  274. describe( 'setTo', () => {
  275. it( 'should return true if selections equal', () => {
  276. selection.addRange( range1 );
  277. const otherSelection = new Selection();
  278. otherSelection.addRange( range2 );
  279. otherSelection.addRange( range3, true );
  280. selection.setTo( otherSelection );
  281. expect( selection.rangeCount ).to.equal( 2 );
  282. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  283. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  284. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  285. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  286. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  287. } );
  288. it( 'should fire change event', ( done ) => {
  289. selection.on( 'change', () => {
  290. expect( selection.rangeCount ).to.equal( 1 );
  291. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  292. done();
  293. } );
  294. const otherSelection = new Selection();
  295. otherSelection.addRange( range1 );
  296. selection.setTo( otherSelection );
  297. } );
  298. } );
  299. describe( 'collapse', () => {
  300. beforeEach( () => {
  301. selection.setRanges( [ range1, range2 ] );
  302. } );
  303. it( 'should collapse selection at position', () => {
  304. const position = new Position( el, 4 );
  305. selection.collapse( position );
  306. const range = selection.getFirstRange();
  307. expect( range.start.parent ).to.equal( el );
  308. expect( range.start.offset ).to.equal( 4 );
  309. expect( range.start.isEqual( range.end ) ).to.be.true;
  310. } );
  311. it( 'should collapse selection at node and offset', () => {
  312. const foo = new Text( 'foo' );
  313. const p = new Element( 'p', null, foo );
  314. selection.collapse( foo );
  315. let range = selection.getFirstRange();
  316. expect( range.start.parent ).to.equal( foo );
  317. expect( range.start.offset ).to.equal( 0 );
  318. expect( range.start.isEqual( range.end ) ).to.be.true;
  319. selection.collapse( p, 1 );
  320. range = selection.getFirstRange();
  321. expect( range.start.parent ).to.equal( p );
  322. expect( range.start.offset ).to.equal( 1 );
  323. expect( range.start.isEqual( range.end ) ).to.be.true;
  324. } );
  325. it( 'should collapse selection at node and flag', () => {
  326. const foo = new Text( 'foo' );
  327. const p = new Element( 'p', null, foo );
  328. selection.collapse( foo, 'end' );
  329. let range = selection.getFirstRange();
  330. expect( range.start.parent ).to.equal( foo );
  331. expect( range.start.offset ).to.equal( 3 );
  332. expect( range.start.isEqual( range.end ) ).to.be.true;
  333. selection.collapse( foo, 'before' );
  334. range = selection.getFirstRange();
  335. expect( range.start.parent ).to.equal( p );
  336. expect( range.start.offset ).to.equal( 0 );
  337. expect( range.start.isEqual( range.end ) ).to.be.true;
  338. selection.collapse( foo, 'after' );
  339. range = selection.getFirstRange();
  340. expect( range.start.parent ).to.equal( p );
  341. expect( range.start.offset ).to.equal( 1 );
  342. expect( range.start.isEqual( range.end ) ).to.be.true;
  343. } );
  344. } );
  345. describe( 'collapseToStart', () => {
  346. it( 'should collapse to start position and fire change event', ( done ) => {
  347. selection.setRanges( [ range1, range2, range3 ] );
  348. selection.once( 'change', () => {
  349. expect( selection.rangeCount ).to.equal( 1 );
  350. expect( selection.isCollapsed ).to.be.true;
  351. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  352. done();
  353. } );
  354. selection.collapseToStart();
  355. } );
  356. it( 'should do nothing if no ranges present', () => {
  357. const fireSpy = sinon.spy( selection, 'fire' );
  358. selection.collapseToStart();
  359. fireSpy.restore();
  360. expect( fireSpy.notCalled ).to.be.true;
  361. } );
  362. } );
  363. describe( 'collapseToEnd', () => {
  364. it( 'should collapse to end position and fire change event', ( done ) => {
  365. selection.setRanges( [ range1, range2, range3 ] );
  366. selection.once( 'change', () => {
  367. expect( selection.rangeCount ).to.equal( 1 );
  368. expect( selection.isCollapsed ).to.be.true;
  369. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  370. done();
  371. } );
  372. selection.collapseToEnd();
  373. } );
  374. it( 'should do nothing if no ranges present', () => {
  375. const fireSpy = sinon.spy( selection, 'fire' );
  376. selection.collapseToEnd();
  377. fireSpy.restore();
  378. expect( fireSpy.notCalled ).to.be.true;
  379. } );
  380. } );
  381. describe( 'getEditableElement', () => {
  382. it( 'should return null if no ranges in selection', () => {
  383. expect( selection.editableElement ).to.be.null;
  384. } );
  385. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  386. selection.addRange( range1 );
  387. expect( selection.editableElement ).to.be.null;
  388. } );
  389. it( 'should return EditableElement when selection is placed inside', () => {
  390. const viewDocument = new Document();
  391. const selection = viewDocument.selection;
  392. const root = viewDocument.createRoot( 'div' );
  393. const element = new Element( 'p' );
  394. root.appendChildren( element );
  395. selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  396. expect( selection.editableElement ).to.equal( root );
  397. } );
  398. } );
  399. describe( 'createFromSelection', () => {
  400. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  401. selection.setRanges( [ range1, range2 ], true );
  402. const snapshot = Selection.createFromSelection( selection );
  403. expect( snapshot.isBackward ).to.equal( selection.isBackward );
  404. const selectionRanges = Array.from( selection.getRanges() );
  405. const snapshotRanges = Array.from( snapshot.getRanges() );
  406. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  407. for ( let i = 0; i < selectionRanges.length; i++ ) {
  408. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  409. }
  410. } );
  411. } );
  412. } );