selection.js 34 KB

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