8
0

selection.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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( 'moveFocusTo', () => {
  83. it( 'keeps all existing ranges when no modifications needed', () => {
  84. selection.setTo( range1 );
  85. selection.moveFocusTo( 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.moveFocusTo( endPos );
  92. } ).to.throw( CKEditorError, /view-selection-moveFocusTo-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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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.moveFocusTo( 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. } );
  421. describe( 'setTo()', () => {
  422. it( 'should set selection ranges from the given selection', () => {
  423. selection.setTo( range1 );
  424. const otherSelection = new Selection( [ range2, range3 ], true );
  425. selection.setTo( otherSelection );
  426. expect( selection.rangeCount ).to.equal( 2 );
  427. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  428. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  429. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  430. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  431. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  432. } );
  433. it( 'should set selection on the given Range using _setRanges method', () => {
  434. const spy = sinon.spy( selection, '_setRanges' );
  435. selection.setTo( range1 );
  436. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  437. expect( selection.isBackward ).to.be.false;
  438. expect( selection._setRanges.calledOnce ).to.be.true;
  439. spy.restore();
  440. } );
  441. it( 'should set selection on the given iterable of Ranges using _setRanges method', () => {
  442. const spy = sinon.spy( selection, '_setRanges' );
  443. selection.setTo( new Set( [ range1, range2 ] ) );
  444. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  445. expect( selection.isBackward ).to.be.false;
  446. expect( selection._setRanges.calledOnce ).to.be.true;
  447. spy.restore();
  448. } );
  449. it( 'should set collapsed selection on the given Position using _setRanges method', () => {
  450. const spy = sinon.spy( selection, '_setRanges' );
  451. selection.setTo( range1.start );
  452. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  453. expect( Array.from( selection.getRanges() )[ 0 ].start ).to.deep.equal( range1.start );
  454. expect( selection.isBackward ).to.be.false;
  455. expect( selection.isCollapsed ).to.be.true;
  456. expect( selection._setRanges.calledOnce ).to.be.true;
  457. spy.restore();
  458. } );
  459. it( 'should fire change event', done => {
  460. selection.on( 'change', () => {
  461. expect( selection.rangeCount ).to.equal( 1 );
  462. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  463. done();
  464. } );
  465. const otherSelection = new Selection( [ range1 ] );
  466. selection.setTo( otherSelection );
  467. } );
  468. it( 'should set fake state and label', () => {
  469. const otherSelection = new Selection();
  470. const label = 'foo bar baz';
  471. otherSelection.setFake( true, { label } );
  472. selection.setTo( otherSelection );
  473. expect( selection.isFake ).to.be.true;
  474. expect( selection.fakeSelectionLabel ).to.equal( label );
  475. } );
  476. } );
  477. describe( 'setTo - set collapsed at', () => {
  478. beforeEach( () => {
  479. selection.setTo( [ range1, range2 ] );
  480. } );
  481. it( 'should collapse selection at position', () => {
  482. const position = new Position( el, 4 );
  483. selection.setTo( position );
  484. const range = selection.getFirstRange();
  485. expect( range.start.parent ).to.equal( el );
  486. expect( range.start.offset ).to.equal( 4 );
  487. expect( range.start.isEqual( range.end ) ).to.be.true;
  488. } );
  489. it( 'should collapse selection at node and offset', () => {
  490. const foo = new Text( 'foo' );
  491. const p = new Element( 'p', null, foo );
  492. selection.setTo( foo );
  493. let range = selection.getFirstRange();
  494. expect( range.start.parent ).to.equal( foo );
  495. expect( range.start.offset ).to.equal( 0 );
  496. expect( range.start.isEqual( range.end ) ).to.be.true;
  497. selection.setTo( p, 1 );
  498. range = selection.getFirstRange();
  499. expect( range.start.parent ).to.equal( p );
  500. expect( range.start.offset ).to.equal( 1 );
  501. expect( range.start.isEqual( range.end ) ).to.be.true;
  502. } );
  503. it( 'should collapse selection at node and flag', () => {
  504. const foo = new Text( 'foo' );
  505. const p = new Element( 'p', null, foo );
  506. selection.setTo( foo, 'end' );
  507. let range = selection.getFirstRange();
  508. expect( range.start.parent ).to.equal( foo );
  509. expect( range.start.offset ).to.equal( 3 );
  510. expect( range.start.isEqual( range.end ) ).to.be.true;
  511. selection.setTo( foo, 'before' );
  512. range = selection.getFirstRange();
  513. expect( range.start.parent ).to.equal( p );
  514. expect( range.start.offset ).to.equal( 0 );
  515. expect( range.start.isEqual( range.end ) ).to.be.true;
  516. selection.setTo( foo, 'after' );
  517. range = selection.getFirstRange();
  518. expect( range.start.parent ).to.equal( p );
  519. expect( range.start.offset ).to.equal( 1 );
  520. expect( range.start.isEqual( range.end ) ).to.be.true;
  521. } );
  522. } );
  523. describe( 'setTo - collapse at start', () => {
  524. it( 'should collapse to start position and fire change event', done => {
  525. selection.setTo( [ range1, range2, range3 ] );
  526. selection.once( 'change', () => {
  527. expect( selection.rangeCount ).to.equal( 1 );
  528. expect( selection.isCollapsed ).to.be.true;
  529. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  530. done();
  531. } );
  532. selection.setTo( selection.getFirstPosition() );
  533. } );
  534. it( 'should do nothing if no ranges present', () => {
  535. const fireSpy = sinon.spy( selection, 'fire' );
  536. selection.setTo( selection.getFirstPosition() );
  537. fireSpy.restore();
  538. expect( fireSpy.notCalled ).to.be.true;
  539. } );
  540. } );
  541. describe( 'setTo - collapse to end', () => {
  542. it( 'should collapse to end position and fire change event', done => {
  543. selection.setTo( [ range1, range2, range3 ] );
  544. selection.once( 'change', () => {
  545. expect( selection.rangeCount ).to.equal( 1 );
  546. expect( selection.isCollapsed ).to.be.true;
  547. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  548. done();
  549. } );
  550. selection.setTo( selection.getLastPosition() );
  551. } );
  552. it( 'should do nothing if no ranges present', () => {
  553. const fireSpy = sinon.spy( selection, 'fire' );
  554. selection.setTo( selection.getLastPosition() );
  555. fireSpy.restore();
  556. expect( fireSpy.notCalled ).to.be.true;
  557. } );
  558. } );
  559. describe( 'getEditableElement', () => {
  560. it( 'should return null if no ranges in selection', () => {
  561. expect( selection.editableElement ).to.be.null;
  562. } );
  563. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  564. selection.setTo( range1 );
  565. expect( selection.editableElement ).to.be.null;
  566. } );
  567. it( 'should return EditableElement when selection is placed inside', () => {
  568. const viewDocument = new Document();
  569. const selection = viewDocument.selection;
  570. const root = createViewRoot( viewDocument, 'div', 'main' );
  571. const element = new Element( 'p' );
  572. root.appendChildren( element );
  573. selection.setTo( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  574. expect( selection.editableElement ).to.equal( root );
  575. viewDocument.destroy();
  576. } );
  577. } );
  578. describe( 'createFromSelection', () => {
  579. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  580. selection.setTo( [ range1, range2 ], true );
  581. const snapshot = Selection.createFromSelection( selection );
  582. expect( snapshot.isBackward ).to.equal( selection.isBackward );
  583. const selectionRanges = Array.from( selection.getRanges() );
  584. const snapshotRanges = Array.from( snapshot.getRanges() );
  585. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  586. for ( let i = 0; i < selectionRanges.length; i++ ) {
  587. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  588. }
  589. } );
  590. } );
  591. describe( 'isFake', () => {
  592. it( 'should be false for newly created instance', () => {
  593. expect( selection.isFake ).to.be.false;
  594. } );
  595. } );
  596. describe( 'setFake', () => {
  597. it( 'should allow to set selection to fake', () => {
  598. selection.setFake( true );
  599. expect( selection.isFake ).to.be.true;
  600. } );
  601. it( 'should allow to set fake selection label', () => {
  602. const label = 'foo bar baz';
  603. selection.setFake( true, { label } );
  604. expect( selection.fakeSelectionLabel ).to.equal( label );
  605. } );
  606. it( 'should not set label when set to false', () => {
  607. const label = 'foo bar baz';
  608. selection.setFake( false, { label } );
  609. expect( selection.fakeSelectionLabel ).to.equal( '' );
  610. } );
  611. it( 'should reset label when set to false', () => {
  612. const label = 'foo bar baz';
  613. selection.setFake( true, { label } );
  614. selection.setFake( false );
  615. expect( selection.fakeSelectionLabel ).to.equal( '' );
  616. } );
  617. it( 'should fire change event', done => {
  618. selection.once( 'change', () => {
  619. expect( selection.isFake ).to.be.true;
  620. expect( selection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  621. done();
  622. } );
  623. selection.setFake( true, { label: 'foo bar baz' } );
  624. } );
  625. } );
  626. describe( 'getSelectedElement', () => {
  627. it( 'should return selected element', () => {
  628. const { selection, view } = parse( 'foo [<b>bar</b>] baz' );
  629. const b = view.getChild( 1 );
  630. expect( selection.getSelectedElement() ).to.equal( b );
  631. } );
  632. it( 'should return null if there is more than one range', () => {
  633. const { selection } = parse( 'foo [<b>bar</b>] [<i>baz</i>]' );
  634. expect( selection.getSelectedElement() ).to.be.null;
  635. } );
  636. it( 'should return null if there is no selection', () => {
  637. expect( selection.getSelectedElement() ).to.be.null;
  638. } );
  639. it( 'should return null if selection is not over single element #1', () => {
  640. const { selection } = parse( 'foo [<b>bar</b> ba}z' );
  641. expect( selection.getSelectedElement() ).to.be.null;
  642. } );
  643. it( 'should return null if selection is not over single element #2', () => {
  644. const { selection } = parse( 'foo <b>{bar}</b> baz' );
  645. expect( selection.getSelectedElement() ).to.be.null;
  646. } );
  647. } );
  648. } );