selection.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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( 'getSelectedElement', () => {
  719. let schema;
  720. beforeEach( () => {
  721. schema = new Schema();
  722. schema.registerItem( 'p', '$block' );
  723. } );
  724. it( 'should return selected element', () => {
  725. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  726. const p = model.getChild( 1 );
  727. expect( selection.getSelectedElement() ).to.equal( p );
  728. } );
  729. it( 'should return null if there is more than one range', () => {
  730. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  731. expect( selection.getSelectedElement() ).to.be.null;
  732. } );
  733. it( 'should return null if there is no selection', () => {
  734. expect( selection.getSelectedElement() ).to.be.null;
  735. } );
  736. it( 'should return null if selection is not over single element #1', () => {
  737. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  738. expect( selection.getSelectedElement() ).to.be.null;
  739. } );
  740. it( 'should return null if selection is not over single element #2', () => {
  741. const { selection } = parse( '<p>{bar}</p>', schema );
  742. expect( selection.getSelectedElement() ).to.be.null;
  743. } );
  744. } );
  745. } );