selection.js 25 KB

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