8
0

selection.js 33 KB

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