selection.js 34 KB

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