documentselection.js 33 KB

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