8
0

selection.js 26 KB

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