selection.js 34 KB

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