8
0

selection.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 if added range intersects with already stored range', () => {
  123. selection.addRange( liveRange );
  124. expect( () => {
  125. selection.addRange(
  126. new Range(
  127. new Position( root, [ 0, 4 ] ),
  128. new Position( root, [ 1, 2 ] )
  129. )
  130. );
  131. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  132. } );
  133. } );
  134. describe( 'collapse', () => {
  135. it( 'fires change:range', () => {
  136. const spy = sinon.spy();
  137. selection.on( 'change:range', spy );
  138. selection.collapse( root );
  139. expect( spy.calledOnce ).to.be.true;
  140. } );
  141. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  142. selection.collapse( root );
  143. expect( selection ).to.have.property( 'isCollapsed', true );
  144. const focus = selection.focus;
  145. expect( focus ).to.have.property( 'parent', root );
  146. expect( focus ).to.have.property( 'offset', 0 );
  147. } );
  148. it( 'sets selection at given offset in given parent', () => {
  149. selection.collapse( root, 3 );
  150. expect( selection ).to.have.property( 'isCollapsed', true );
  151. const focus = selection.focus;
  152. expect( focus ).to.have.property( 'parent', root );
  153. expect( focus ).to.have.property( 'offset', 3 );
  154. } );
  155. it( 'sets selection at the end of the given parent', () => {
  156. selection.collapse( root, 'end' );
  157. expect( selection ).to.have.property( 'isCollapsed', true );
  158. const focus = selection.focus;
  159. expect( focus ).to.have.property( 'parent', root );
  160. expect( focus ).to.have.property( 'offset', root.maxOffset );
  161. } );
  162. it( 'sets selection before the specified element', () => {
  163. selection.collapse( root.getChild( 1 ), 'before' );
  164. expect( selection ).to.have.property( 'isCollapsed', true );
  165. const focus = selection.focus;
  166. expect( focus ).to.have.property( 'parent', root );
  167. expect( focus ).to.have.property( 'offset', 1 );
  168. } );
  169. it( 'sets selection after the specified element', () => {
  170. selection.collapse( root.getChild( 1 ), 'after' );
  171. expect( selection ).to.have.property( 'isCollapsed', true );
  172. const focus = selection.focus;
  173. expect( focus ).to.have.property( 'parent', root );
  174. expect( focus ).to.have.property( 'offset', 2 );
  175. } );
  176. it( 'sets selection at the specified position', () => {
  177. const pos = Position.createFromParentAndOffset( root, 3 );
  178. selection.collapse( pos );
  179. expect( selection ).to.have.property( 'isCollapsed', true );
  180. const focus = selection.focus;
  181. expect( focus ).to.have.property( 'parent', root );
  182. expect( focus ).to.have.property( 'offset', 3 );
  183. } );
  184. } );
  185. describe( 'focus', () => {
  186. let r3;
  187. beforeEach( () => {
  188. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  189. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  190. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  191. selection.addRange( r1 );
  192. selection.addRange( r2 );
  193. } );
  194. it( 'should return correct focus when last added range is not backward one', () => {
  195. selection.addRange( r3 );
  196. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  197. } );
  198. it( 'should return correct focus when last added range is backward one', () => {
  199. selection.addRange( r3, true );
  200. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  201. } );
  202. it( 'should return null if no ranges in selection', () => {
  203. selection.removeAllRanges();
  204. expect( selection.focus ).to.be.null;
  205. } );
  206. } );
  207. describe( 'setFocus', () => {
  208. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  209. selection.addRange( range );
  210. selection.addRange( liveRange );
  211. const spy = sinon.spy();
  212. selection.on( 'change:range', spy );
  213. selection.setFocus( selection.focus );
  214. expect( count( selection.getRanges() ) ).to.equal( 2 );
  215. expect( spy.callCount ).to.equal( 0 );
  216. } );
  217. it( 'fires change:range', () => {
  218. selection.addRange( range );
  219. const spy = sinon.spy();
  220. selection.on( 'change:range', spy );
  221. selection.setFocus( Position.createAt( root, 'end' ) );
  222. expect( spy.calledOnce ).to.be.true;
  223. } );
  224. it( 'throws if there are no ranges in selection', () => {
  225. const endPos = Position.createAt( root, 'end' );
  226. expect( () => {
  227. selection.setFocus( endPos );
  228. } ).to.throw( CKEditorError, /model-selection-setFocus-no-ranges/ );
  229. } );
  230. it( 'modifies existing collapsed selection', () => {
  231. const startPos = Position.createAt( root, 1 );
  232. const endPos = Position.createAt( root, 2 );
  233. selection.collapse( startPos );
  234. selection.setFocus( endPos );
  235. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  236. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  237. } );
  238. it( 'makes existing collapsed selection a backward selection', () => {
  239. const startPos = Position.createAt( root, 1 );
  240. const endPos = Position.createAt( root, 0 );
  241. selection.collapse( startPos );
  242. selection.setFocus( endPos );
  243. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  244. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  245. expect( selection.isBackward ).to.be.true;
  246. } );
  247. it( 'modifies existing non-collapsed selection', () => {
  248. const startPos = Position.createAt( root, 1 );
  249. const endPos = Position.createAt( root, 2 );
  250. const newEndPos = Position.createAt( root, 3 );
  251. selection.addRange( new Range( startPos, endPos ) );
  252. selection.setFocus( newEndPos );
  253. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  254. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  255. } );
  256. it( 'makes existing non-collapsed selection a backward selection', () => {
  257. const startPos = Position.createAt( root, 1 );
  258. const endPos = Position.createAt( root, 2 );
  259. const newEndPos = Position.createAt( root, 0 );
  260. selection.addRange( new Range( startPos, endPos ) );
  261. selection.setFocus( newEndPos );
  262. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  263. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  264. expect( selection.isBackward ).to.be.true;
  265. } );
  266. it( 'makes existing backward selection a forward selection', () => {
  267. const startPos = Position.createAt( root, 1 );
  268. const endPos = Position.createAt( root, 2 );
  269. const newEndPos = Position.createAt( root, 3 );
  270. selection.addRange( new Range( startPos, endPos ), true );
  271. selection.setFocus( newEndPos );
  272. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  273. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  274. expect( selection.isBackward ).to.be.false;
  275. } );
  276. it( 'modifies existing backward selection', () => {
  277. const startPos = Position.createAt( root, 1 );
  278. const endPos = Position.createAt( root, 2 );
  279. const newEndPos = Position.createAt( root, 0 );
  280. selection.addRange( new Range( startPos, endPos ), true );
  281. selection.setFocus( newEndPos );
  282. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  283. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  284. expect( selection.isBackward ).to.be.true;
  285. } );
  286. it( 'modifies only the last range', () => {
  287. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  288. const startPos1 = Position.createAt( root, 4 );
  289. const endPos1 = Position.createAt( root, 5 );
  290. const startPos2 = Position.createAt( root, 1 );
  291. const endPos2 = Position.createAt( root, 2 );
  292. const newEndPos = Position.createAt( root, 0 );
  293. selection.addRange( new Range( startPos1, endPos1 ) );
  294. selection.addRange( new Range( startPos2, endPos2 ) );
  295. const spy = sinon.spy();
  296. selection.on( 'change:range', spy );
  297. selection.setFocus( newEndPos );
  298. const ranges = Array.from( selection.getRanges() );
  299. expect( ranges ).to.have.lengthOf( 2 );
  300. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  301. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  302. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  303. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  304. expect( selection.isBackward ).to.be.true;
  305. expect( spy.calledOnce ).to.be.true;
  306. } );
  307. it( 'collapses the selection when extending to the anchor', () => {
  308. const startPos = Position.createAt( root, 1 );
  309. const endPos = Position.createAt( root, 2 );
  310. selection.addRange( new Range( startPos, endPos ) );
  311. selection.setFocus( startPos );
  312. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  313. expect( selection.isCollapsed ).to.be.true;
  314. } );
  315. it( 'uses Position.createAt', () => {
  316. const startPos = Position.createAt( root, 1 );
  317. const endPos = Position.createAt( root, 2 );
  318. const newEndPos = Position.createAt( root, 4 );
  319. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  320. selection.addRange( new Range( startPos, endPos ) );
  321. selection.setFocus( root, 'end' );
  322. expect( spy.calledOnce ).to.be.true;
  323. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  324. } );
  325. } );
  326. describe( 'removeAllRanges', () => {
  327. let spy;
  328. it( 'should remove all stored ranges', () => {
  329. selection.addRange( liveRange );
  330. selection.addRange( range );
  331. selection.removeAllRanges();
  332. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  333. } );
  334. it( 'should fire exactly one change:range event', () => {
  335. selection.addRange( liveRange );
  336. selection.addRange( range );
  337. spy = sinon.spy();
  338. selection.on( 'change:range', spy );
  339. selection.removeAllRanges();
  340. expect( spy.calledOnce ).to.be.true;
  341. } );
  342. it( 'should not fire change:range event if there were no ranges', () => {
  343. spy = sinon.spy();
  344. selection.on( 'change:range', spy );
  345. selection.removeAllRanges();
  346. expect( spy.called ).to.be.false;
  347. } );
  348. } );
  349. describe( 'setRanges', () => {
  350. let newRanges, spy, oldRanges;
  351. beforeEach( () => {
  352. newRanges = [
  353. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  354. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  355. ];
  356. selection.addRange( liveRange );
  357. selection.addRange( range );
  358. spy = sinon.spy();
  359. selection.on( 'change:range', spy );
  360. oldRanges = selection._ranges;
  361. } );
  362. it( 'should remove all ranges and add given ranges', () => {
  363. selection.setRanges( newRanges );
  364. let ranges = Array.from( selection.getRanges() );
  365. expect( ranges ).to.deep.equal( newRanges );
  366. } );
  367. it( 'should use last range from given array to get anchor and focus position', () => {
  368. selection.setRanges( newRanges );
  369. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  370. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  371. } );
  372. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  373. selection.setRanges( newRanges, true );
  374. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  375. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  376. } );
  377. it( 'should fire exactly one change:range event', () => {
  378. selection.setRanges( newRanges );
  379. expect( spy.calledOnce ).to.be.true;
  380. } );
  381. it( 'should fire change:range event with correct parameters', () => {
  382. selection.on( 'change:range', ( evt, data ) => {
  383. expect( data.directChange ).to.be.true;
  384. } );
  385. selection.setRanges( newRanges );
  386. } );
  387. it( 'should not fire change:range event if given ranges are the same', () => {
  388. selection.setRanges( [ liveRange, range ] );
  389. expect( spy.calledOnce ).to.be.false;
  390. } );
  391. } );
  392. describe( 'setTo', () => {
  393. it( 'should set selection to be same as given selection, using setRanges method', () => {
  394. const spy = sinon.spy( selection, 'setRanges' );
  395. const otherSelection = new Selection();
  396. otherSelection.addRange( range1 );
  397. otherSelection.addRange( range2, true );
  398. selection.setTo( otherSelection );
  399. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  400. expect( selection.isBackward ).to.be.true;
  401. expect( selection.setRanges.calledOnce ).to.be.true;
  402. spy.restore();
  403. } );
  404. } );
  405. describe( 'getFirstRange', () => {
  406. it( 'should return null if no ranges were added', () => {
  407. expect( selection.getFirstRange() ).to.be.null;
  408. } );
  409. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  410. // This will not be the first range despite being added as first
  411. selection.addRange( range2 );
  412. // This should be the first range.
  413. selection.addRange( range1 );
  414. // A random range that is not first.
  415. selection.addRange( range3 );
  416. let range = selection.getFirstRange();
  417. expect( range.start.path ).to.deep.equal( [ 1 ] );
  418. expect( range.end.path ).to.deep.equal( [ 4 ] );
  419. } );
  420. } );
  421. describe( 'getFirstPosition', () => {
  422. it( 'should return null if no ranges were added', () => {
  423. expect( selection.getFirstPosition() ).to.be.null;
  424. } );
  425. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  426. // This will not be the first range despite being added as first
  427. selection.addRange( range2 );
  428. // This should be the first range.
  429. selection.addRange( range1 );
  430. // A random range that is not first.
  431. selection.addRange( range3 );
  432. let position = selection.getFirstPosition();
  433. expect( position.path ).to.deep.equal( [ 1 ] );
  434. } );
  435. } );
  436. describe( 'getLastRange', () => {
  437. it( 'should return null if no ranges were added', () => {
  438. expect( selection.getLastRange() ).to.be.null;
  439. } );
  440. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  441. selection.addRange( range3 );
  442. selection.addRange( range1 );
  443. selection.addRange( range2 );
  444. let range = selection.getLastRange();
  445. expect( range.start.path ).to.deep.equal( [ 6 ] );
  446. expect( range.end.path ).to.deep.equal( [ 7 ] );
  447. } );
  448. } );
  449. describe( 'getLastPosition', () => {
  450. it( 'should return null if no ranges were added', () => {
  451. expect( selection.getLastPosition() ).to.be.null;
  452. } );
  453. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  454. selection.addRange( range3 );
  455. selection.addRange( range1 );
  456. selection.addRange( range2 );
  457. let position = selection.getLastPosition();
  458. expect( position.path ).to.deep.equal( [ 7 ] );
  459. } );
  460. } );
  461. describe( 'isEqual', () => {
  462. it( 'should return true if selections equal', () => {
  463. selection.addRange( range1 );
  464. selection.addRange( range2 );
  465. const otherSelection = new Selection();
  466. otherSelection.addRange( range1 );
  467. otherSelection.addRange( range2 );
  468. expect( selection.isEqual( otherSelection ) ).to.be.true;
  469. } );
  470. it( 'should return true if backward selections equal', () => {
  471. selection.addRange( range1, true );
  472. const otherSelection = new Selection();
  473. otherSelection.addRange( range1, true );
  474. expect( selection.isEqual( otherSelection ) ).to.be.true;
  475. } );
  476. it( 'should return false if ranges count does not equal', () => {
  477. selection.addRange( range1 );
  478. selection.addRange( range2 );
  479. const otherSelection = new Selection();
  480. otherSelection.addRange( range1 );
  481. expect( selection.isEqual( otherSelection ) ).to.be.false;
  482. } );
  483. it( 'should return false if ranges do not equal', () => {
  484. selection.addRange( range1 );
  485. const otherSelection = new Selection();
  486. otherSelection.addRange( range2 );
  487. expect( selection.isEqual( otherSelection ) ).to.be.false;
  488. } );
  489. it( 'should return false if directions do not equal', () => {
  490. selection.addRange( range1 );
  491. const otherSelection = new Selection();
  492. otherSelection.addRange( range1, true );
  493. expect( selection.isEqual( otherSelection ) ).to.be.false;
  494. } );
  495. } );
  496. describe( 'collapseToStart', () => {
  497. it( 'should collapse to start position and fire change event', () => {
  498. selection.setRanges( [ range2, range1, range3 ] );
  499. const spy = sinon.spy();
  500. selection.on( 'change:range', spy );
  501. selection.collapseToStart();
  502. expect( selection.rangeCount ).to.equal( 1 );
  503. expect( selection.isCollapsed ).to.be.true;
  504. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  505. expect( spy.calledOnce ).to.be.true;
  506. } );
  507. it( 'should do nothing if selection was already collapsed', () => {
  508. selection.collapse( range1.start );
  509. const spy = sinon.spy( selection, 'fire' );
  510. selection.collapseToStart();
  511. expect( spy.notCalled ).to.be.true;
  512. spy.restore();
  513. } );
  514. it( 'should do nothing if no ranges present', () => {
  515. const spy = sinon.spy( selection, 'fire' );
  516. selection.collapseToStart();
  517. spy.restore();
  518. expect( spy.notCalled ).to.be.true;
  519. } );
  520. } );
  521. describe( 'collapseToEnd', () => {
  522. it( 'should collapse to start position and fire change:range event', () => {
  523. selection.setRanges( [ range2, range3, range1 ] );
  524. const spy = sinon.spy();
  525. selection.on( 'change:range', spy );
  526. selection.collapseToEnd();
  527. expect( selection.rangeCount ).to.equal( 1 );
  528. expect( selection.isCollapsed ).to.be.true;
  529. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  530. expect( spy.calledOnce ).to.be.true;
  531. } );
  532. it( 'should do nothing if selection was already collapsed', () => {
  533. selection.collapse( range1.start );
  534. const spy = sinon.spy( selection, 'fire' );
  535. selection.collapseToEnd();
  536. expect( spy.notCalled ).to.be.true;
  537. spy.restore();
  538. } );
  539. it( 'should do nothing if selection has no ranges', () => {
  540. const spy = sinon.spy( selection, 'fire' );
  541. selection.collapseToEnd();
  542. expect( spy.notCalled ).to.be.true;
  543. spy.restore();
  544. } );
  545. } );
  546. describe( 'createFromSelection', () => {
  547. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  548. selection.addRange( liveRange );
  549. selection.addRange( range, true );
  550. const snapshot = Selection.createFromSelection( selection );
  551. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  552. const selectionRanges = Array.from( selection.getRanges() );
  553. const snapshotRanges = Array.from( snapshot.getRanges() );
  554. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  555. for ( let i = 0; i < selectionRanges.length; i++ ) {
  556. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  557. }
  558. } );
  559. } );
  560. describe( 'attributes interface', () => {
  561. let rangeInFullP;
  562. beforeEach( () => {
  563. root.insertChildren( 0, [
  564. new Element( 'p', [], new Text( 'foobar' ) ),
  565. new Element( 'p', [], [] )
  566. ] );
  567. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  568. } );
  569. describe( 'setAttribute', () => {
  570. it( 'should set given attribute on the selection', () => {
  571. selection.setRanges( [ rangeInFullP ] );
  572. selection.setAttribute( 'foo', 'bar' );
  573. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  574. } );
  575. it( 'should fire change:attribute event with correct parameters', () => {
  576. selection.on( 'change:attribute', ( evt, data ) => {
  577. expect( data.directChange ).to.be.true;
  578. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  579. } );
  580. selection.setAttribute( 'foo', 'bar' );
  581. } );
  582. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  583. selection.setAttribute( 'foo', 'bar' );
  584. let spy = sinon.spy();
  585. selection.on( 'change:attribute', spy );
  586. selection.setAttribute( 'foo', 'bar' );
  587. expect( spy.called ).to.be.false;
  588. } );
  589. } );
  590. describe( 'getAttribute', () => {
  591. it( 'should return undefined if element does not contain given attribute', () => {
  592. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  593. } );
  594. } );
  595. describe( 'getAttributes', () => {
  596. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  597. selection.setRanges( [ rangeInFullP ] );
  598. selection.setAttribute( 'foo', 'bar' );
  599. selection.setAttribute( 'abc', 'xyz' );
  600. let attrs = Array.from( selection.getAttributes() );
  601. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  602. } );
  603. } );
  604. describe( 'getAttributeKeys', () => {
  605. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  606. selection.setRanges( [ rangeInFullP ] );
  607. selection.setAttribute( 'foo', 'bar' );
  608. selection.setAttribute( 'abc', 'xyz' );
  609. let attrs = Array.from( selection.getAttributeKeys() );
  610. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  611. } );
  612. } );
  613. describe( 'hasAttribute', () => {
  614. it( 'should return true if element contains attribute with given key', () => {
  615. selection.setRanges( [ rangeInFullP ] );
  616. selection.setAttribute( 'foo', 'bar' );
  617. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  618. } );
  619. it( 'should return false if element does not contain attribute with given key', () => {
  620. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  621. } );
  622. } );
  623. describe( 'clearAttributes', () => {
  624. it( 'should remove all attributes from the element', () => {
  625. selection.setRanges( [ rangeInFullP ] );
  626. selection.setAttribute( 'foo', 'bar' );
  627. selection.setAttribute( 'abc', 'xyz' );
  628. selection.clearAttributes();
  629. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  630. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  631. } );
  632. it( 'should fire change:attribute event with correct parameters', () => {
  633. selection.setAttribute( 'foo', 'bar' );
  634. selection.on( 'change:attribute', ( evt, data ) => {
  635. expect( data.directChange ).to.be.true;
  636. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  637. } );
  638. selection.clearAttributes();
  639. } );
  640. it( 'should not fire change:attribute event if there were no attributes', () => {
  641. let spy = sinon.spy();
  642. selection.on( 'change:attribute', spy );
  643. selection.clearAttributes();
  644. expect( spy.called ).to.be.false;
  645. } );
  646. } );
  647. describe( 'removeAttribute', () => {
  648. it( 'should remove attribute', () => {
  649. selection.setRanges( [ rangeInFullP ] );
  650. selection.setAttribute( 'foo', 'bar' );
  651. selection.removeAttribute( 'foo' );
  652. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  653. } );
  654. it( 'should fire change:attribute event with correct parameters', () => {
  655. selection.setAttribute( 'foo', 'bar' );
  656. selection.on( 'change:attribute', ( evt, data ) => {
  657. expect( data.directChange ).to.be.true;
  658. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  659. } );
  660. selection.removeAttribute( 'foo' );
  661. } );
  662. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  663. let spy = sinon.spy();
  664. selection.on( 'change:attribute', spy );
  665. selection.removeAttribute( 'foo' );
  666. expect( spy.called ).to.be.false;
  667. } );
  668. } );
  669. describe( 'setAttributesTo', () => {
  670. it( 'should remove all attributes set on element and set the given ones', () => {
  671. selection.setAttribute( 'abc', 'xyz' );
  672. selection.setAttributesTo( { foo: 'bar' } );
  673. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  674. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  675. } );
  676. it( 'should fire only one change:attribute event', () => {
  677. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  678. let spy = sinon.spy();
  679. selection.on( 'change:attribute', spy );
  680. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  681. expect( spy.calledOnce ).to.be.true;
  682. } );
  683. it( 'should fire change:attribute event with correct parameters', () => {
  684. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  685. selection.on( 'change:attribute', ( evt, data ) => {
  686. expect( data.directChange ).to.be.true;
  687. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  688. } );
  689. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  690. } );
  691. it( 'should not fire change:attribute event if attributes had not changed', () => {
  692. selection.setRanges( [ rangeInFullP ] );
  693. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  694. let spy = sinon.spy();
  695. selection.on( 'change:attribute', spy );
  696. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  697. expect( spy.called ).to.be.false;
  698. } );
  699. } );
  700. } );
  701. } );