selection.js 25 KB

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