selection.js 29 KB

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