8
0

selection.js 32 KB

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