selection.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import Text from '/ckeditor5/engine/model/text.js';
  9. import Range from '/ckeditor5/engine/model/range.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import LiveRange from '/ckeditor5/engine/model/liverange.js';
  12. import Selection from '/ckeditor5/engine/model/selection.js';
  13. import testUtils from '/tests/core/_utils/utils.js';
  14. import count from '/ckeditor5/utils/count.js';
  15. testUtils.createSinonSandbox();
  16. describe( 'Selection', () => {
  17. let doc, root, selection, liveRange, range, range1, range2, range3;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot();
  21. root.appendChildren( [
  22. new Element( 'p' ),
  23. new Element( 'p' ),
  24. new Element( 'p', [], new Text( 'foobar' ) ),
  25. new Element( 'p' ),
  26. new Element( 'p' ),
  27. new Element( 'p' ),
  28. new Element( 'p', [], new Text( 'foobar' ) )
  29. ] );
  30. selection = new Selection();
  31. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  32. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  33. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  34. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  35. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  36. } );
  37. afterEach( () => {
  38. doc.destroy();
  39. liveRange.detach();
  40. } );
  41. describe( 'isCollapsed', () => {
  42. it( 'should return false for empty selection', () => {
  43. expect( selection.isCollapsed ).to.be.false;
  44. } );
  45. it( 'should return true when there is single collapsed ranges', () => {
  46. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  47. expect( selection.isCollapsed ).to.be.true;
  48. } );
  49. it( 'should return false when there are multiple ranges', () => {
  50. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  51. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  52. expect( selection.isCollapsed ).to.be.false;
  53. } );
  54. it( 'should return false when there is not collapsed range', () => {
  55. selection.addRange( range );
  56. expect( selection.isCollapsed ).to.be.false;
  57. } );
  58. } );
  59. describe( 'rangeCount', () => {
  60. it( 'should return proper range count', () => {
  61. expect( selection.rangeCount ).to.equal( 0 );
  62. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  63. expect( selection.rangeCount ).to.equal( 1 );
  64. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  65. expect( selection.rangeCount ).to.equal( 2 );
  66. } );
  67. } );
  68. describe( 'isBackward', () => {
  69. it( 'is defined by the last added range', () => {
  70. selection.addRange( range, true );
  71. expect( selection ).to.have.property( 'isBackward', true );
  72. selection.addRange( liveRange );
  73. expect( selection ).to.have.property( 'isBackward', false );
  74. } );
  75. it( 'is false when last range is collapsed', () => {
  76. const pos = Position.createAt( root, 0 );
  77. selection.addRange( new Range( pos, pos ), true );
  78. expect( selection.isBackward ).to.be.false;
  79. } );
  80. } );
  81. describe( 'addRange', () => {
  82. it( 'should throw an error when range is invalid', () => {
  83. expect( () => {
  84. selection.addRange( { invalid: 'Range' } );
  85. } ).to.throwCKEditorError( /model-selection-added-not-range/ );
  86. } );
  87. it( 'should copy added ranges and store multiple ranges', () => {
  88. selection.addRange( liveRange );
  89. selection.addRange( range );
  90. const ranges = selection._ranges;
  91. expect( ranges.length ).to.equal( 2 );
  92. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  93. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  94. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  95. expect( ranges[ 1 ] ).not.to.be.equal( range );
  96. } );
  97. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  98. selection.addRange( liveRange );
  99. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  100. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  101. selection.addRange( range );
  102. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  103. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  104. } );
  105. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  106. selection.addRange( liveRange, true );
  107. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  108. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  109. selection.addRange( range, true );
  110. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  111. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  112. } );
  113. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  114. selection.addRange( liveRange );
  115. const ranges = Array.from( selection.getRanges() );
  116. selection.addRange( range );
  117. expect( ranges.length ).to.equal( 1 );
  118. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  119. } );
  120. it( 'should fire change:range event when adding a range', () => {
  121. let spy = sinon.spy();
  122. selection.on( 'change:range', spy );
  123. selection.addRange( range );
  124. expect( spy.called ).to.be.true;
  125. } );
  126. it( 'should throw an error if added range intersects with already stored range', () => {
  127. selection.addRange( liveRange );
  128. expect( () => {
  129. selection.addRange(
  130. new Range(
  131. new Position( root, [ 0, 4 ] ),
  132. new Position( root, [ 1, 2 ] )
  133. )
  134. );
  135. } ).to.throwCKEditorError( /model-selection-range-intersects/ );
  136. } );
  137. } );
  138. describe( 'collapse', () => {
  139. it( 'fires change:range', () => {
  140. const spy = sinon.spy();
  141. selection.on( 'change:range', spy );
  142. selection.collapse( root );
  143. expect( spy.calledOnce ).to.be.true;
  144. } );
  145. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  146. selection.collapse( root );
  147. expect( selection ).to.have.property( 'isCollapsed', true );
  148. const focus = selection.focus;
  149. expect( focus ).to.have.property( 'parent', root );
  150. expect( focus ).to.have.property( 'offset', 0 );
  151. } );
  152. it( 'sets selection at given offset in given parent', () => {
  153. selection.collapse( root, 3 );
  154. expect( selection ).to.have.property( 'isCollapsed', true );
  155. const focus = selection.focus;
  156. expect( focus ).to.have.property( 'parent', root );
  157. expect( focus ).to.have.property( 'offset', 3 );
  158. } );
  159. it( 'sets selection at the end of the given parent', () => {
  160. selection.collapse( root, 'end' );
  161. expect( selection ).to.have.property( 'isCollapsed', true );
  162. const focus = selection.focus;
  163. expect( focus ).to.have.property( 'parent', root );
  164. expect( focus ).to.have.property( 'offset', root.maxOffset );
  165. } );
  166. it( 'sets selection before the specified element', () => {
  167. selection.collapse( root.getChild( 1 ), 'before' );
  168. expect( selection ).to.have.property( 'isCollapsed', true );
  169. const focus = selection.focus;
  170. expect( focus ).to.have.property( 'parent', root );
  171. expect( focus ).to.have.property( 'offset', 1 );
  172. } );
  173. it( 'sets selection after the specified element', () => {
  174. selection.collapse( root.getChild( 1 ), 'after' );
  175. expect( selection ).to.have.property( 'isCollapsed', true );
  176. const focus = selection.focus;
  177. expect( focus ).to.have.property( 'parent', root );
  178. expect( focus ).to.have.property( 'offset', 2 );
  179. } );
  180. it( 'sets selection at the specified position', () => {
  181. const pos = Position.createFromParentAndOffset( root, 3 );
  182. selection.collapse( pos );
  183. expect( selection ).to.have.property( 'isCollapsed', true );
  184. const focus = selection.focus;
  185. expect( focus ).to.have.property( 'parent', root );
  186. expect( focus ).to.have.property( 'offset', 3 );
  187. } );
  188. } );
  189. describe( 'focus', () => {
  190. let r3;
  191. beforeEach( () => {
  192. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  193. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  194. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  195. selection.addRange( r1 );
  196. selection.addRange( r2 );
  197. } );
  198. it( 'should return correct focus when last added range is not backward one', () => {
  199. selection.addRange( r3 );
  200. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  201. } );
  202. it( 'should return correct focus when last added range is backward one', () => {
  203. selection.addRange( r3, true );
  204. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  205. } );
  206. it( 'should return null if no ranges in selection', () => {
  207. selection.removeAllRanges();
  208. expect( selection.focus ).to.be.null;
  209. } );
  210. } );
  211. describe( 'setFocus', () => {
  212. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  213. selection.addRange( range );
  214. selection.addRange( liveRange );
  215. const spy = sinon.spy();
  216. selection.on( 'change:range', spy );
  217. selection.setFocus( selection.focus );
  218. expect( count( selection.getRanges() ) ).to.equal( 2 );
  219. expect( spy.callCount ).to.equal( 0 );
  220. } );
  221. it( 'fires change:range', () => {
  222. selection.addRange( range );
  223. const spy = sinon.spy();
  224. selection.on( 'change:range', spy );
  225. selection.setFocus( Position.createAt( root, 'end' ) );
  226. expect( spy.calledOnce ).to.be.true;
  227. } );
  228. it( 'throws if there are no ranges in selection', () => {
  229. const endPos = Position.createAt( root, 'end' );
  230. expect( () => {
  231. selection.setFocus( endPos );
  232. } ).to.throwCKEditorError( /model-selection-setFocus-no-ranges/ );
  233. } );
  234. it( 'modifies existing collapsed selection', () => {
  235. const startPos = Position.createAt( root, 1 );
  236. const endPos = Position.createAt( root, 2 );
  237. selection.collapse( startPos );
  238. selection.setFocus( endPos );
  239. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  240. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  241. } );
  242. it( 'makes existing collapsed selection a backward selection', () => {
  243. const startPos = Position.createAt( root, 1 );
  244. const endPos = Position.createAt( root, 0 );
  245. selection.collapse( startPos );
  246. selection.setFocus( endPos );
  247. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  248. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  249. expect( selection.isBackward ).to.be.true;
  250. } );
  251. it( 'modifies existing non-collapsed selection', () => {
  252. const startPos = Position.createAt( root, 1 );
  253. const endPos = Position.createAt( root, 2 );
  254. const newEndPos = Position.createAt( root, 3 );
  255. selection.addRange( new Range( startPos, endPos ) );
  256. selection.setFocus( newEndPos );
  257. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  258. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  259. } );
  260. it( 'makes existing non-collapsed selection a backward selection', () => {
  261. const startPos = Position.createAt( root, 1 );
  262. const endPos = Position.createAt( root, 2 );
  263. const newEndPos = Position.createAt( root, 0 );
  264. selection.addRange( new Range( startPos, endPos ) );
  265. selection.setFocus( newEndPos );
  266. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  267. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  268. expect( selection.isBackward ).to.be.true;
  269. } );
  270. it( 'makes existing backward selection a forward selection', () => {
  271. const startPos = Position.createAt( root, 1 );
  272. const endPos = Position.createAt( root, 2 );
  273. const newEndPos = Position.createAt( root, 3 );
  274. selection.addRange( new Range( startPos, endPos ), true );
  275. selection.setFocus( newEndPos );
  276. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  277. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  278. expect( selection.isBackward ).to.be.false;
  279. } );
  280. it( 'modifies existing backward selection', () => {
  281. const startPos = Position.createAt( root, 1 );
  282. const endPos = Position.createAt( root, 2 );
  283. const newEndPos = Position.createAt( root, 0 );
  284. selection.addRange( new Range( startPos, endPos ), true );
  285. selection.setFocus( newEndPos );
  286. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  287. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  288. expect( selection.isBackward ).to.be.true;
  289. } );
  290. it( 'modifies only the last range', () => {
  291. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  292. const startPos1 = Position.createAt( root, 4 );
  293. const endPos1 = Position.createAt( root, 5 );
  294. const startPos2 = Position.createAt( root, 1 );
  295. const endPos2 = Position.createAt( root, 2 );
  296. const newEndPos = Position.createAt( root, 0 );
  297. selection.addRange( new Range( startPos1, endPos1 ) );
  298. selection.addRange( new Range( startPos2, endPos2 ) );
  299. const spy = sinon.spy();
  300. selection.on( 'change:range', spy );
  301. selection.setFocus( newEndPos );
  302. const ranges = Array.from( selection.getRanges() );
  303. expect( ranges ).to.have.lengthOf( 2 );
  304. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  305. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  306. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  307. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  308. expect( selection.isBackward ).to.be.true;
  309. expect( spy.calledOnce ).to.be.true;
  310. } );
  311. it( 'collapses the selection when extending to the anchor', () => {
  312. const startPos = Position.createAt( root, 1 );
  313. const endPos = Position.createAt( root, 2 );
  314. selection.addRange( new Range( startPos, endPos ) );
  315. selection.setFocus( startPos );
  316. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  317. expect( selection.isCollapsed ).to.be.true;
  318. } );
  319. it( 'uses Position.createAt', () => {
  320. const startPos = Position.createAt( root, 1 );
  321. const endPos = Position.createAt( root, 2 );
  322. const newEndPos = Position.createAt( root, 4 );
  323. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  324. selection.addRange( new Range( startPos, endPos ) );
  325. selection.setFocus( root, 'end' );
  326. expect( spy.calledOnce ).to.be.true;
  327. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  328. } );
  329. } );
  330. describe( 'removeAllRanges', () => {
  331. let spy, ranges;
  332. beforeEach( () => {
  333. selection.addRange( liveRange );
  334. selection.addRange( range );
  335. spy = sinon.spy();
  336. selection.on( 'change:range', spy );
  337. ranges = selection._ranges;
  338. selection.removeAllRanges();
  339. } );
  340. it( 'should remove all stored ranges', () => {
  341. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  342. } );
  343. it( 'should fire exactly one update event', () => {
  344. expect( spy.calledOnce ).to.be.true;
  345. } );
  346. } );
  347. describe( 'setRanges', () => {
  348. let newRanges, spy, oldRanges;
  349. before( () => {
  350. newRanges = [
  351. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  352. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  353. ];
  354. } );
  355. beforeEach( () => {
  356. selection.addRange( liveRange );
  357. selection.addRange( range );
  358. spy = sinon.spy();
  359. selection.on( 'change:range', spy );
  360. oldRanges = selection._ranges;
  361. } );
  362. it( 'should throw an error when range is invalid', () => {
  363. expect( () => {
  364. selection.setRanges( [ { invalid: 'range' } ] );
  365. } ).to.throwCKEditorError( /model-selection-added-not-range/ );
  366. } );
  367. it( 'should remove all ranges and add given ranges', () => {
  368. selection.setRanges( newRanges );
  369. let ranges = selection._ranges;
  370. expect( ranges.length ).to.equal( 2 );
  371. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  372. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  373. } );
  374. it( 'should use last range from given array to get anchor and focus position', () => {
  375. selection.setRanges( newRanges );
  376. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  377. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  378. } );
  379. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  380. selection.setRanges( newRanges, true );
  381. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  382. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  383. } );
  384. it( 'should fire exactly one update event', () => {
  385. selection.setRanges( newRanges );
  386. expect( spy.calledOnce ).to.be.true;
  387. } );
  388. } );
  389. describe( 'setTo', () => {
  390. it( 'should set selection to be same as given selection, using setRanges method', () => {
  391. sinon.spy( selection, 'setRanges' );
  392. const otherSelection = new Selection();
  393. otherSelection.addRange( range1 );
  394. otherSelection.addRange( range2, true );
  395. selection.setTo( otherSelection );
  396. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  397. expect( selection.isBackward ).to.be.true;
  398. expect( selection.setRanges.calledOnce ).to.be.true;
  399. } );
  400. } );
  401. describe( 'getFirstRange', () => {
  402. it( 'should return null if no ranges were added', () => {
  403. expect( selection.getFirstRange() ).to.be.null;
  404. } );
  405. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  406. // This will not be the first range despite being added as first
  407. selection.addRange( range2 );
  408. // This should be the first range.
  409. selection.addRange( range1 );
  410. // A random range that is not first.
  411. selection.addRange( range3 );
  412. let range = selection.getFirstRange();
  413. expect( range.start.path ).to.deep.equal( [ 1 ] );
  414. expect( range.end.path ).to.deep.equal( [ 4 ] );
  415. } );
  416. } );
  417. describe( 'getFirstPosition', () => {
  418. it( 'should return null if no ranges were added', () => {
  419. expect( selection.getFirstPosition() ).to.be.null;
  420. } );
  421. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  422. // This will not be the first range despite being added as first
  423. selection.addRange( range2 );
  424. // This should be the first range.
  425. selection.addRange( range1 );
  426. // A random range that is not first.
  427. selection.addRange( range3 );
  428. let position = selection.getFirstPosition();
  429. expect( position.path ).to.deep.equal( [ 1 ] );
  430. } );
  431. } );
  432. describe( 'getLastRange', () => {
  433. it( 'should return null if no ranges were added', () => {
  434. expect( selection.getLastRange() ).to.be.null;
  435. } );
  436. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  437. selection.addRange( range3 );
  438. selection.addRange( range1 );
  439. selection.addRange( range2 );
  440. let range = selection.getLastRange();
  441. expect( range.start.path ).to.deep.equal( [ 6 ] );
  442. expect( range.end.path ).to.deep.equal( [ 7 ] );
  443. } );
  444. } );
  445. describe( 'getLastPosition', () => {
  446. it( 'should return null if no ranges were added', () => {
  447. expect( selection.getLastPosition() ).to.be.null;
  448. } );
  449. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  450. selection.addRange( range3 );
  451. selection.addRange( range1 );
  452. selection.addRange( range2 );
  453. let position = selection.getLastPosition();
  454. expect( position.path ).to.deep.equal( [ 7 ] );
  455. } );
  456. } );
  457. describe( 'isEqual', () => {
  458. it( 'should return true if selections equal', () => {
  459. selection.addRange( range1 );
  460. selection.addRange( range2 );
  461. const otherSelection = new Selection();
  462. otherSelection.addRange( range1 );
  463. otherSelection.addRange( range2 );
  464. expect( selection.isEqual( otherSelection ) ).to.be.true;
  465. } );
  466. it( 'should return true if backward selections equal', () => {
  467. selection.addRange( range1, true );
  468. const otherSelection = new Selection();
  469. otherSelection.addRange( range1, true );
  470. expect( selection.isEqual( otherSelection ) ).to.be.true;
  471. } );
  472. it( 'should return false if ranges count does not equal', () => {
  473. selection.addRange( range1 );
  474. selection.addRange( range2 );
  475. const otherSelection = new Selection();
  476. otherSelection.addRange( range1 );
  477. expect( selection.isEqual( otherSelection ) ).to.be.false;
  478. } );
  479. it( 'should return false if ranges do not equal', () => {
  480. selection.addRange( range1 );
  481. const otherSelection = new Selection();
  482. otherSelection.addRange( range2 );
  483. expect( selection.isEqual( otherSelection ) ).to.be.false;
  484. } );
  485. it( 'should return false if directions do not equal', () => {
  486. selection.addRange( range1 );
  487. const otherSelection = new Selection();
  488. otherSelection.addRange( range1, true );
  489. expect( selection.isEqual( otherSelection ) ).to.be.false;
  490. } );
  491. } );
  492. describe( 'collapseToStart', () => {
  493. it( 'should collapse to start position and fire change event', () => {
  494. selection.setRanges( [ range2, range1, range3 ] );
  495. selection.collapseToStart();
  496. expect( selection.rangeCount ).to.equal( 1 );
  497. expect( selection.isCollapsed ).to.be.true;
  498. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  499. } );
  500. it( 'should do nothing if no ranges present', () => {
  501. const spy = sinon.spy( selection, 'fire' );
  502. selection.collapseToStart();
  503. spy.restore();
  504. expect( spy.notCalled ).to.be.true;
  505. } );
  506. } );
  507. describe( 'collapseToEnd', () => {
  508. it( 'should collapse to start position and fire change event', () => {
  509. selection.setRanges( [ range2, range3, range1 ] );
  510. selection.collapseToEnd();
  511. expect( selection.rangeCount ).to.equal( 1 );
  512. expect( selection.isCollapsed ).to.be.true;
  513. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  514. } );
  515. it( 'should do nothing if no ranges present', () => {
  516. const spy = sinon.spy( selection, 'fire' );
  517. selection.collapseToEnd();
  518. spy.restore();
  519. expect( spy.notCalled ).to.be.true;
  520. } );
  521. } );
  522. describe( 'createFromSelection', () => {
  523. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  524. selection.addRange( liveRange );
  525. selection.addRange( range, true );
  526. const snapshot = Selection.createFromSelection( selection );
  527. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  528. const selectionRanges = Array.from( selection.getRanges() );
  529. const snapshotRanges = Array.from( snapshot.getRanges() );
  530. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  531. for ( let i = 0; i < selectionRanges.length; i++ ) {
  532. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  533. }
  534. } );
  535. } );
  536. describe( 'attributes interface', () => {
  537. let rangeInFullP;
  538. beforeEach( () => {
  539. root.insertChildren( 0, [
  540. new Element( 'p', [], new Text( 'foobar' ) ),
  541. new Element( 'p', [], [] )
  542. ] );
  543. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  544. } );
  545. describe( 'setAttribute', () => {
  546. it( 'should set given attribute on the selection', () => {
  547. selection.setRanges( [ rangeInFullP ] );
  548. selection.setAttribute( 'foo', 'bar' );
  549. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  550. } );
  551. it( 'should fire change:attribute event', () => {
  552. let spy = sinon.spy();
  553. selection.on( 'change:attribute', spy );
  554. selection.setAttribute( 'foo', 'bar' );
  555. expect( spy.called ).to.be.true;
  556. } );
  557. } );
  558. describe( 'getAttribute', () => {
  559. it( 'should return undefined if element does not contain given attribute', () => {
  560. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  561. } );
  562. } );
  563. describe( 'getAttributes', () => {
  564. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  565. selection.setRanges( [ rangeInFullP ] );
  566. selection.setAttribute( 'foo', 'bar' );
  567. selection.setAttribute( 'abc', 'xyz' );
  568. let attrs = Array.from( selection.getAttributes() );
  569. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  570. } );
  571. } );
  572. describe( 'getAttributeKeys', () => {
  573. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  574. selection.setRanges( [ rangeInFullP ] );
  575. selection.setAttribute( 'foo', 'bar' );
  576. selection.setAttribute( 'abc', 'xyz' );
  577. let attrs = Array.from( selection.getAttributeKeys() );
  578. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  579. } );
  580. } );
  581. describe( 'hasAttribute', () => {
  582. it( 'should return true if element contains attribute with given key', () => {
  583. selection.setRanges( [ rangeInFullP ] );
  584. selection.setAttribute( 'foo', 'bar' );
  585. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  586. } );
  587. it( 'should return false if element does not contain attribute with given key', () => {
  588. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  589. } );
  590. } );
  591. describe( 'clearAttributes', () => {
  592. it( 'should remove all attributes from the element', () => {
  593. selection.setRanges( [ rangeInFullP ] );
  594. selection.setAttribute( 'foo', 'bar' );
  595. selection.setAttribute( 'abc', 'xyz' );
  596. selection.clearAttributes();
  597. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  598. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  599. } );
  600. it( 'should fire change:attribute event', () => {
  601. let spy = sinon.spy();
  602. selection.on( 'change:attribute', spy );
  603. selection.clearAttributes();
  604. expect( spy.called ).to.be.true;
  605. } );
  606. } );
  607. describe( 'removeAttribute', () => {
  608. it( 'should remove attribute', () => {
  609. selection.setRanges( [ rangeInFullP ] );
  610. selection.setAttribute( 'foo', 'bar' );
  611. selection.removeAttribute( 'foo' );
  612. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  613. } );
  614. it( 'should fire change:attribute event', () => {
  615. let spy = sinon.spy();
  616. selection.on( 'change:attribute', spy );
  617. selection.removeAttribute( 'foo' );
  618. expect( spy.called ).to.be.true;
  619. } );
  620. } );
  621. describe( 'setAttributesTo', () => {
  622. it( 'should remove all attributes set on element and set the given ones', () => {
  623. selection.setRanges( [ rangeInFullP ] );
  624. selection.setAttribute( 'abc', 'xyz' );
  625. selection.setAttributesTo( { foo: 'bar' } );
  626. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  627. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  628. } );
  629. } );
  630. } );
  631. } );