selection.js 25 KB

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