selection.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 copy added ranges and store multiple ranges', () => {
  84. selection.addRange( liveRange );
  85. selection.addRange( range );
  86. const ranges = selection._ranges;
  87. expect( ranges.length ).to.equal( 2 );
  88. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  89. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  90. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  91. expect( ranges[ 1 ] ).not.to.be.equal( range );
  92. } );
  93. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  94. selection.addRange( liveRange );
  95. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  96. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  97. selection.addRange( range );
  98. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  99. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  100. } );
  101. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  102. selection.addRange( liveRange, true );
  103. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  104. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  105. selection.addRange( range, true );
  106. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  107. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  108. } );
  109. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  110. selection.addRange( liveRange );
  111. const ranges = Array.from( selection.getRanges() );
  112. selection.addRange( range );
  113. expect( ranges.length ).to.equal( 1 );
  114. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  115. } );
  116. it( 'should fire change:range event when adding a range', () => {
  117. let spy = sinon.spy();
  118. selection.on( 'change:range', spy );
  119. selection.addRange( range );
  120. expect( spy.called ).to.be.true;
  121. } );
  122. it( 'should throw an error when range is invalid', () => {
  123. expect( () => {
  124. selection.addRange( { invalid: 'range' } );
  125. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  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;
  333. it( 'should remove all stored ranges', () => {
  334. selection.addRange( liveRange );
  335. selection.addRange( range );
  336. selection.removeAllRanges();
  337. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  338. } );
  339. it( 'should fire exactly one change:range event', () => {
  340. selection.addRange( liveRange );
  341. selection.addRange( range );
  342. spy = sinon.spy();
  343. selection.on( 'change:range', spy );
  344. selection.removeAllRanges();
  345. expect( spy.calledOnce ).to.be.true;
  346. } );
  347. it( 'should not fire change:range event if there were no ranges', () => {
  348. spy = sinon.spy();
  349. selection.on( 'change:range', spy );
  350. selection.removeAllRanges();
  351. expect( spy.called ).to.be.false;
  352. } );
  353. } );
  354. describe( 'setRanges', () => {
  355. let newRanges, spy, oldRanges;
  356. beforeEach( () => {
  357. newRanges = [
  358. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  359. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  360. ];
  361. selection.addRange( liveRange );
  362. selection.addRange( range );
  363. spy = sinon.spy();
  364. selection.on( 'change:range', spy );
  365. oldRanges = selection._ranges;
  366. } );
  367. it( 'should throw an error when range is invalid', () => {
  368. expect( () => {
  369. selection.setRanges( [ { invalid: 'range' } ] );
  370. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  371. } );
  372. it( 'should remove all ranges and add given ranges', () => {
  373. selection.setRanges( newRanges );
  374. let ranges = Array.from( selection.getRanges() );
  375. expect( ranges ).to.deep.equal( newRanges );
  376. } );
  377. it( 'should use last range from given array to get anchor and focus position', () => {
  378. selection.setRanges( newRanges );
  379. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  380. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  381. } );
  382. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  383. selection.setRanges( newRanges, true );
  384. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  385. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  386. } );
  387. it( 'should fire exactly one change:range event', () => {
  388. selection.setRanges( newRanges );
  389. expect( spy.calledOnce ).to.be.true;
  390. } );
  391. it( 'should fire change:range event with correct parameters', () => {
  392. selection.on( 'change:range', ( evt, data ) => {
  393. expect( data.directChange ).to.be.true;
  394. } );
  395. selection.setRanges( newRanges );
  396. } );
  397. it( 'should not fire change:range event if given ranges are the same', () => {
  398. selection.setRanges( [ liveRange, range ] );
  399. expect( spy.calledOnce ).to.be.false;
  400. } );
  401. } );
  402. describe( 'setTo', () => {
  403. it( 'should set selection to be same as given selection, using setRanges method', () => {
  404. const spy = sinon.spy( selection, 'setRanges' );
  405. const otherSelection = new Selection();
  406. otherSelection.addRange( range1 );
  407. otherSelection.addRange( range2, true );
  408. selection.setTo( otherSelection );
  409. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  410. expect( selection.isBackward ).to.be.true;
  411. expect( selection.setRanges.calledOnce ).to.be.true;
  412. spy.restore();
  413. } );
  414. } );
  415. describe( 'getFirstRange', () => {
  416. it( 'should return null if no ranges were added', () => {
  417. expect( selection.getFirstRange() ).to.be.null;
  418. } );
  419. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  420. // This will not be the first range despite being added as first
  421. selection.addRange( range2 );
  422. // This should be the first range.
  423. selection.addRange( range1 );
  424. // A random range that is not first.
  425. selection.addRange( range3 );
  426. let range = selection.getFirstRange();
  427. expect( range.start.path ).to.deep.equal( [ 1 ] );
  428. expect( range.end.path ).to.deep.equal( [ 4 ] );
  429. } );
  430. } );
  431. describe( 'getFirstPosition', () => {
  432. it( 'should return null if no ranges were added', () => {
  433. expect( selection.getFirstPosition() ).to.be.null;
  434. } );
  435. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  436. // This will not be the first range despite being added as first
  437. selection.addRange( range2 );
  438. // This should be the first range.
  439. selection.addRange( range1 );
  440. // A random range that is not first.
  441. selection.addRange( range3 );
  442. let position = selection.getFirstPosition();
  443. expect( position.path ).to.deep.equal( [ 1 ] );
  444. } );
  445. } );
  446. describe( 'getLastRange', () => {
  447. it( 'should return null if no ranges were added', () => {
  448. expect( selection.getLastRange() ).to.be.null;
  449. } );
  450. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  451. selection.addRange( range3 );
  452. selection.addRange( range1 );
  453. selection.addRange( range2 );
  454. let range = selection.getLastRange();
  455. expect( range.start.path ).to.deep.equal( [ 6 ] );
  456. expect( range.end.path ).to.deep.equal( [ 7 ] );
  457. } );
  458. } );
  459. describe( 'getLastPosition', () => {
  460. it( 'should return null if no ranges were added', () => {
  461. expect( selection.getLastPosition() ).to.be.null;
  462. } );
  463. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  464. selection.addRange( range3 );
  465. selection.addRange( range1 );
  466. selection.addRange( range2 );
  467. let position = selection.getLastPosition();
  468. expect( position.path ).to.deep.equal( [ 7 ] );
  469. } );
  470. } );
  471. describe( 'isEqual', () => {
  472. it( 'should return true if selections equal', () => {
  473. selection.addRange( range1 );
  474. selection.addRange( range2 );
  475. const otherSelection = new Selection();
  476. otherSelection.addRange( range1 );
  477. otherSelection.addRange( range2 );
  478. expect( selection.isEqual( otherSelection ) ).to.be.true;
  479. } );
  480. it( 'should return true if backward selections equal', () => {
  481. selection.addRange( range1, true );
  482. const otherSelection = new Selection();
  483. otherSelection.addRange( range1, true );
  484. expect( selection.isEqual( otherSelection ) ).to.be.true;
  485. } );
  486. it( 'should return true if both selections have no ranges', () => {
  487. const otherSelection = new Selection();
  488. expect( selection.isEqual( otherSelection ) ).to.be.true;
  489. } );
  490. it( 'should return false if ranges count does not equal', () => {
  491. selection.addRange( range1 );
  492. selection.addRange( range2 );
  493. const otherSelection = new Selection();
  494. otherSelection.addRange( range2 );
  495. expect( selection.isEqual( otherSelection ) ).to.be.false;
  496. } );
  497. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  498. selection.addRange( range1 );
  499. selection.addRange( range3 );
  500. const otherSelection = new Selection();
  501. otherSelection.addRange( range2 );
  502. otherSelection.addRange( range3 );
  503. expect( selection.isEqual( otherSelection ) ).to.be.false;
  504. } );
  505. it( 'should return false if directions do not equal', () => {
  506. selection.addRange( range1 );
  507. const otherSelection = new Selection();
  508. otherSelection.addRange( range1, true );
  509. expect( selection.isEqual( otherSelection ) ).to.be.false;
  510. } );
  511. } );
  512. describe( 'collapseToStart', () => {
  513. it( 'should collapse to start position and fire change event', () => {
  514. selection.setRanges( [ range2, range1, range3 ] );
  515. const spy = sinon.spy();
  516. selection.on( 'change:range', spy );
  517. selection.collapseToStart();
  518. expect( selection.rangeCount ).to.equal( 1 );
  519. expect( selection.isCollapsed ).to.be.true;
  520. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  521. expect( spy.calledOnce ).to.be.true;
  522. } );
  523. it( 'should do nothing if selection was already collapsed', () => {
  524. selection.collapse( range1.start );
  525. const spy = sinon.spy( selection, 'fire' );
  526. selection.collapseToStart();
  527. expect( spy.notCalled ).to.be.true;
  528. spy.restore();
  529. } );
  530. it( 'should do nothing if no ranges present', () => {
  531. const spy = sinon.spy( selection, 'fire' );
  532. selection.collapseToStart();
  533. spy.restore();
  534. expect( spy.notCalled ).to.be.true;
  535. } );
  536. } );
  537. describe( 'collapseToEnd', () => {
  538. it( 'should collapse to start position and fire change:range event', () => {
  539. selection.setRanges( [ range2, range3, range1 ] );
  540. const spy = sinon.spy();
  541. selection.on( 'change:range', spy );
  542. selection.collapseToEnd();
  543. expect( selection.rangeCount ).to.equal( 1 );
  544. expect( selection.isCollapsed ).to.be.true;
  545. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  546. expect( spy.calledOnce ).to.be.true;
  547. } );
  548. it( 'should do nothing if selection was already collapsed', () => {
  549. selection.collapse( range1.start );
  550. const spy = sinon.spy( selection, 'fire' );
  551. selection.collapseToEnd();
  552. expect( spy.notCalled ).to.be.true;
  553. spy.restore();
  554. } );
  555. it( 'should do nothing if selection has no ranges', () => {
  556. const spy = sinon.spy( selection, 'fire' );
  557. selection.collapseToEnd();
  558. expect( spy.notCalled ).to.be.true;
  559. spy.restore();
  560. } );
  561. } );
  562. describe( 'createFromSelection', () => {
  563. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  564. selection.addRange( liveRange );
  565. selection.addRange( range, true );
  566. const snapshot = Selection.createFromSelection( selection );
  567. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  568. const selectionRanges = Array.from( selection.getRanges() );
  569. const snapshotRanges = Array.from( snapshot.getRanges() );
  570. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  571. for ( let i = 0; i < selectionRanges.length; i++ ) {
  572. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  573. }
  574. } );
  575. } );
  576. describe( 'attributes interface', () => {
  577. let rangeInFullP;
  578. beforeEach( () => {
  579. root.insertChildren( 0, [
  580. new Element( 'p', [], new Text( 'foobar' ) ),
  581. new Element( 'p', [], [] )
  582. ] );
  583. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  584. } );
  585. describe( 'setAttribute', () => {
  586. it( 'should set given attribute on the selection', () => {
  587. selection.setRanges( [ rangeInFullP ] );
  588. selection.setAttribute( 'foo', 'bar' );
  589. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  590. } );
  591. it( 'should fire change:attribute event with correct parameters', () => {
  592. selection.on( 'change:attribute', ( evt, data ) => {
  593. expect( data.directChange ).to.be.true;
  594. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  595. } );
  596. selection.setAttribute( 'foo', 'bar' );
  597. } );
  598. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  599. selection.setAttribute( 'foo', 'bar' );
  600. let spy = sinon.spy();
  601. selection.on( 'change:attribute', spy );
  602. selection.setAttribute( 'foo', 'bar' );
  603. expect( spy.called ).to.be.false;
  604. } );
  605. } );
  606. describe( 'getAttribute', () => {
  607. it( 'should return undefined if element does not contain given attribute', () => {
  608. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  609. } );
  610. } );
  611. describe( 'getAttributes', () => {
  612. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  613. selection.setRanges( [ rangeInFullP ] );
  614. selection.setAttribute( 'foo', 'bar' );
  615. selection.setAttribute( 'abc', 'xyz' );
  616. let attrs = Array.from( selection.getAttributes() );
  617. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  618. } );
  619. } );
  620. describe( 'getAttributeKeys', () => {
  621. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  622. selection.setRanges( [ rangeInFullP ] );
  623. selection.setAttribute( 'foo', 'bar' );
  624. selection.setAttribute( 'abc', 'xyz' );
  625. let attrs = Array.from( selection.getAttributeKeys() );
  626. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  627. } );
  628. } );
  629. describe( 'hasAttribute', () => {
  630. it( 'should return true if element contains attribute with given key', () => {
  631. selection.setRanges( [ rangeInFullP ] );
  632. selection.setAttribute( 'foo', 'bar' );
  633. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  634. } );
  635. it( 'should return false if element does not contain attribute with given key', () => {
  636. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  637. } );
  638. } );
  639. describe( 'clearAttributes', () => {
  640. it( 'should remove all attributes from the element', () => {
  641. selection.setRanges( [ rangeInFullP ] );
  642. selection.setAttribute( 'foo', 'bar' );
  643. selection.setAttribute( 'abc', 'xyz' );
  644. selection.clearAttributes();
  645. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  646. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  647. } );
  648. it( 'should fire change:attribute event with correct parameters', () => {
  649. selection.setAttribute( 'foo', 'bar' );
  650. selection.on( 'change:attribute', ( evt, data ) => {
  651. expect( data.directChange ).to.be.true;
  652. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  653. } );
  654. selection.clearAttributes();
  655. } );
  656. it( 'should not fire change:attribute event if there were no attributes', () => {
  657. let spy = sinon.spy();
  658. selection.on( 'change:attribute', spy );
  659. selection.clearAttributes();
  660. expect( spy.called ).to.be.false;
  661. } );
  662. } );
  663. describe( 'removeAttribute', () => {
  664. it( 'should remove attribute', () => {
  665. selection.setRanges( [ rangeInFullP ] );
  666. selection.setAttribute( 'foo', 'bar' );
  667. selection.removeAttribute( 'foo' );
  668. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  669. } );
  670. it( 'should fire change:attribute event with correct parameters', () => {
  671. selection.setAttribute( 'foo', 'bar' );
  672. selection.on( 'change:attribute', ( evt, data ) => {
  673. expect( data.directChange ).to.be.true;
  674. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  675. } );
  676. selection.removeAttribute( 'foo' );
  677. } );
  678. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  679. let spy = sinon.spy();
  680. selection.on( 'change:attribute', spy );
  681. selection.removeAttribute( 'foo' );
  682. expect( spy.called ).to.be.false;
  683. } );
  684. } );
  685. describe( 'setAttributesTo', () => {
  686. it( 'should remove all attributes set on element and set the given ones', () => {
  687. selection.setAttribute( 'abc', 'xyz' );
  688. selection.setAttributesTo( { foo: 'bar' } );
  689. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  690. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  691. } );
  692. it( 'should fire only one change:attribute event', () => {
  693. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  694. let spy = sinon.spy();
  695. selection.on( 'change:attribute', spy );
  696. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  697. expect( spy.calledOnce ).to.be.true;
  698. } );
  699. it( 'should fire change:attribute event with correct parameters', () => {
  700. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  701. selection.on( 'change:attribute', ( evt, data ) => {
  702. expect( data.directChange ).to.be.true;
  703. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  704. } );
  705. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  706. } );
  707. it( 'should not fire change:attribute event if attributes had not changed', () => {
  708. selection.setRanges( [ rangeInFullP ] );
  709. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  710. let spy = sinon.spy();
  711. selection.on( 'change:attribute', spy );
  712. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  713. expect( spy.called ).to.be.false;
  714. } );
  715. } );
  716. } );
  717. } );