selection.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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, setData } 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( 'constructor()', () => {
  44. it( 'should be able to create an empty selection', () => {
  45. const selection = new Selection();
  46. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  47. } );
  48. it( 'should be able to create a selection from the given ranges', () => {
  49. const ranges = [ range1, range2, range3 ];
  50. const selection = new Selection( ranges );
  51. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  52. } );
  53. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  54. const ranges = [ range1, range2, range3 ];
  55. const selection = new Selection( ranges, true );
  56. expect( selection.isBackward ).to.be.true;
  57. } );
  58. } );
  59. describe( 'isCollapsed', () => {
  60. it( 'should return false for empty selection', () => {
  61. expect( selection.isCollapsed ).to.be.false;
  62. } );
  63. it( 'should return true when there is single collapsed ranges', () => {
  64. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  65. expect( selection.isCollapsed ).to.be.true;
  66. } );
  67. it( 'should return false when there are multiple ranges', () => {
  68. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  69. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  70. expect( selection.isCollapsed ).to.be.false;
  71. } );
  72. it( 'should return false when there is not collapsed range', () => {
  73. selection.addRange( range );
  74. expect( selection.isCollapsed ).to.be.false;
  75. } );
  76. } );
  77. describe( 'rangeCount', () => {
  78. it( 'should return proper range count', () => {
  79. expect( selection.rangeCount ).to.equal( 0 );
  80. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  81. expect( selection.rangeCount ).to.equal( 1 );
  82. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  83. expect( selection.rangeCount ).to.equal( 2 );
  84. } );
  85. } );
  86. describe( 'isBackward', () => {
  87. it( 'is defined by the last added range', () => {
  88. selection.addRange( range, true );
  89. expect( selection ).to.have.property( 'isBackward', true );
  90. selection.addRange( liveRange );
  91. expect( selection ).to.have.property( 'isBackward', false );
  92. } );
  93. it( 'is false when last range is collapsed', () => {
  94. const pos = Position.createAt( root, 0 );
  95. selection.addRange( new Range( pos, pos ), true );
  96. expect( selection.isBackward ).to.be.false;
  97. } );
  98. } );
  99. describe( 'focus', () => {
  100. let r3;
  101. beforeEach( () => {
  102. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  103. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  104. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  105. selection.addRange( r1 );
  106. selection.addRange( r2 );
  107. } );
  108. it( 'should return correct focus when last added range is not backward one', () => {
  109. selection.addRange( r3 );
  110. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  111. } );
  112. it( 'should return correct focus when last added range is backward one', () => {
  113. selection.addRange( r3, true );
  114. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  115. } );
  116. it( 'should return null if no ranges in selection', () => {
  117. selection.removeAllRanges();
  118. expect( selection.focus ).to.be.null;
  119. } );
  120. } );
  121. describe( 'addRange()', () => {
  122. it( 'should copy added ranges and store multiple ranges', () => {
  123. selection.addRange( liveRange );
  124. selection.addRange( range );
  125. const ranges = selection._ranges;
  126. expect( ranges.length ).to.equal( 2 );
  127. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  128. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  129. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  130. expect( ranges[ 1 ] ).not.to.be.equal( range );
  131. } );
  132. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  133. selection.addRange( liveRange );
  134. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  135. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  136. selection.addRange( range );
  137. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  138. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  139. } );
  140. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  141. selection.addRange( liveRange, true );
  142. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  143. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  144. selection.addRange( range, true );
  145. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  146. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  147. } );
  148. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  149. selection.addRange( liveRange );
  150. const ranges = Array.from( selection.getRanges() );
  151. selection.addRange( range );
  152. expect( ranges.length ).to.equal( 1 );
  153. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  154. } );
  155. it( 'should fire change:range event when adding a range', () => {
  156. const spy = sinon.spy();
  157. selection.on( 'change:range', spy );
  158. selection.addRange( range );
  159. expect( spy.called ).to.be.true;
  160. } );
  161. it( 'should throw an error when range is invalid', () => {
  162. expect( () => {
  163. selection.addRange( { invalid: 'range' } );
  164. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  165. } );
  166. it( 'should throw an error if added range intersects with already stored range', () => {
  167. selection.addRange( liveRange );
  168. expect( () => {
  169. selection.addRange(
  170. new Range(
  171. new Position( root, [ 0, 4 ] ),
  172. new Position( root, [ 1, 2 ] )
  173. )
  174. );
  175. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  176. } );
  177. } );
  178. describe( 'collapse()', () => {
  179. it( 'fires change:range', () => {
  180. const spy = sinon.spy();
  181. selection.on( 'change:range', spy );
  182. selection.collapse( root );
  183. expect( spy.calledOnce ).to.be.true;
  184. } );
  185. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  186. selection.collapse( root );
  187. expect( selection ).to.have.property( 'isCollapsed', true );
  188. const focus = selection.focus;
  189. expect( focus ).to.have.property( 'parent', root );
  190. expect( focus ).to.have.property( 'offset', 0 );
  191. } );
  192. it( 'sets selection at given offset in given parent', () => {
  193. selection.collapse( root, 3 );
  194. expect( selection ).to.have.property( 'isCollapsed', true );
  195. const focus = selection.focus;
  196. expect( focus ).to.have.property( 'parent', root );
  197. expect( focus ).to.have.property( 'offset', 3 );
  198. } );
  199. it( 'sets selection at the end of the given parent', () => {
  200. selection.collapse( root, 'end' );
  201. expect( selection ).to.have.property( 'isCollapsed', true );
  202. const focus = selection.focus;
  203. expect( focus ).to.have.property( 'parent', root );
  204. expect( focus ).to.have.property( 'offset', root.maxOffset );
  205. } );
  206. it( 'sets selection before the specified element', () => {
  207. selection.collapse( root.getChild( 1 ), 'before' );
  208. expect( selection ).to.have.property( 'isCollapsed', true );
  209. const focus = selection.focus;
  210. expect( focus ).to.have.property( 'parent', root );
  211. expect( focus ).to.have.property( 'offset', 1 );
  212. } );
  213. it( 'sets selection after the specified element', () => {
  214. selection.collapse( root.getChild( 1 ), 'after' );
  215. expect( selection ).to.have.property( 'isCollapsed', true );
  216. const focus = selection.focus;
  217. expect( focus ).to.have.property( 'parent', root );
  218. expect( focus ).to.have.property( 'offset', 2 );
  219. } );
  220. it( 'sets selection at the specified position', () => {
  221. const pos = Position.createFromParentAndOffset( root, 3 );
  222. selection.collapse( pos );
  223. expect( selection ).to.have.property( 'isCollapsed', true );
  224. const focus = selection.focus;
  225. expect( focus ).to.have.property( 'parent', root );
  226. expect( focus ).to.have.property( 'offset', 3 );
  227. } );
  228. } );
  229. describe( 'setFocus()', () => {
  230. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  231. selection.addRange( range );
  232. selection.addRange( liveRange );
  233. const spy = sinon.spy();
  234. selection.on( 'change:range', spy );
  235. selection.setFocus( selection.focus );
  236. expect( count( selection.getRanges() ) ).to.equal( 2 );
  237. expect( spy.callCount ).to.equal( 0 );
  238. } );
  239. it( 'fires change:range', () => {
  240. selection.addRange( range );
  241. const spy = sinon.spy();
  242. selection.on( 'change:range', spy );
  243. selection.setFocus( Position.createAt( root, 'end' ) );
  244. expect( spy.calledOnce ).to.be.true;
  245. } );
  246. it( 'throws if there are no ranges in selection', () => {
  247. const endPos = Position.createAt( root, 'end' );
  248. expect( () => {
  249. selection.setFocus( endPos );
  250. } ).to.throw( CKEditorError, /model-selection-setFocus-no-ranges/ );
  251. } );
  252. it( 'modifies existing collapsed selection', () => {
  253. const startPos = Position.createAt( root, 1 );
  254. const endPos = Position.createAt( root, 2 );
  255. selection.collapse( startPos );
  256. selection.setFocus( endPos );
  257. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  258. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  259. } );
  260. it( 'makes existing collapsed selection a backward selection', () => {
  261. const startPos = Position.createAt( root, 1 );
  262. const endPos = Position.createAt( root, 0 );
  263. selection.collapse( startPos );
  264. selection.setFocus( endPos );
  265. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  266. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  267. expect( selection.isBackward ).to.be.true;
  268. } );
  269. it( 'modifies existing non-collapsed selection', () => {
  270. const startPos = Position.createAt( root, 1 );
  271. const endPos = Position.createAt( root, 2 );
  272. const newEndPos = Position.createAt( root, 3 );
  273. selection.addRange( new Range( startPos, endPos ) );
  274. selection.setFocus( newEndPos );
  275. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  276. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  277. } );
  278. it( 'makes existing non-collapsed selection a backward selection', () => {
  279. const startPos = Position.createAt( root, 1 );
  280. const endPos = Position.createAt( root, 2 );
  281. const newEndPos = Position.createAt( root, 0 );
  282. selection.addRange( new Range( startPos, endPos ) );
  283. selection.setFocus( newEndPos );
  284. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  285. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  286. expect( selection.isBackward ).to.be.true;
  287. } );
  288. it( 'makes existing backward selection a forward selection', () => {
  289. const startPos = Position.createAt( root, 1 );
  290. const endPos = Position.createAt( root, 2 );
  291. const newEndPos = Position.createAt( root, 3 );
  292. selection.addRange( new Range( startPos, endPos ), true );
  293. selection.setFocus( newEndPos );
  294. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  295. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  296. expect( selection.isBackward ).to.be.false;
  297. } );
  298. it( 'modifies existing backward selection', () => {
  299. const startPos = Position.createAt( root, 1 );
  300. const endPos = Position.createAt( root, 2 );
  301. const newEndPos = Position.createAt( root, 0 );
  302. selection.addRange( new Range( startPos, endPos ), true );
  303. selection.setFocus( newEndPos );
  304. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  305. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  306. expect( selection.isBackward ).to.be.true;
  307. } );
  308. it( 'modifies only the last range', () => {
  309. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  310. const startPos1 = Position.createAt( root, 4 );
  311. const endPos1 = Position.createAt( root, 5 );
  312. const startPos2 = Position.createAt( root, 1 );
  313. const endPos2 = Position.createAt( root, 2 );
  314. const newEndPos = Position.createAt( root, 0 );
  315. selection.addRange( new Range( startPos1, endPos1 ) );
  316. selection.addRange( new Range( startPos2, endPos2 ) );
  317. const spy = sinon.spy();
  318. selection.on( 'change:range', spy );
  319. selection.setFocus( newEndPos );
  320. const ranges = Array.from( selection.getRanges() );
  321. expect( ranges ).to.have.lengthOf( 2 );
  322. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  323. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  324. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  325. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  326. expect( selection.isBackward ).to.be.true;
  327. expect( spy.calledOnce ).to.be.true;
  328. } );
  329. it( 'collapses the selection when extending to the anchor', () => {
  330. const startPos = Position.createAt( root, 1 );
  331. const endPos = Position.createAt( root, 2 );
  332. selection.addRange( new Range( startPos, endPos ) );
  333. selection.setFocus( startPos );
  334. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  335. expect( selection.isCollapsed ).to.be.true;
  336. } );
  337. it( 'uses Position.createAt', () => {
  338. const startPos = Position.createAt( root, 1 );
  339. const endPos = Position.createAt( root, 2 );
  340. const newEndPos = Position.createAt( root, 4 );
  341. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  342. selection.addRange( new Range( startPos, endPos ) );
  343. selection.setFocus( root, 'end' );
  344. expect( spy.calledOnce ).to.be.true;
  345. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  346. } );
  347. } );
  348. describe( 'removeAllRanges()', () => {
  349. let spy;
  350. it( 'should remove all stored ranges', () => {
  351. selection.addRange( liveRange );
  352. selection.addRange( range );
  353. selection.removeAllRanges();
  354. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  355. } );
  356. it( 'should fire exactly one change:range event', () => {
  357. selection.addRange( liveRange );
  358. selection.addRange( range );
  359. spy = sinon.spy();
  360. selection.on( 'change:range', spy );
  361. selection.removeAllRanges();
  362. expect( spy.calledOnce ).to.be.true;
  363. } );
  364. it( 'should not fire change:range event if there were no ranges', () => {
  365. spy = sinon.spy();
  366. selection.on( 'change:range', spy );
  367. selection.removeAllRanges();
  368. expect( spy.called ).to.be.false;
  369. } );
  370. } );
  371. describe( 'setRanges()', () => {
  372. let newRanges, spy;
  373. beforeEach( () => {
  374. newRanges = [
  375. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  376. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  377. ];
  378. selection.addRange( liveRange );
  379. selection.addRange( range );
  380. spy = sinon.spy();
  381. selection.on( 'change:range', spy );
  382. } );
  383. it( 'should throw an error when range is invalid', () => {
  384. expect( () => {
  385. selection.setRanges( [ { invalid: 'range' } ] );
  386. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  387. } );
  388. it( 'should remove all ranges and add given ranges', () => {
  389. selection.setRanges( newRanges );
  390. const ranges = Array.from( selection.getRanges() );
  391. expect( ranges ).to.deep.equal( newRanges );
  392. } );
  393. it( 'should use last range from given array to get anchor and focus position', () => {
  394. selection.setRanges( newRanges );
  395. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  396. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  397. } );
  398. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  399. selection.setRanges( newRanges, true );
  400. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  401. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  402. } );
  403. it( 'should fire exactly one change:range event', () => {
  404. selection.setRanges( newRanges );
  405. expect( spy.calledOnce ).to.be.true;
  406. } );
  407. it( 'should fire change:range event with correct parameters', () => {
  408. selection.on( 'change:range', ( evt, data ) => {
  409. expect( data.directChange ).to.be.true;
  410. } );
  411. selection.setRanges( newRanges );
  412. } );
  413. it( 'should not fire change:range event if given ranges are the same', () => {
  414. selection.setRanges( [ liveRange, range ] );
  415. expect( spy.calledOnce ).to.be.false;
  416. } );
  417. } );
  418. describe( 'setTo()', () => {
  419. it( 'should set selection to be same as given selection, using setRanges method', () => {
  420. const spy = sinon.spy( selection, 'setRanges' );
  421. const otherSelection = new Selection();
  422. otherSelection.addRange( range1 );
  423. otherSelection.addRange( range2, true );
  424. selection.setTo( otherSelection );
  425. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  426. expect( selection.isBackward ).to.be.true;
  427. expect( selection.setRanges.calledOnce ).to.be.true;
  428. spy.restore();
  429. } );
  430. } );
  431. describe( 'getFirstRange()', () => {
  432. it( 'should return null if no ranges were added', () => {
  433. expect( selection.getFirstRange() ).to.be.null;
  434. } );
  435. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  436. // This will not be the first range despite being added as first
  437. selection.addRange( range2 );
  438. // This should be the first range.
  439. selection.addRange( range1 );
  440. // A random range that is not first.
  441. selection.addRange( range3 );
  442. const range = selection.getFirstRange();
  443. expect( range.start.path ).to.deep.equal( [ 1 ] );
  444. expect( range.end.path ).to.deep.equal( [ 4 ] );
  445. } );
  446. } );
  447. describe( 'getFirstPosition()', () => {
  448. it( 'should return null if no ranges were added', () => {
  449. expect( selection.getFirstPosition() ).to.be.null;
  450. } );
  451. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  452. // This will not be the first range despite being added as first
  453. selection.addRange( range2 );
  454. // This should be the first range.
  455. selection.addRange( range1 );
  456. // A random range that is not first.
  457. selection.addRange( range3 );
  458. const position = selection.getFirstPosition();
  459. expect( position.path ).to.deep.equal( [ 1 ] );
  460. } );
  461. } );
  462. describe( 'getLastRange()', () => {
  463. it( 'should return null if no ranges were added', () => {
  464. expect( selection.getLastRange() ).to.be.null;
  465. } );
  466. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  467. selection.addRange( range3 );
  468. selection.addRange( range1 );
  469. selection.addRange( range2 );
  470. const range = selection.getLastRange();
  471. expect( range.start.path ).to.deep.equal( [ 6 ] );
  472. expect( range.end.path ).to.deep.equal( [ 7 ] );
  473. } );
  474. } );
  475. describe( 'getLastPosition()', () => {
  476. it( 'should return null if no ranges were added', () => {
  477. expect( selection.getLastPosition() ).to.be.null;
  478. } );
  479. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  480. selection.addRange( range3 );
  481. selection.addRange( range1 );
  482. selection.addRange( range2 );
  483. const position = selection.getLastPosition();
  484. expect( position.path ).to.deep.equal( [ 7 ] );
  485. } );
  486. } );
  487. describe( 'isEqual()', () => {
  488. it( 'should return true if selections equal', () => {
  489. selection.addRange( range1 );
  490. selection.addRange( range2 );
  491. const otherSelection = new Selection();
  492. otherSelection.addRange( range1 );
  493. otherSelection.addRange( range2 );
  494. expect( selection.isEqual( otherSelection ) ).to.be.true;
  495. } );
  496. it( 'should return true if backward selections equal', () => {
  497. selection.addRange( range1, true );
  498. const otherSelection = new Selection();
  499. otherSelection.addRange( range1, true );
  500. expect( selection.isEqual( otherSelection ) ).to.be.true;
  501. } );
  502. it( 'should return true if both selections have no ranges', () => {
  503. const otherSelection = new Selection();
  504. expect( selection.isEqual( otherSelection ) ).to.be.true;
  505. } );
  506. it( 'should return false if ranges count does not equal', () => {
  507. selection.addRange( range1 );
  508. selection.addRange( range2 );
  509. const otherSelection = new Selection();
  510. otherSelection.addRange( range2 );
  511. expect( selection.isEqual( otherSelection ) ).to.be.false;
  512. } );
  513. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  514. selection.addRange( range1 );
  515. selection.addRange( range3 );
  516. const otherSelection = new Selection();
  517. otherSelection.addRange( range2 );
  518. otherSelection.addRange( range3 );
  519. expect( selection.isEqual( otherSelection ) ).to.be.false;
  520. } );
  521. it( 'should return false if directions do not equal', () => {
  522. selection.addRange( range1 );
  523. const otherSelection = new Selection();
  524. otherSelection.addRange( range1, true );
  525. expect( selection.isEqual( otherSelection ) ).to.be.false;
  526. } );
  527. } );
  528. describe( 'collapseToStart()', () => {
  529. it( 'should collapse to start position and fire change event', () => {
  530. selection.setRanges( [ range2, range1, range3 ] );
  531. const spy = sinon.spy();
  532. selection.on( 'change:range', spy );
  533. selection.collapseToStart();
  534. expect( selection.rangeCount ).to.equal( 1 );
  535. expect( selection.isCollapsed ).to.be.true;
  536. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  537. expect( spy.calledOnce ).to.be.true;
  538. } );
  539. it( 'should do nothing if selection was already collapsed', () => {
  540. selection.collapse( range1.start );
  541. const spy = sinon.spy( selection, 'fire' );
  542. selection.collapseToStart();
  543. expect( spy.notCalled ).to.be.true;
  544. spy.restore();
  545. } );
  546. it( 'should do nothing if no ranges present', () => {
  547. const spy = sinon.spy( selection, 'fire' );
  548. selection.collapseToStart();
  549. spy.restore();
  550. expect( spy.notCalled ).to.be.true;
  551. } );
  552. } );
  553. describe( 'collapseToEnd()', () => {
  554. it( 'should collapse to start position and fire change:range event', () => {
  555. selection.setRanges( [ range2, range3, range1 ] );
  556. const spy = sinon.spy();
  557. selection.on( 'change:range', spy );
  558. selection.collapseToEnd();
  559. expect( selection.rangeCount ).to.equal( 1 );
  560. expect( selection.isCollapsed ).to.be.true;
  561. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  562. expect( spy.calledOnce ).to.be.true;
  563. } );
  564. it( 'should do nothing if selection was already collapsed', () => {
  565. selection.collapse( range1.start );
  566. const spy = sinon.spy( selection, 'fire' );
  567. selection.collapseToEnd();
  568. expect( spy.notCalled ).to.be.true;
  569. spy.restore();
  570. } );
  571. it( 'should do nothing if selection has no ranges', () => {
  572. const spy = sinon.spy( selection, 'fire' );
  573. selection.collapseToEnd();
  574. expect( spy.notCalled ).to.be.true;
  575. spy.restore();
  576. } );
  577. } );
  578. describe( 'createFromSelection()', () => {
  579. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  580. selection.addRange( liveRange );
  581. selection.addRange( range, true );
  582. const snapshot = Selection.createFromSelection( selection );
  583. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  584. const selectionRanges = Array.from( selection.getRanges() );
  585. const snapshotRanges = Array.from( snapshot.getRanges() );
  586. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  587. for ( let i = 0; i < selectionRanges.length; i++ ) {
  588. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  589. }
  590. } );
  591. } );
  592. describe( 'getSelectedElement()', () => {
  593. let schema;
  594. beforeEach( () => {
  595. schema = new Schema();
  596. schema.registerItem( 'p', '$block' );
  597. } );
  598. it( 'should return selected element', () => {
  599. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  600. const p = model.getChild( 1 );
  601. expect( selection.getSelectedElement() ).to.equal( p );
  602. } );
  603. it( 'should return null if there is more than one range', () => {
  604. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  605. expect( selection.getSelectedElement() ).to.be.null;
  606. } );
  607. it( 'should return null if there is no selection', () => {
  608. expect( selection.getSelectedElement() ).to.be.null;
  609. } );
  610. it( 'should return null if selection is not over single element #1', () => {
  611. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  612. expect( selection.getSelectedElement() ).to.be.null;
  613. } );
  614. it( 'should return null if selection is not over single element #2', () => {
  615. const { selection } = parse( '<p>{bar}</p>', schema );
  616. expect( selection.getSelectedElement() ).to.be.null;
  617. } );
  618. } );
  619. describe( 'getSelectedBlocks()', () => {
  620. beforeEach( () => {
  621. doc.schema.registerItem( 'p', '$block' );
  622. doc.schema.registerItem( 'h', '$block' );
  623. doc.schema.registerItem( 'blockquote' );
  624. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  625. doc.schema.allow( { name: '$block', inside: 'blockquote' } );
  626. doc.schema.registerItem( 'image' );
  627. doc.schema.allow( { name: 'image', inside: '$root' } );
  628. doc.schema.allow( { name: 'image', inside: '$block' } );
  629. doc.schema.allow( { name: '$text', inside: 'image' } );
  630. // Special block which can contain another blocks.
  631. doc.schema.registerItem( 'nestedBlock', '$block' );
  632. doc.schema.allow( { name: 'nestedBlock', inside: '$block' } );
  633. } );
  634. it( 'returns an iterator', () => {
  635. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  636. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  637. } );
  638. it( 'returns block for a collapsed selection', () => {
  639. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  640. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  641. } );
  642. it( 'returns block for a collapsed selection (empty block)', () => {
  643. setData( doc, '<p>a</p><p>[]</p><p>c</p>' );
  644. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  645. expect( blocks ).to.have.length( 1 );
  646. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  647. } );
  648. it( 'returns block for a non collapsed selection', () => {
  649. setData( doc, '<p>a</p><p>[b]</p><p>c</p>' );
  650. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  651. } );
  652. it( 'returns two blocks for a non collapsed selection', () => {
  653. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  654. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  655. } );
  656. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  657. setData( doc, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  658. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  659. } );
  660. it( 'returns proper block for a multi-range selection', () => {
  661. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  662. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  663. } );
  664. it( 'does not return a block twice if two ranges are anchored in it', () => {
  665. setData( doc, '<p>[a]b[c]</p>' );
  666. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  667. } );
  668. it( 'returns only blocks', () => {
  669. setData( doc, '<p>[a</p><image>b</image><p>c]</p>' );
  670. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  671. } );
  672. it( 'gets deeper into the tree', () => {
  673. setData( doc, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  674. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  675. } );
  676. it( 'gets deeper into the tree (end deeper)', () => {
  677. setData( doc, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  678. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  679. } );
  680. it( 'gets deeper into the tree (start deeper)', () => {
  681. setData( doc, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  682. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  683. } );
  684. it( 'returns an empty array if none of the selected elements is a block', () => {
  685. setData( doc, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  686. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  687. } );
  688. it( 'returns an empty array if the selected element is not a block', () => {
  689. setData( doc, '<p>a</p><image>[]a</image><p>b</p>' );
  690. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  691. } );
  692. // Super edge case – should not happen (blocks should never be nested),
  693. // but since the code handles it already it's worth testing.
  694. it( 'returns only the lowest block if blocks are nested', () => {
  695. setData( doc, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  696. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  697. } );
  698. // Like above but trickier.
  699. it( 'returns only the lowest block if blocks are nested', () => {
  700. setData(
  701. doc,
  702. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  703. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  704. );
  705. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  706. } );
  707. it( 'returns nothing if directly in a root', () => {
  708. doc.createRoot( 'p', 'inlineOnlyRoot' );
  709. setData( doc, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  710. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  711. } );
  712. describe( '#984', () => {
  713. it( 'does not return the last block if none of its content is selected', () => {
  714. setData( doc, '<p>[a</p><p>b</p><p>]c</p>' );
  715. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  716. } );
  717. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  718. setData( doc, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  719. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  720. } );
  721. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  722. setData( doc, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  723. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  724. } );
  725. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  726. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  727. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  728. setData( doc, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  729. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  730. } );
  731. it( 'returns the last block if at least one of its child nodes is selected', () => {
  732. setData( doc, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  733. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  734. } );
  735. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  736. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  737. setData( doc, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  738. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  739. } );
  740. it(
  741. 'does not return the last block if at least one of its child nodes is selected ' +
  742. '(end in an inline element, no content selected)',
  743. () => {
  744. setData( doc, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  745. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  746. }
  747. );
  748. } );
  749. // Map all elements to data of its first child text node.
  750. function toText( elements ) {
  751. return Array.from( elements ).map( el => {
  752. return Array.from( el.getChildren() ).find( child => child.data ).data;
  753. } );
  754. }
  755. } );
  756. describe( 'attributes interface', () => {
  757. let rangeInFullP;
  758. beforeEach( () => {
  759. root.insertChildren( 0, [
  760. new Element( 'p', [], new Text( 'foobar' ) ),
  761. new Element( 'p', [], [] )
  762. ] );
  763. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  764. } );
  765. describe( 'setAttribute()', () => {
  766. it( 'should set given attribute on the selection', () => {
  767. selection.setRanges( [ rangeInFullP ] );
  768. selection.setAttribute( 'foo', 'bar' );
  769. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  770. } );
  771. it( 'should fire change:attribute event with correct parameters', () => {
  772. selection.on( 'change:attribute', ( evt, data ) => {
  773. expect( data.directChange ).to.be.true;
  774. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  775. } );
  776. selection.setAttribute( 'foo', 'bar' );
  777. } );
  778. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  779. selection.setAttribute( 'foo', 'bar' );
  780. const spy = sinon.spy();
  781. selection.on( 'change:attribute', spy );
  782. selection.setAttribute( 'foo', 'bar' );
  783. expect( spy.called ).to.be.false;
  784. } );
  785. } );
  786. describe( 'getAttribute()', () => {
  787. it( 'should return undefined if element does not contain given attribute', () => {
  788. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  789. } );
  790. } );
  791. describe( 'getAttributes()', () => {
  792. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  793. selection.setRanges( [ rangeInFullP ] );
  794. selection.setAttribute( 'foo', 'bar' );
  795. selection.setAttribute( 'abc', 'xyz' );
  796. const attrs = Array.from( selection.getAttributes() );
  797. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  798. } );
  799. } );
  800. describe( 'getAttributeKeys()', () => {
  801. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  802. selection.setRanges( [ rangeInFullP ] );
  803. selection.setAttribute( 'foo', 'bar' );
  804. selection.setAttribute( 'abc', 'xyz' );
  805. const attrs = Array.from( selection.getAttributeKeys() );
  806. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  807. } );
  808. } );
  809. describe( 'hasAttribute()', () => {
  810. it( 'should return true if element contains attribute with given key', () => {
  811. selection.setRanges( [ rangeInFullP ] );
  812. selection.setAttribute( 'foo', 'bar' );
  813. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  814. } );
  815. it( 'should return false if element does not contain attribute with given key', () => {
  816. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  817. } );
  818. } );
  819. describe( 'clearAttributes()', () => {
  820. it( 'should remove all attributes from the element', () => {
  821. selection.setRanges( [ rangeInFullP ] );
  822. selection.setAttribute( 'foo', 'bar' );
  823. selection.setAttribute( 'abc', 'xyz' );
  824. selection.clearAttributes();
  825. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  826. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  827. } );
  828. it( 'should fire change:attribute event with correct parameters', () => {
  829. selection.setAttribute( 'foo', 'bar' );
  830. selection.on( 'change:attribute', ( evt, data ) => {
  831. expect( data.directChange ).to.be.true;
  832. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  833. } );
  834. selection.clearAttributes();
  835. } );
  836. it( 'should not fire change:attribute event if there were no attributes', () => {
  837. const spy = sinon.spy();
  838. selection.on( 'change:attribute', spy );
  839. selection.clearAttributes();
  840. expect( spy.called ).to.be.false;
  841. } );
  842. } );
  843. describe( 'removeAttribute()', () => {
  844. it( 'should remove attribute', () => {
  845. selection.setRanges( [ rangeInFullP ] );
  846. selection.setAttribute( 'foo', 'bar' );
  847. selection.removeAttribute( 'foo' );
  848. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  849. } );
  850. it( 'should fire change:attribute event with correct parameters', () => {
  851. selection.setAttribute( 'foo', 'bar' );
  852. selection.on( 'change:attribute', ( evt, data ) => {
  853. expect( data.directChange ).to.be.true;
  854. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  855. } );
  856. selection.removeAttribute( 'foo' );
  857. } );
  858. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  859. const spy = sinon.spy();
  860. selection.on( 'change:attribute', spy );
  861. selection.removeAttribute( 'foo' );
  862. expect( spy.called ).to.be.false;
  863. } );
  864. } );
  865. describe( 'setAttributesTo()', () => {
  866. it( 'should remove all attributes set on element and set the given ones', () => {
  867. selection.setAttribute( 'abc', 'xyz' );
  868. selection.setAttributesTo( { foo: 'bar' } );
  869. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  870. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  871. } );
  872. it( 'should fire only one change:attribute event', () => {
  873. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  874. const spy = sinon.spy();
  875. selection.on( 'change:attribute', spy );
  876. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  877. expect( spy.calledOnce ).to.be.true;
  878. } );
  879. it( 'should fire change:attribute event with correct parameters', () => {
  880. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  881. selection.on( 'change:attribute', ( evt, data ) => {
  882. expect( data.directChange ).to.be.true;
  883. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  884. } );
  885. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  886. } );
  887. it( 'should not fire change:attribute event if attributes had not changed', () => {
  888. selection.setRanges( [ rangeInFullP ] );
  889. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  890. const spy = sinon.spy();
  891. selection.on( 'change:attribute', spy );
  892. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  893. expect( spy.called ).to.be.false;
  894. } );
  895. } );
  896. } );
  897. } );