8
0

selection.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. import count from '/ckeditor5/utils/count.js';
  14. describe( 'Selection', () => {
  15. let selection;
  16. let el;
  17. let range1, range2, range3;
  18. beforeEach( () => {
  19. selection = new Selection();
  20. el = new Element( 'p' );
  21. range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  22. range2 = Range.createFromParentsAndOffsets( el, 1, el, 2 );
  23. range3 = Range.createFromParentsAndOffsets( el, 12, el, 14 );
  24. } );
  25. describe( 'anchor', () => {
  26. it( 'should return null if no ranges in selection', () => {
  27. expect( selection.anchor ).to.be.null;
  28. } );
  29. it( 'should return start of single range in selection', () => {
  30. selection.addRange( range1 );
  31. const anchor = selection.anchor;
  32. expect( anchor.isEqual( range1.start ) ).to.be.true;
  33. expect( anchor ).to.not.equal( range1.start );
  34. } );
  35. it( 'should return end of single range in selection when added as backward', () => {
  36. selection.addRange( range1, true );
  37. const anchor = selection.anchor;
  38. expect( anchor.isEqual( range1.end ) ).to.be.true;
  39. expect( anchor ).to.not.equal( range1.end );
  40. } );
  41. it( 'should get anchor from last inserted range', () => {
  42. selection.addRange( range1 );
  43. selection.addRange( range2 );
  44. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  45. } );
  46. } );
  47. describe( 'focus', () => {
  48. it( 'should return null if no ranges in selection', () => {
  49. expect( selection.focus ).to.be.null;
  50. } );
  51. it( 'should return end of single range in selection', () => {
  52. selection.addRange( range1 );
  53. const focus = selection.focus;
  54. expect( focus.isEqual( range1.end ) ).to.be.true;
  55. } );
  56. it( 'should return start of single range in selection when added as backward', () => {
  57. selection.addRange( range1, true );
  58. const focus = selection.focus;
  59. expect( focus.isEqual( range1.start ) ).to.be.true;
  60. expect( focus ).to.not.equal( range1.start );
  61. } );
  62. it( 'should get focus from last inserted range', () => {
  63. selection.addRange( range1 );
  64. selection.addRange( range2 );
  65. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  66. } );
  67. } );
  68. describe( 'setFocus', () => {
  69. it( 'keeps all existing ranges when no modifications needed', () => {
  70. selection.addRange( range1 );
  71. selection.setFocus( selection.focus );
  72. expect( count( selection.getRanges() ) ).to.equal( 1 );
  73. } );
  74. it( 'throws if there are no ranges in selection', () => {
  75. const endPos = Position.createAt( el, 'end' );
  76. expect( () => {
  77. selection.setFocus( endPos );
  78. } ).to.throw( CKEditorError, /view-selection-setFocus-no-ranges/ );
  79. } );
  80. it( 'modifies existing collapsed selection', () => {
  81. const startPos = Position.createAt( el, 1 );
  82. const endPos = Position.createAt( el, 2 );
  83. selection.collapse( startPos );
  84. selection.setFocus( endPos );
  85. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  86. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  87. } );
  88. it( 'makes existing collapsed selection a backward selection', () => {
  89. const startPos = Position.createAt( el, 1 );
  90. const endPos = Position.createAt( el, 0 );
  91. selection.collapse( startPos );
  92. selection.setFocus( endPos );
  93. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  94. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  95. expect( selection.isBackward ).to.be.true;
  96. } );
  97. it( 'modifies existing non-collapsed selection', () => {
  98. const startPos = Position.createAt( el, 1 );
  99. const endPos = Position.createAt( el, 2 );
  100. const newEndPos = Position.createAt( el, 3 );
  101. selection.addRange( new Range( startPos, endPos ) );
  102. selection.setFocus( newEndPos );
  103. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  104. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  105. } );
  106. it( 'makes existing non-collapsed selection a backward selection', () => {
  107. const startPos = Position.createAt( el, 1 );
  108. const endPos = Position.createAt( el, 2 );
  109. const newEndPos = Position.createAt( el, 0 );
  110. selection.addRange( new Range( startPos, endPos ) );
  111. selection.setFocus( newEndPos );
  112. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  113. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  114. expect( selection.isBackward ).to.be.true;
  115. } );
  116. it( 'makes existing backward selection a forward selection', () => {
  117. const startPos = Position.createAt( el, 1 );
  118. const endPos = Position.createAt( el, 2 );
  119. const newEndPos = Position.createAt( el, 3 );
  120. selection.addRange( new Range( startPos, endPos ), true );
  121. selection.setFocus( newEndPos );
  122. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  123. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  124. expect( selection.isBackward ).to.be.false;
  125. } );
  126. it( 'modifies existing backward selection', () => {
  127. const startPos = Position.createAt( el, 1 );
  128. const endPos = Position.createAt( el, 2 );
  129. const newEndPos = Position.createAt( el, 0 );
  130. selection.addRange( new Range( startPos, endPos ), true );
  131. selection.setFocus( newEndPos );
  132. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  133. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  134. expect( selection.isBackward ).to.be.true;
  135. } );
  136. it( 'modifies only the last range', () => {
  137. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  138. const startPos1 = Position.createAt( el, 4 );
  139. const endPos1 = Position.createAt( el, 5 );
  140. const startPos2 = Position.createAt( el, 1 );
  141. const endPos2 = Position.createAt( el, 2 );
  142. const newEndPos = Position.createAt( el, 0 );
  143. selection.addRange( new Range( startPos1, endPos1 ) );
  144. selection.addRange( new Range( startPos2, endPos2 ) );
  145. selection.setFocus( newEndPos );
  146. const ranges = Array.from( selection.getRanges() );
  147. expect( ranges ).to.have.lengthOf( 2 );
  148. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  149. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  150. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  151. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  152. expect( selection.isBackward ).to.be.true;
  153. } );
  154. it( 'collapses the selection when extending to the anchor', () => {
  155. const startPos = Position.createAt( el, 1 );
  156. const endPos = Position.createAt( el, 2 );
  157. selection.addRange( new Range( startPos, endPos ) );
  158. selection.setFocus( startPos );
  159. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  160. expect( selection.isCollapsed ).to.be.true;
  161. } );
  162. it( 'uses Position.createAt', () => {
  163. const startPos = Position.createAt( el, 1 );
  164. const endPos = Position.createAt( el, 2 );
  165. const newEndPos = Position.createAt( el, 4 );
  166. const spy = sinon.stub( Position, 'createAt', () => newEndPos );
  167. selection.addRange( new Range( startPos, endPos ) );
  168. selection.setFocus( el, 'end' );
  169. expect( spy.calledOnce ).to.be.true;
  170. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  171. Position.createAt.restore();
  172. } );
  173. } );
  174. describe( 'isCollapsed', () => {
  175. it( 'should return true when there is single collapsed range', () => {
  176. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  177. selection.addRange( range );
  178. expect( selection.isCollapsed ).to.be.true;
  179. } );
  180. it( 'should return false when there are multiple ranges', () => {
  181. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  182. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  183. selection.addRange( range1 );
  184. selection.addRange( range2 );
  185. expect( selection.isCollapsed ).to.be.false;
  186. } );
  187. it( 'should return false when there is not collapsed range', () => {
  188. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  189. selection.addRange( range );
  190. expect( selection.isCollapsed ).to.be.false;
  191. } );
  192. } );
  193. describe( 'rangeCount', () => {
  194. it( 'should return proper range count', () => {
  195. expect( selection.rangeCount ).to.equal( 0 );
  196. selection.addRange( range1 );
  197. expect( selection.rangeCount ).to.equal( 1 );
  198. selection.addRange( range2 );
  199. expect( selection.rangeCount ).to.equal( 2 );
  200. } );
  201. } );
  202. describe( 'isBackward', () => {
  203. it( 'is defined by the last added range', () => {
  204. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  205. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  206. selection.addRange( range1, true );
  207. expect( selection ).to.have.property( 'isBackward', true );
  208. selection.addRange( range2 );
  209. expect( selection ).to.have.property( 'isBackward', false );
  210. } );
  211. it( 'is false when last range is collapsed', () => {
  212. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  213. selection.addRange( range, true );
  214. expect( selection.isBackward ).to.be.false;
  215. } );
  216. } );
  217. describe( 'addRange', () => {
  218. it( 'should throw an error when range is invalid', () => {
  219. expect( () => {
  220. selection.addRange( { invalid: 'range' } );
  221. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  222. } );
  223. it( 'should add range to selection ranges', () => {
  224. selection.addRange( range1 );
  225. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  226. } );
  227. it( 'should fire change event', ( done ) => {
  228. selection.once( 'change', () => {
  229. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  230. done();
  231. } );
  232. selection.addRange( range1 );
  233. } );
  234. it( 'should throw when range is intersecting with already added range', () => {
  235. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  236. selection.addRange( range1 );
  237. expect( () => {
  238. selection.addRange( range2 );
  239. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  240. expect( () => {
  241. selection.addRange( range1 );
  242. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  243. } );
  244. } );
  245. describe( 'getRanges', () => {
  246. it( 'should return iterator with copies of all ranges', () => {
  247. selection.addRange( range1 );
  248. selection.addRange( range2 );
  249. const iterable = selection.getRanges();
  250. const ranges = Array.from( iterable );
  251. expect( ranges.length ).to.equal( 2 );
  252. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  253. expect( ranges[ 0 ] ).to.not.equal( range1 );
  254. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  255. expect( ranges[ 1 ] ).to.not.equal( range2 );
  256. } );
  257. } );
  258. describe( 'getFirstRange', () => {
  259. it( 'should return copy of range with first position', () => {
  260. selection.addRange( range1 );
  261. selection.addRange( range2 );
  262. selection.addRange( range3 );
  263. const range = selection.getFirstRange();
  264. expect( range.isEqual( range2 ) ).to.be.true;
  265. expect( range ).to.not.equal( range2 );
  266. } );
  267. it( 'should return null if no ranges are present', () => {
  268. expect( selection.getFirstRange() ).to.be.null;
  269. } );
  270. } );
  271. describe( 'getLastRange', () => {
  272. it( 'should return copy of range with last position', () => {
  273. selection.addRange( range1 );
  274. selection.addRange( range2 );
  275. selection.addRange( range3 );
  276. const range = selection.getLastRange();
  277. expect( range.isEqual( range3 ) ).to.be.true;
  278. expect( range ).to.not.equal( range3 );
  279. } );
  280. it( 'should return null if no ranges are present', () => {
  281. expect( selection.getLastRange() ).to.be.null;
  282. } );
  283. } );
  284. describe( 'getFirstPosition', () => {
  285. it( 'should return copy of first position', () => {
  286. selection.addRange( range1 );
  287. selection.addRange( range2 );
  288. selection.addRange( range3 );
  289. const position = selection.getFirstPosition();
  290. expect( position.isEqual( range2.start ) ).to.be.true;
  291. expect( position ).to.not.equal( range2.start );
  292. } );
  293. it( 'should return null if no ranges are present', () => {
  294. expect( selection.getFirstPosition() ).to.be.null;
  295. } );
  296. } );
  297. describe( 'getLastPosition', () => {
  298. it( 'should return copy of range with last position', () => {
  299. selection.addRange( range1 );
  300. selection.addRange( range2 );
  301. selection.addRange( range3 );
  302. const position = selection.getLastPosition();
  303. expect( position.isEqual( range3.end ) ).to.be.true;
  304. expect( position ).to.not.equal( range3.end );
  305. } );
  306. it( 'should return null if no ranges are present', () => {
  307. expect( selection.getLastPosition() ).to.be.null;
  308. } );
  309. } );
  310. describe( 'isEqual', () => {
  311. it( 'should return true if selections equal', () => {
  312. selection.addRange( range1 );
  313. selection.addRange( range2 );
  314. const otherSelection = new Selection();
  315. otherSelection.addRange( range1 );
  316. otherSelection.addRange( range2 );
  317. expect( selection.isEqual( otherSelection ) ).to.be.true;
  318. } );
  319. it( 'should return true if backward selections equal', () => {
  320. selection.addRange( range1, true );
  321. const otherSelection = new Selection();
  322. otherSelection.addRange( range1, true );
  323. expect( selection.isEqual( otherSelection ) ).to.be.true;
  324. } );
  325. it( 'should return false if ranges count does not equal', () => {
  326. selection.addRange( range1 );
  327. selection.addRange( range2 );
  328. const otherSelection = new Selection();
  329. otherSelection.addRange( range1 );
  330. expect( selection.isEqual( otherSelection ) ).to.be.false;
  331. } );
  332. it( 'should return false if ranges do not equal', () => {
  333. selection.addRange( range1 );
  334. const otherSelection = new Selection();
  335. otherSelection.addRange( range2 );
  336. expect( selection.isEqual( otherSelection ) ).to.be.false;
  337. } );
  338. it( 'should return false if directions do not equal', () => {
  339. selection.addRange( range1 );
  340. const otherSelection = new Selection();
  341. otherSelection.addRange( range1, true );
  342. expect( selection.isEqual( otherSelection ) ).to.be.false;
  343. } );
  344. it( 'should return false if one selection is fake', () => {
  345. const otherSelection = new Selection();
  346. otherSelection.setFake( true );
  347. expect( selection.isEqual( otherSelection ) ).to.be.false;
  348. } );
  349. it( 'should return true if both selection are fake', () => {
  350. const otherSelection = new Selection();
  351. otherSelection.addRange( range1 );
  352. otherSelection.setFake( true );
  353. selection.setFake( true );
  354. selection.addRange( range1 );
  355. expect( selection.isEqual( otherSelection ) ).to.be.true;
  356. } );
  357. it( 'should return false if both selection are fake but have different label', () => {
  358. const otherSelection = new Selection();
  359. otherSelection.addRange( range1 );
  360. otherSelection.setFake( true , { label: 'foo bar baz' } );
  361. selection.setFake( true );
  362. selection.addRange( range1 );
  363. expect( selection.isEqual( otherSelection ) ).to.be.false;
  364. } );
  365. } );
  366. describe( 'removeAllRanges', () => {
  367. it( 'should remove all ranges and fire change event', ( done ) => {
  368. selection.addRange( range1 );
  369. selection.addRange( range2 );
  370. selection.once( 'change', () => {
  371. expect( selection.rangeCount ).to.equal( 0 );
  372. done();
  373. } );
  374. selection.removeAllRanges();
  375. } );
  376. it( 'should do nothing when no ranges are present', () => {
  377. const fireSpy = sinon.spy( selection, 'fire' );
  378. selection.removeAllRanges();
  379. fireSpy.restore();
  380. expect( fireSpy.notCalled ).to.be.true;
  381. } );
  382. } );
  383. describe( 'setRanges', () => {
  384. it( 'should throw an error when range is invalid', () => {
  385. expect( () => {
  386. selection.setRanges( [ { invalid: 'range' } ] );
  387. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  388. } );
  389. it( 'should add ranges and fire change event', ( done ) => {
  390. selection.addRange( range1 );
  391. selection.once( 'change', () => {
  392. expect( selection.rangeCount ).to.equal( 2 );
  393. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  394. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  395. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  396. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  397. done();
  398. } );
  399. selection.setRanges( [ range2, range3 ] );
  400. } );
  401. } );
  402. describe( 'setTo', () => {
  403. it( 'should return true if selections equal', () => {
  404. selection.addRange( range1 );
  405. const otherSelection = new Selection();
  406. otherSelection.addRange( range2 );
  407. otherSelection.addRange( range3, true );
  408. selection.setTo( otherSelection );
  409. expect( selection.rangeCount ).to.equal( 2 );
  410. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  411. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  412. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  413. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  414. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  415. } );
  416. it( 'should fire change event', ( done ) => {
  417. selection.on( 'change', () => {
  418. expect( selection.rangeCount ).to.equal( 1 );
  419. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  420. done();
  421. } );
  422. const otherSelection = new Selection();
  423. otherSelection.addRange( range1 );
  424. selection.setTo( otherSelection );
  425. } );
  426. it( 'should set fake state and label', () => {
  427. const otherSelection = new Selection();
  428. const label = 'foo bar baz';
  429. otherSelection.setFake( true, { label } );
  430. selection.setTo( otherSelection );
  431. expect( selection.isFake ).to.be.true;
  432. expect( selection.fakeSelectionLabel ).to.equal( label );
  433. } );
  434. } );
  435. describe( 'collapse', () => {
  436. beforeEach( () => {
  437. selection.setRanges( [ range1, range2 ] );
  438. } );
  439. it( 'should collapse selection at position', () => {
  440. const position = new Position( el, 4 );
  441. selection.collapse( position );
  442. const range = selection.getFirstRange();
  443. expect( range.start.parent ).to.equal( el );
  444. expect( range.start.offset ).to.equal( 4 );
  445. expect( range.start.isEqual( range.end ) ).to.be.true;
  446. } );
  447. it( 'should collapse selection at node and offset', () => {
  448. const foo = new Text( 'foo' );
  449. const p = new Element( 'p', null, foo );
  450. selection.collapse( foo );
  451. let range = selection.getFirstRange();
  452. expect( range.start.parent ).to.equal( foo );
  453. expect( range.start.offset ).to.equal( 0 );
  454. expect( range.start.isEqual( range.end ) ).to.be.true;
  455. selection.collapse( p, 1 );
  456. range = selection.getFirstRange();
  457. expect( range.start.parent ).to.equal( p );
  458. expect( range.start.offset ).to.equal( 1 );
  459. expect( range.start.isEqual( range.end ) ).to.be.true;
  460. } );
  461. it( 'should collapse selection at node and flag', () => {
  462. const foo = new Text( 'foo' );
  463. const p = new Element( 'p', null, foo );
  464. selection.collapse( foo, 'end' );
  465. let range = selection.getFirstRange();
  466. expect( range.start.parent ).to.equal( foo );
  467. expect( range.start.offset ).to.equal( 3 );
  468. expect( range.start.isEqual( range.end ) ).to.be.true;
  469. selection.collapse( foo, 'before' );
  470. range = selection.getFirstRange();
  471. expect( range.start.parent ).to.equal( p );
  472. expect( range.start.offset ).to.equal( 0 );
  473. expect( range.start.isEqual( range.end ) ).to.be.true;
  474. selection.collapse( foo, 'after' );
  475. range = selection.getFirstRange();
  476. expect( range.start.parent ).to.equal( p );
  477. expect( range.start.offset ).to.equal( 1 );
  478. expect( range.start.isEqual( range.end ) ).to.be.true;
  479. } );
  480. } );
  481. describe( 'collapseToStart', () => {
  482. it( 'should collapse to start position and fire change event', ( done ) => {
  483. selection.setRanges( [ range1, range2, range3 ] );
  484. selection.once( 'change', () => {
  485. expect( selection.rangeCount ).to.equal( 1 );
  486. expect( selection.isCollapsed ).to.be.true;
  487. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  488. done();
  489. } );
  490. selection.collapseToStart();
  491. } );
  492. it( 'should do nothing if no ranges present', () => {
  493. const fireSpy = sinon.spy( selection, 'fire' );
  494. selection.collapseToStart();
  495. fireSpy.restore();
  496. expect( fireSpy.notCalled ).to.be.true;
  497. } );
  498. } );
  499. describe( 'collapseToEnd', () => {
  500. it( 'should collapse to end position and fire change event', ( done ) => {
  501. selection.setRanges( [ range1, range2, range3 ] );
  502. selection.once( 'change', () => {
  503. expect( selection.rangeCount ).to.equal( 1 );
  504. expect( selection.isCollapsed ).to.be.true;
  505. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  506. done();
  507. } );
  508. selection.collapseToEnd();
  509. } );
  510. it( 'should do nothing if no ranges present', () => {
  511. const fireSpy = sinon.spy( selection, 'fire' );
  512. selection.collapseToEnd();
  513. fireSpy.restore();
  514. expect( fireSpy.notCalled ).to.be.true;
  515. } );
  516. } );
  517. describe( 'getEditableElement', () => {
  518. it( 'should return null if no ranges in selection', () => {
  519. expect( selection.editableElement ).to.be.null;
  520. } );
  521. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  522. selection.addRange( range1 );
  523. expect( selection.editableElement ).to.be.null;
  524. } );
  525. it( 'should return EditableElement when selection is placed inside', () => {
  526. const viewDocument = new Document();
  527. const selection = viewDocument.selection;
  528. const root = viewDocument.createRoot( 'div' );
  529. const element = new Element( 'p' );
  530. root.appendChildren( element );
  531. selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  532. expect( selection.editableElement ).to.equal( root );
  533. } );
  534. } );
  535. describe( 'createFromSelection', () => {
  536. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  537. selection.setRanges( [ range1, range2 ], true );
  538. const snapshot = Selection.createFromSelection( selection );
  539. expect( snapshot.isBackward ).to.equal( selection.isBackward );
  540. const selectionRanges = Array.from( selection.getRanges() );
  541. const snapshotRanges = Array.from( snapshot.getRanges() );
  542. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  543. for ( let i = 0; i < selectionRanges.length; i++ ) {
  544. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  545. }
  546. } );
  547. } );
  548. describe( 'isFake', () => {
  549. it( 'should be false for newly created instance', () => {
  550. expect( selection.isFake ).to.be.false;
  551. } );
  552. } );
  553. describe( 'setFake', () => {
  554. it( 'should allow to set selection to fake', () => {
  555. selection.setFake( true );
  556. expect( selection.isFake ).to.be.true;
  557. } );
  558. it( 'should allow to set fake selection label', () => {
  559. const label = 'foo bar baz';
  560. selection.setFake( true, { label } );
  561. expect( selection.fakeSelectionLabel ).to.equal( label );
  562. } );
  563. it( 'should not set label when set to false', () => {
  564. const label = 'foo bar baz';
  565. selection.setFake( false, { label } );
  566. expect( selection.fakeSelectionLabel ).to.equal( '' );
  567. } );
  568. it( 'should reset label when set to false', () => {
  569. const label = 'foo bar baz';
  570. selection.setFake( true, { label } );
  571. selection.setFake( false );
  572. expect( selection.fakeSelectionLabel ).to.equal( '' );
  573. } );
  574. it( 'should fire change event', ( done ) => {
  575. selection.once( 'change', () => {
  576. expect( selection.isFake ).to.be.true;
  577. expect( selection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  578. done();
  579. } );
  580. selection.setFake( true, { label: 'foo bar baz' } );
  581. } );
  582. } );
  583. } );