selection.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. const text = new Text( 'xxxxxxxxxxxxxxxxxxxx' );
  18. el = new Element( 'p', null, text );
  19. selection = new Selection();
  20. range1 = Range.createFromParentsAndOffsets( text, 5, text, 10 );
  21. range2 = Range.createFromParentsAndOffsets( text, 1, text, 2 );
  22. range3 = Range.createFromParentsAndOffsets( text, 12, text, 14 );
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'should be able to create an empty selection', () => {
  26. const selection = new Selection();
  27. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  28. } );
  29. it( 'should be able to create a selection from the given ranges', () => {
  30. const ranges = [ range1, range2, range3 ];
  31. const selection = new Selection( ranges );
  32. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  33. } );
  34. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  35. const ranges = [ range1, range2, range3 ];
  36. const selection = new Selection( ranges, true );
  37. expect( selection.isBackward ).to.be.true;
  38. } );
  39. } );
  40. describe( 'anchor', () => {
  41. it( 'should return null if no ranges in selection', () => {
  42. expect( selection.anchor ).to.be.null;
  43. } );
  44. it( 'should return start of single range in selection', () => {
  45. selection.addRange( range1 );
  46. const anchor = selection.anchor;
  47. expect( anchor.isEqual( range1.start ) ).to.be.true;
  48. expect( anchor ).to.not.equal( range1.start );
  49. } );
  50. it( 'should return end of single range in selection when added as backward', () => {
  51. selection.addRange( range1, true );
  52. const anchor = selection.anchor;
  53. expect( anchor.isEqual( range1.end ) ).to.be.true;
  54. expect( anchor ).to.not.equal( range1.end );
  55. } );
  56. it( 'should get anchor from last inserted range', () => {
  57. selection.addRange( range1 );
  58. selection.addRange( range2 );
  59. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  60. } );
  61. } );
  62. describe( 'focus', () => {
  63. it( 'should return null if no ranges in selection', () => {
  64. expect( selection.focus ).to.be.null;
  65. } );
  66. it( 'should return end of single range in selection', () => {
  67. selection.addRange( range1 );
  68. const focus = selection.focus;
  69. expect( focus.isEqual( range1.end ) ).to.be.true;
  70. } );
  71. it( 'should return start of single range in selection when added as backward', () => {
  72. selection.addRange( range1, true );
  73. const focus = selection.focus;
  74. expect( focus.isEqual( range1.start ) ).to.be.true;
  75. expect( focus ).to.not.equal( range1.start );
  76. } );
  77. it( 'should get focus from last inserted range', () => {
  78. selection.addRange( range1 );
  79. selection.addRange( range2 );
  80. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  81. } );
  82. } );
  83. describe( 'setFocus', () => {
  84. it( 'keeps all existing ranges when no modifications needed', () => {
  85. selection.addRange( range1 );
  86. selection.setFocus( selection.focus );
  87. expect( count( selection.getRanges() ) ).to.equal( 1 );
  88. } );
  89. it( 'throws if there are no ranges in selection', () => {
  90. const endPos = Position.createAt( el, 'end' );
  91. expect( () => {
  92. selection.setFocus( endPos );
  93. } ).to.throw( CKEditorError, /view-selection-setFocus-no-ranges/ );
  94. } );
  95. it( 'modifies existing collapsed selection', () => {
  96. const startPos = Position.createAt( el, 1 );
  97. const endPos = Position.createAt( el, 2 );
  98. selection.collapse( startPos );
  99. selection.setFocus( endPos );
  100. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  101. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  102. } );
  103. it( 'makes existing collapsed selection a backward selection', () => {
  104. const startPos = Position.createAt( el, 1 );
  105. const endPos = Position.createAt( el, 0 );
  106. selection.collapse( startPos );
  107. selection.setFocus( endPos );
  108. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  109. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  110. expect( selection.isBackward ).to.be.true;
  111. } );
  112. it( 'modifies existing non-collapsed selection', () => {
  113. const startPos = Position.createAt( el, 1 );
  114. const endPos = Position.createAt( el, 2 );
  115. const newEndPos = Position.createAt( el, 3 );
  116. selection.addRange( new Range( startPos, endPos ) );
  117. selection.setFocus( newEndPos );
  118. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  119. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  120. } );
  121. it( 'makes existing non-collapsed selection a backward selection', () => {
  122. const startPos = Position.createAt( el, 1 );
  123. const endPos = Position.createAt( el, 2 );
  124. const newEndPos = Position.createAt( el, 0 );
  125. selection.addRange( new Range( startPos, endPos ) );
  126. selection.setFocus( newEndPos );
  127. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  128. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  129. expect( selection.isBackward ).to.be.true;
  130. } );
  131. it( 'makes existing backward selection a forward selection', () => {
  132. const startPos = Position.createAt( el, 1 );
  133. const endPos = Position.createAt( el, 2 );
  134. const newEndPos = Position.createAt( el, 3 );
  135. selection.addRange( new Range( startPos, endPos ), true );
  136. selection.setFocus( newEndPos );
  137. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  138. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  139. expect( selection.isBackward ).to.be.false;
  140. } );
  141. it( 'modifies existing backward selection', () => {
  142. const startPos = Position.createAt( el, 1 );
  143. const endPos = Position.createAt( el, 2 );
  144. const newEndPos = Position.createAt( el, 0 );
  145. selection.addRange( new Range( startPos, endPos ), true );
  146. selection.setFocus( newEndPos );
  147. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  148. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  149. expect( selection.isBackward ).to.be.true;
  150. } );
  151. it( 'modifies only the last range', () => {
  152. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  153. const startPos1 = Position.createAt( el, 4 );
  154. const endPos1 = Position.createAt( el, 5 );
  155. const startPos2 = Position.createAt( el, 1 );
  156. const endPos2 = Position.createAt( el, 2 );
  157. const newEndPos = Position.createAt( el, 0 );
  158. selection.addRange( new Range( startPos1, endPos1 ) );
  159. selection.addRange( new Range( startPos2, endPos2 ) );
  160. selection.setFocus( newEndPos );
  161. const ranges = Array.from( selection.getRanges() );
  162. expect( ranges ).to.have.lengthOf( 2 );
  163. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  164. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  165. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  166. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  167. expect( selection.isBackward ).to.be.true;
  168. } );
  169. it( 'collapses the selection when extending to the anchor', () => {
  170. const startPos = Position.createAt( el, 1 );
  171. const endPos = Position.createAt( el, 2 );
  172. selection.addRange( new Range( startPos, endPos ) );
  173. selection.setFocus( startPos );
  174. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  175. expect( selection.isCollapsed ).to.be.true;
  176. } );
  177. it( 'uses Position.createAt', () => {
  178. const startPos = Position.createAt( el, 1 );
  179. const endPos = Position.createAt( el, 2 );
  180. const newEndPos = Position.createAt( el, 4 );
  181. const spy = sinon.stub( Position, 'createAt' ).returns( newEndPos );
  182. selection.addRange( new Range( startPos, endPos ) );
  183. selection.setFocus( el, 'end' );
  184. expect( spy.calledOnce ).to.be.true;
  185. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  186. Position.createAt.restore();
  187. } );
  188. } );
  189. describe( 'isCollapsed', () => {
  190. it( 'should return true when there is single collapsed range', () => {
  191. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  192. selection.addRange( range );
  193. expect( selection.isCollapsed ).to.be.true;
  194. } );
  195. it( 'should return false when there are multiple ranges', () => {
  196. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  197. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  198. selection.addRange( range1 );
  199. selection.addRange( range2 );
  200. expect( selection.isCollapsed ).to.be.false;
  201. } );
  202. it( 'should return false when there is not collapsed range', () => {
  203. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  204. selection.addRange( range );
  205. expect( selection.isCollapsed ).to.be.false;
  206. } );
  207. } );
  208. describe( 'rangeCount', () => {
  209. it( 'should return proper range count', () => {
  210. expect( selection.rangeCount ).to.equal( 0 );
  211. selection.addRange( range1 );
  212. expect( selection.rangeCount ).to.equal( 1 );
  213. selection.addRange( range2 );
  214. expect( selection.rangeCount ).to.equal( 2 );
  215. } );
  216. } );
  217. describe( 'isBackward', () => {
  218. it( 'is defined by the last added range', () => {
  219. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  220. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  221. selection.addRange( range1, true );
  222. expect( selection ).to.have.property( 'isBackward', true );
  223. selection.addRange( range2 );
  224. expect( selection ).to.have.property( 'isBackward', false );
  225. } );
  226. it( 'is false when last range is collapsed', () => {
  227. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  228. selection.addRange( range, true );
  229. expect( selection.isBackward ).to.be.false;
  230. } );
  231. } );
  232. describe( 'addRange', () => {
  233. it( 'should throw an error when range is invalid', () => {
  234. expect( () => {
  235. selection.addRange( { invalid: 'range' } );
  236. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  237. } );
  238. it( 'should add range to selection ranges', () => {
  239. selection.addRange( range1 );
  240. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  241. } );
  242. it( 'should fire change event', done => {
  243. selection.once( 'change', () => {
  244. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  245. done();
  246. } );
  247. selection.addRange( range1 );
  248. } );
  249. it( 'should throw when range is intersecting with already added range', () => {
  250. const text = el.getChild( 0 );
  251. const range2 = Range.createFromParentsAndOffsets( text, 7, text, 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( 'isSimilar', () => {
  389. it( 'should return true if selections equal', () => {
  390. selection.addRange( range1 );
  391. selection.addRange( range2 );
  392. const otherSelection = new Selection();
  393. otherSelection.addRange( range1 );
  394. otherSelection.addRange( range2 );
  395. expect( selection.isSimilar( otherSelection ) ).to.be.true;
  396. } );
  397. it( 'should return false if ranges count does not equal', () => {
  398. selection.addRange( range1 );
  399. selection.addRange( range2 );
  400. const otherSelection = new Selection();
  401. otherSelection.addRange( range1 );
  402. expect( selection.isSimilar( otherSelection ) ).to.be.false;
  403. } );
  404. it( 'should return false if trimmed ranges (other than the last added one) are not equal', () => {
  405. selection.addRange( range1 );
  406. selection.addRange( range3 );
  407. const otherSelection = new Selection();
  408. otherSelection.addRange( range2 );
  409. otherSelection.addRange( range3 );
  410. expect( selection.isSimilar( otherSelection ) ).to.be.false;
  411. } );
  412. it( 'should return false if directions are not equal', () => {
  413. selection.addRange( range1 );
  414. const otherSelection = new Selection();
  415. otherSelection.addRange( range1, true );
  416. expect( selection.isSimilar( otherSelection ) ).to.be.false;
  417. } );
  418. it( 'should return true if both selections are empty', () => {
  419. const otherSelection = new Selection();
  420. expect( selection.isSimilar( otherSelection ) ).to.be.true;
  421. } );
  422. it( 'should return true if all ranges trimmed from both selections are equal', () => {
  423. const view = parse(
  424. '<container:p><attribute:span></attribute:span></container:p>' +
  425. '<container:p><attribute:span>xx</attribute:span></container:p>'
  426. );
  427. const p1 = view.getChild( 0 );
  428. const p2 = view.getChild( 1 );
  429. const span1 = p1.getChild( 0 );
  430. const span2 = p2.getChild( 0 );
  431. // <p>[<span>{]</span>}</p><p>[<span>{xx}</span>]</p>
  432. const rangeA1 = Range.createFromParentsAndOffsets( p1, 0, span1, 0 );
  433. const rangeB1 = Range.createFromParentsAndOffsets( span1, 0, p1, 1 );
  434. const rangeA2 = Range.createFromParentsAndOffsets( p2, 0, p2, 1 );
  435. const rangeB2 = Range.createFromParentsAndOffsets( span2, 0, span2, 1 );
  436. selection.addRange( rangeA1 );
  437. selection.addRange( rangeA2 );
  438. const otherSelection = new Selection();
  439. otherSelection.addRange( rangeB2 );
  440. otherSelection.addRange( rangeB1 );
  441. expect( selection.isSimilar( otherSelection ) ).to.be.true;
  442. expect( otherSelection.isSimilar( selection ) ).to.be.true;
  443. expect( selection.isEqual( otherSelection ) ).to.be.false;
  444. expect( otherSelection.isEqual( selection ) ).to.be.false;
  445. } );
  446. } );
  447. describe( 'removeAllRanges', () => {
  448. it( 'should remove all ranges and fire change event', done => {
  449. selection.addRange( range1 );
  450. selection.addRange( range2 );
  451. selection.once( 'change', () => {
  452. expect( selection.rangeCount ).to.equal( 0 );
  453. done();
  454. } );
  455. selection.removeAllRanges();
  456. } );
  457. it( 'should do nothing when no ranges are present', () => {
  458. const fireSpy = sinon.spy( selection, 'fire' );
  459. selection.removeAllRanges();
  460. fireSpy.restore();
  461. expect( fireSpy.notCalled ).to.be.true;
  462. } );
  463. } );
  464. describe( 'setRanges', () => {
  465. it( 'should throw an error when range is invalid', () => {
  466. expect( () => {
  467. selection.setRanges( [ { invalid: 'range' } ] );
  468. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  469. } );
  470. it( 'should add ranges and fire change event', done => {
  471. selection.addRange( range1 );
  472. selection.once( 'change', () => {
  473. expect( selection.rangeCount ).to.equal( 2 );
  474. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  475. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  476. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  477. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  478. done();
  479. } );
  480. selection.setRanges( [ range2, range3 ] );
  481. } );
  482. } );
  483. describe( 'setTo', () => {
  484. it( 'should return true if selections equal', () => {
  485. selection.addRange( range1 );
  486. const otherSelection = new Selection();
  487. otherSelection.addRange( range2 );
  488. otherSelection.addRange( range3, true );
  489. selection.setTo( otherSelection );
  490. expect( selection.rangeCount ).to.equal( 2 );
  491. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  492. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  493. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  494. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  495. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  496. } );
  497. it( 'should fire change event', done => {
  498. selection.on( 'change', () => {
  499. expect( selection.rangeCount ).to.equal( 1 );
  500. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  501. done();
  502. } );
  503. const otherSelection = new Selection();
  504. otherSelection.addRange( range1 );
  505. selection.setTo( otherSelection );
  506. } );
  507. it( 'should set fake state and label', () => {
  508. const otherSelection = new Selection();
  509. const label = 'foo bar baz';
  510. otherSelection.setFake( true, { label } );
  511. selection.setTo( otherSelection );
  512. expect( selection.isFake ).to.be.true;
  513. expect( selection.fakeSelectionLabel ).to.equal( label );
  514. } );
  515. } );
  516. describe( 'collapse', () => {
  517. beforeEach( () => {
  518. selection.setRanges( [ range1, range2 ] );
  519. } );
  520. it( 'should collapse selection at position', () => {
  521. const position = new Position( el, 4 );
  522. selection.collapse( position );
  523. const range = selection.getFirstRange();
  524. expect( range.start.parent ).to.equal( el );
  525. expect( range.start.offset ).to.equal( 4 );
  526. expect( range.start.isEqual( range.end ) ).to.be.true;
  527. } );
  528. it( 'should collapse selection at node and offset', () => {
  529. const foo = new Text( 'foo' );
  530. const p = new Element( 'p', null, foo );
  531. selection.collapse( foo );
  532. let range = selection.getFirstRange();
  533. expect( range.start.parent ).to.equal( foo );
  534. expect( range.start.offset ).to.equal( 0 );
  535. expect( range.start.isEqual( range.end ) ).to.be.true;
  536. selection.collapse( p, 1 );
  537. range = selection.getFirstRange();
  538. expect( range.start.parent ).to.equal( p );
  539. expect( range.start.offset ).to.equal( 1 );
  540. expect( range.start.isEqual( range.end ) ).to.be.true;
  541. } );
  542. it( 'should collapse selection at node and flag', () => {
  543. const foo = new Text( 'foo' );
  544. const p = new Element( 'p', null, foo );
  545. selection.collapse( foo, 'end' );
  546. let range = selection.getFirstRange();
  547. expect( range.start.parent ).to.equal( foo );
  548. expect( range.start.offset ).to.equal( 3 );
  549. expect( range.start.isEqual( range.end ) ).to.be.true;
  550. selection.collapse( foo, 'before' );
  551. range = selection.getFirstRange();
  552. expect( range.start.parent ).to.equal( p );
  553. expect( range.start.offset ).to.equal( 0 );
  554. expect( range.start.isEqual( range.end ) ).to.be.true;
  555. selection.collapse( foo, 'after' );
  556. range = selection.getFirstRange();
  557. expect( range.start.parent ).to.equal( p );
  558. expect( range.start.offset ).to.equal( 1 );
  559. expect( range.start.isEqual( range.end ) ).to.be.true;
  560. } );
  561. } );
  562. describe( 'collapseToStart', () => {
  563. it( 'should collapse to start position and fire change event', done => {
  564. selection.setRanges( [ range1, range2, range3 ] );
  565. selection.once( 'change', () => {
  566. expect( selection.rangeCount ).to.equal( 1 );
  567. expect( selection.isCollapsed ).to.be.true;
  568. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  569. done();
  570. } );
  571. selection.collapseToStart();
  572. } );
  573. it( 'should do nothing if no ranges present', () => {
  574. const fireSpy = sinon.spy( selection, 'fire' );
  575. selection.collapseToStart();
  576. fireSpy.restore();
  577. expect( fireSpy.notCalled ).to.be.true;
  578. } );
  579. } );
  580. describe( 'collapseToEnd', () => {
  581. it( 'should collapse to end position and fire change event', done => {
  582. selection.setRanges( [ range1, range2, range3 ] );
  583. selection.once( 'change', () => {
  584. expect( selection.rangeCount ).to.equal( 1 );
  585. expect( selection.isCollapsed ).to.be.true;
  586. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  587. done();
  588. } );
  589. selection.collapseToEnd();
  590. } );
  591. it( 'should do nothing if no ranges present', () => {
  592. const fireSpy = sinon.spy( selection, 'fire' );
  593. selection.collapseToEnd();
  594. fireSpy.restore();
  595. expect( fireSpy.notCalled ).to.be.true;
  596. } );
  597. } );
  598. describe( 'getEditableElement', () => {
  599. it( 'should return null if no ranges in selection', () => {
  600. expect( selection.editableElement ).to.be.null;
  601. } );
  602. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  603. selection.addRange( range1 );
  604. expect( selection.editableElement ).to.be.null;
  605. } );
  606. it( 'should return EditableElement when selection is placed inside', () => {
  607. const viewDocument = new Document();
  608. const selection = viewDocument.selection;
  609. const root = viewDocument.createRoot( 'div' );
  610. const element = new Element( 'p' );
  611. root.appendChildren( element );
  612. selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  613. expect( selection.editableElement ).to.equal( root );
  614. viewDocument.destroy();
  615. } );
  616. } );
  617. describe( 'createFromSelection', () => {
  618. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  619. selection.setRanges( [ range1, range2 ], true );
  620. const snapshot = Selection.createFromSelection( selection );
  621. expect( snapshot.isBackward ).to.equal( selection.isBackward );
  622. const selectionRanges = Array.from( selection.getRanges() );
  623. const snapshotRanges = Array.from( snapshot.getRanges() );
  624. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  625. for ( let i = 0; i < selectionRanges.length; i++ ) {
  626. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  627. }
  628. } );
  629. } );
  630. describe( 'isFake', () => {
  631. it( 'should be false for newly created instance', () => {
  632. expect( selection.isFake ).to.be.false;
  633. } );
  634. } );
  635. describe( 'setFake', () => {
  636. it( 'should allow to set selection to fake', () => {
  637. selection.setFake( true );
  638. expect( selection.isFake ).to.be.true;
  639. } );
  640. it( 'should allow to set fake selection label', () => {
  641. const label = 'foo bar baz';
  642. selection.setFake( true, { label } );
  643. expect( selection.fakeSelectionLabel ).to.equal( label );
  644. } );
  645. it( 'should not set label when set to false', () => {
  646. const label = 'foo bar baz';
  647. selection.setFake( false, { label } );
  648. expect( selection.fakeSelectionLabel ).to.equal( '' );
  649. } );
  650. it( 'should reset label when set to false', () => {
  651. const label = 'foo bar baz';
  652. selection.setFake( true, { label } );
  653. selection.setFake( false );
  654. expect( selection.fakeSelectionLabel ).to.equal( '' );
  655. } );
  656. it( 'should fire change event', done => {
  657. selection.once( 'change', () => {
  658. expect( selection.isFake ).to.be.true;
  659. expect( selection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  660. done();
  661. } );
  662. selection.setFake( true, { label: 'foo bar baz' } );
  663. } );
  664. } );
  665. describe( 'getSelectedElement', () => {
  666. it( 'should return selected element', () => {
  667. const { selection, view } = parse( 'foo [<b>bar</b>] baz' );
  668. const b = view.getChild( 1 );
  669. expect( selection.getSelectedElement() ).to.equal( b );
  670. } );
  671. it( 'should return null if there is more than one range', () => {
  672. const { selection } = parse( 'foo [<b>bar</b>] [<i>baz</i>]' );
  673. expect( selection.getSelectedElement() ).to.be.null;
  674. } );
  675. it( 'should return null if there is no selection', () => {
  676. expect( selection.getSelectedElement() ).to.be.null;
  677. } );
  678. it( 'should return null if selection is not over single element #1', () => {
  679. const { selection } = parse( 'foo [<b>bar</b> ba}z' );
  680. expect( selection.getSelectedElement() ).to.be.null;
  681. } );
  682. it( 'should return null if selection is not over single element #2', () => {
  683. const { selection } = parse( 'foo <b>{bar}</b> baz' );
  684. expect( selection.getSelectedElement() ).to.be.null;
  685. } );
  686. } );
  687. } );