selection.js 31 KB

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