selection.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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: '$text', inside: 'image' } );
  629. // Special block which can contain another blocks.
  630. doc.schema.registerItem( 'nestedBlock', '$block' );
  631. doc.schema.allow( { name: 'nestedBlock', inside: '$block' } );
  632. } );
  633. it( 'returns an iterator', () => {
  634. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  635. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  636. } );
  637. it( 'returns block for a collapsed selection', () => {
  638. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  639. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  640. } );
  641. it( 'returns block for a non collapsed selection', () => {
  642. setData( doc, '<p>a</p><p>[b]</p><p>c</p>' );
  643. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  644. } );
  645. it( 'returns two blocks for a non collapsed selection', () => {
  646. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  647. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  648. } );
  649. // This isn't a behaviour that we like (https://github.com/ckeditor/ckeditor5-heading/issues/9) but I don't
  650. // want to work on this issue now, when porting this method from the list/heading features.
  651. // It has to be covered separately.
  652. it( 'returns two blocks for a non collapsed selection if only end of selection is touching a block', () => {
  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 proper block for a multi-range selection', () => {
  657. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  658. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  659. } );
  660. it( 'does not return a block twice if two ranges are anchored in it', () => {
  661. setData( doc, '<p>[a]b[c]</p>' );
  662. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  663. } );
  664. it( 'returns only blocks', () => {
  665. setData( doc, '<p>[a</p><image>b</image><p>c]</p>' );
  666. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  667. } );
  668. it( 'gets deeper into the tree', () => {
  669. setData( doc, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  670. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  671. } );
  672. it( 'gets deeper into the tree (end deeper)', () => {
  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' ] );
  675. } );
  676. it( 'gets deeper into the tree (start 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( [ 'c', 'd' ] );
  679. } );
  680. it( 'returns an empty array if none of the selected elements is a block', () => {
  681. setData( doc, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  682. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  683. } );
  684. it( 'returns an empty array if the selected element is not a block', () => {
  685. setData( doc, '<p>a</p><image>[]a</image><p>b</p>' );
  686. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  687. } );
  688. // Super edge case – should not happen (blocks should never be nested),
  689. // but since the code handles it already it's worth testing.
  690. it( 'returns only the lowest block if blocks are nested', () => {
  691. setData( doc, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  692. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  693. } );
  694. // Like above but trickier.
  695. it( 'returns only the lowest block if blocks are nested', () => {
  696. setData(
  697. doc,
  698. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  699. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  700. );
  701. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  702. } );
  703. it( 'returns nothing if directly in a root', () => {
  704. doc.createRoot( 'p', 'inlineOnlyRoot' );
  705. setData( doc, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  706. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  707. } );
  708. function toText( elements ) {
  709. return Array.from( elements ).map( el => el.getChild( 0 ).data );
  710. }
  711. } );
  712. describe( 'attributes interface', () => {
  713. let rangeInFullP;
  714. beforeEach( () => {
  715. root.insertChildren( 0, [
  716. new Element( 'p', [], new Text( 'foobar' ) ),
  717. new Element( 'p', [], [] )
  718. ] );
  719. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  720. } );
  721. describe( 'setAttribute()', () => {
  722. it( 'should set given attribute on the selection', () => {
  723. selection.setRanges( [ rangeInFullP ] );
  724. selection.setAttribute( 'foo', 'bar' );
  725. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  726. } );
  727. it( 'should fire change:attribute event with correct parameters', () => {
  728. selection.on( 'change:attribute', ( evt, data ) => {
  729. expect( data.directChange ).to.be.true;
  730. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  731. } );
  732. selection.setAttribute( 'foo', 'bar' );
  733. } );
  734. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  735. selection.setAttribute( 'foo', 'bar' );
  736. const spy = sinon.spy();
  737. selection.on( 'change:attribute', spy );
  738. selection.setAttribute( 'foo', 'bar' );
  739. expect( spy.called ).to.be.false;
  740. } );
  741. } );
  742. describe( 'getAttribute()', () => {
  743. it( 'should return undefined if element does not contain given attribute', () => {
  744. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  745. } );
  746. } );
  747. describe( 'getAttributes()', () => {
  748. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  749. selection.setRanges( [ rangeInFullP ] );
  750. selection.setAttribute( 'foo', 'bar' );
  751. selection.setAttribute( 'abc', 'xyz' );
  752. const attrs = Array.from( selection.getAttributes() );
  753. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  754. } );
  755. } );
  756. describe( 'getAttributeKeys()', () => {
  757. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  758. selection.setRanges( [ rangeInFullP ] );
  759. selection.setAttribute( 'foo', 'bar' );
  760. selection.setAttribute( 'abc', 'xyz' );
  761. const attrs = Array.from( selection.getAttributeKeys() );
  762. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  763. } );
  764. } );
  765. describe( 'hasAttribute()', () => {
  766. it( 'should return true if element contains attribute with given key', () => {
  767. selection.setRanges( [ rangeInFullP ] );
  768. selection.setAttribute( 'foo', 'bar' );
  769. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  770. } );
  771. it( 'should return false if element does not contain attribute with given key', () => {
  772. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  773. } );
  774. } );
  775. describe( 'clearAttributes()', () => {
  776. it( 'should remove all attributes from the element', () => {
  777. selection.setRanges( [ rangeInFullP ] );
  778. selection.setAttribute( 'foo', 'bar' );
  779. selection.setAttribute( 'abc', 'xyz' );
  780. selection.clearAttributes();
  781. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  782. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  783. } );
  784. it( 'should fire change:attribute event with correct parameters', () => {
  785. selection.setAttribute( 'foo', 'bar' );
  786. selection.on( 'change:attribute', ( evt, data ) => {
  787. expect( data.directChange ).to.be.true;
  788. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  789. } );
  790. selection.clearAttributes();
  791. } );
  792. it( 'should not fire change:attribute event if there were no attributes', () => {
  793. const spy = sinon.spy();
  794. selection.on( 'change:attribute', spy );
  795. selection.clearAttributes();
  796. expect( spy.called ).to.be.false;
  797. } );
  798. } );
  799. describe( 'removeAttribute()', () => {
  800. it( 'should remove attribute', () => {
  801. selection.setRanges( [ rangeInFullP ] );
  802. selection.setAttribute( 'foo', 'bar' );
  803. selection.removeAttribute( 'foo' );
  804. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  805. } );
  806. it( 'should fire change:attribute event with correct parameters', () => {
  807. selection.setAttribute( 'foo', 'bar' );
  808. selection.on( 'change:attribute', ( evt, data ) => {
  809. expect( data.directChange ).to.be.true;
  810. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  811. } );
  812. selection.removeAttribute( 'foo' );
  813. } );
  814. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  815. const spy = sinon.spy();
  816. selection.on( 'change:attribute', spy );
  817. selection.removeAttribute( 'foo' );
  818. expect( spy.called ).to.be.false;
  819. } );
  820. } );
  821. describe( 'setAttributesTo()', () => {
  822. it( 'should remove all attributes set on element and set the given ones', () => {
  823. selection.setAttribute( 'abc', 'xyz' );
  824. selection.setAttributesTo( { foo: 'bar' } );
  825. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  826. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  827. } );
  828. it( 'should fire only one change:attribute event', () => {
  829. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  830. const spy = sinon.spy();
  831. selection.on( 'change:attribute', spy );
  832. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  833. expect( spy.calledOnce ).to.be.true;
  834. } );
  835. it( 'should fire change:attribute event with correct parameters', () => {
  836. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  837. selection.on( 'change:attribute', ( evt, data ) => {
  838. expect( data.directChange ).to.be.true;
  839. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  840. } );
  841. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  842. } );
  843. it( 'should not fire change:attribute event if attributes had not changed', () => {
  844. selection.setRanges( [ rangeInFullP ] );
  845. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  846. const spy = sinon.spy();
  847. selection.on( 'change:attribute', spy );
  848. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  849. expect( spy.called ).to.be.false;
  850. } );
  851. } );
  852. } );
  853. } );