selection.js 28 KB

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