selection.js 34 KB

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