selection.js 34 KB

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