selection.js 31 KB

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