selection.js 33 KB

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