selection.js 25 KB

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