selection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Element from '/ckeditor5/engine/model/element.js';
  9. import Range from '/ckeditor5/engine/model/range.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import LiveRange from '/ckeditor5/engine/model/liverange.js';
  12. import Selection from '/ckeditor5/engine/model/selection.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  15. import count from '/ckeditor5/utils/count.js';
  16. testUtils.createSinonSandbox();
  17. describe( 'Selection', () => {
  18. let doc, root, selection, liveRange, range;
  19. beforeEach( () => {
  20. doc = new Document();
  21. root = doc.createRoot();
  22. root.appendChildren( [
  23. new Element( 'p' ),
  24. new Element( 'p' ),
  25. new Element( 'p', [], 'foobar' ),
  26. new Element( 'p' ),
  27. new Element( 'p' ),
  28. new Element( 'p' ),
  29. new Element( 'p', [], 'foobar' )
  30. ] );
  31. selection = new Selection();
  32. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  33. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  34. } );
  35. afterEach( () => {
  36. doc.destroy();
  37. liveRange.detach();
  38. } );
  39. describe( 'isCollapsed', () => {
  40. it( 'should return false for empty selection', () => {
  41. expect( selection.isCollapsed ).to.be.false;
  42. } );
  43. it( 'should return true when there is single collapsed ranges', () => {
  44. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  45. expect( selection.isCollapsed ).to.be.true;
  46. } );
  47. it( 'should return false when there are multiple ranges', () => {
  48. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  49. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  50. expect( selection.isCollapsed ).to.be.false;
  51. } );
  52. it( 'should return false when there is not collapsed range', () => {
  53. selection.addRange( range );
  54. expect( selection.isCollapsed ).to.be.false;
  55. } );
  56. } );
  57. describe( 'rangeCount', () => {
  58. it( 'should return proper range count', () => {
  59. expect( selection.rangeCount ).to.equal( 0 );
  60. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  61. expect( selection.rangeCount ).to.equal( 1 );
  62. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  63. expect( selection.rangeCount ).to.equal( 2 );
  64. } );
  65. } );
  66. describe( 'isBackward', () => {
  67. it( 'is defined by the last added range', () => {
  68. selection.addRange( range, true );
  69. expect( selection ).to.have.property( 'isBackward', true );
  70. selection.addRange( liveRange );
  71. expect( selection ).to.have.property( 'isBackward', false );
  72. } );
  73. it( 'is false when last range is collapsed', () => {
  74. const pos = Position.createAt( root, 0 );
  75. selection.addRange( new Range( pos, pos ), true );
  76. expect( selection.isBackward ).to.be.false;
  77. } );
  78. } );
  79. describe( 'addRange', () => {
  80. it( 'should throw an error when range is invalid', () => {
  81. expect( () => {
  82. selection.addRange( { invalid: 'Range' } );
  83. } ).to.throw( CKEditorError, 'model-selection-invalid-range: Invalid Range.' );
  84. } );
  85. it( 'should copy added ranges and store multiple ranges', () => {
  86. selection.addRange( liveRange );
  87. selection.addRange( range );
  88. const ranges = selection._ranges;
  89. expect( ranges.length ).to.equal( 2 );
  90. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  91. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  92. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  93. expect( ranges[ 1 ] ).not.to.be.equal( range );
  94. } );
  95. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  96. selection.addRange( liveRange );
  97. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  98. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  99. selection.addRange( range );
  100. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  101. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  102. } );
  103. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  104. selection.addRange( liveRange, true );
  105. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  106. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  107. selection.addRange( range, true );
  108. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  109. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  110. } );
  111. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  112. selection.addRange( liveRange );
  113. const ranges = Array.from( selection.getRanges() );
  114. selection.addRange( range );
  115. expect( ranges.length ).to.equal( 1 );
  116. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  117. } );
  118. it( 'should fire change:range event when adding a range', () => {
  119. let spy = sinon.spy();
  120. selection.on( 'change:range', spy );
  121. selection.addRange( range );
  122. expect( spy.called ).to.be.true;
  123. } );
  124. it( 'should throw an error if added range intersects with already stored range', () => {
  125. selection.addRange( liveRange );
  126. expect( () => {
  127. selection.addRange(
  128. new Range(
  129. new Position( root, [ 0, 4 ] ),
  130. new Position( root, [ 1, 2 ] )
  131. )
  132. );
  133. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  134. } );
  135. } );
  136. describe( 'collapse', () => {
  137. it( 'fires change:range', () => {
  138. const spy = sinon.spy();
  139. selection.on( 'change:range', spy );
  140. selection.collapse( root );
  141. expect( spy.calledOnce ).to.be.true;
  142. } );
  143. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  144. selection.collapse( root );
  145. expect( selection ).to.have.property( 'isCollapsed', true );
  146. const focus = selection.focus;
  147. expect( focus ).to.have.property( 'parent', root );
  148. expect( focus ).to.have.property( 'offset', 0 );
  149. } );
  150. it( 'sets selection at given offset in given parent', () => {
  151. selection.collapse( root, 3 );
  152. expect( selection ).to.have.property( 'isCollapsed', true );
  153. const focus = selection.focus;
  154. expect( focus ).to.have.property( 'parent', root );
  155. expect( focus ).to.have.property( 'offset', 3 );
  156. } );
  157. it( 'sets selection at the end of the given parent', () => {
  158. selection.collapse( root, 'END' );
  159. expect( selection ).to.have.property( 'isCollapsed', true );
  160. const focus = selection.focus;
  161. expect( focus ).to.have.property( 'parent', root );
  162. expect( focus ).to.have.property( 'offset', root.getChildCount() );
  163. } );
  164. it( 'sets selection before the specified element', () => {
  165. selection.collapse( root.getChild( 1 ), 'BEFORE' );
  166. expect( selection ).to.have.property( 'isCollapsed', true );
  167. const focus = selection.focus;
  168. expect( focus ).to.have.property( 'parent', root );
  169. expect( focus ).to.have.property( 'offset', 1 );
  170. } );
  171. it( 'sets selection after the specified element', () => {
  172. selection.collapse( root.getChild( 1 ), 'AFTER' );
  173. expect( selection ).to.have.property( 'isCollapsed', true );
  174. const focus = selection.focus;
  175. expect( focus ).to.have.property( 'parent', root );
  176. expect( focus ).to.have.property( 'offset', 2 );
  177. } );
  178. it( 'sets selection at the specified position', () => {
  179. const pos = Position.createFromParentAndOffset( root, 3 );
  180. selection.collapse( pos );
  181. expect( selection ).to.have.property( 'isCollapsed', true );
  182. const focus = selection.focus;
  183. expect( focus ).to.have.property( 'parent', root );
  184. expect( focus ).to.have.property( 'offset', 3 );
  185. } );
  186. } );
  187. describe( 'setFocus', () => {
  188. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  189. selection.addRange( range );
  190. selection.addRange( liveRange );
  191. const spy = sinon.spy();
  192. selection.on( 'change:range', spy );
  193. selection.setFocus( selection.focus );
  194. expect( count( selection.getRanges() ) ).to.equal( 2 );
  195. expect( spy.callCount ).to.equal( 0 );
  196. } );
  197. it( 'fires change:range', () => {
  198. selection.addRange( range );
  199. const spy = sinon.spy();
  200. selection.on( 'change:range', spy );
  201. selection.setFocus( Position.createAt( root, 'END' ) );
  202. expect( spy.calledOnce ).to.be.true;
  203. } );
  204. it( 'throws if there are no ranges in selection', () => {
  205. const endPos = Position.createAt( root, 'END' );
  206. expect( () => {
  207. selection.setFocus( endPos );
  208. } ).to.throw( CKEditorError, /selection-setFocus-no-ranges/ );
  209. } );
  210. it( 'modifies existing collapsed selection', () => {
  211. const startPos = Position.createAt( root, 1 );
  212. const endPos = Position.createAt( root, 2 );
  213. selection.collapse( startPos );
  214. selection.setFocus( endPos );
  215. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  216. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  217. } );
  218. it( 'makes existing collapsed selection a backward selection', () => {
  219. const startPos = Position.createAt( root, 1 );
  220. const endPos = Position.createAt( root, 0 );
  221. selection.collapse( startPos );
  222. selection.setFocus( endPos );
  223. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  224. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  225. expect( selection.isBackward ).to.be.true;
  226. } );
  227. it( 'modifies existing non-collapsed selection', () => {
  228. const startPos = Position.createAt( root, 1 );
  229. const endPos = Position.createAt( root, 2 );
  230. const newEndPos = Position.createAt( root, 3 );
  231. selection.addRange( new Range( startPos, endPos ) );
  232. selection.setFocus( newEndPos );
  233. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  234. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  235. } );
  236. it( 'makes existing non-collapsed selection a backward selection', () => {
  237. const startPos = Position.createAt( root, 1 );
  238. const endPos = Position.createAt( root, 2 );
  239. const newEndPos = Position.createAt( root, 0 );
  240. selection.addRange( new Range( startPos, endPos ) );
  241. selection.setFocus( newEndPos );
  242. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  243. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  244. expect( selection.isBackward ).to.be.true;
  245. } );
  246. it( 'makes existing backward selection a forward selection', () => {
  247. const startPos = Position.createAt( root, 1 );
  248. const endPos = Position.createAt( root, 2 );
  249. const newEndPos = Position.createAt( root, 3 );
  250. selection.addRange( new Range( startPos, endPos ), true );
  251. selection.setFocus( newEndPos );
  252. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  253. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  254. expect( selection.isBackward ).to.be.false;
  255. } );
  256. it( 'modifies existing backward selection', () => {
  257. const startPos = Position.createAt( root, 1 );
  258. const endPos = Position.createAt( root, 2 );
  259. const newEndPos = Position.createAt( root, 0 );
  260. selection.addRange( new Range( startPos, endPos ), true );
  261. selection.setFocus( newEndPos );
  262. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  263. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  264. expect( selection.isBackward ).to.be.true;
  265. } );
  266. it( 'modifies only the last range', () => {
  267. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  268. const startPos1 = Position.createAt( root, 4 );
  269. const endPos1 = Position.createAt( root, 5 );
  270. const startPos2 = Position.createAt( root, 1 );
  271. const endPos2 = Position.createAt( root, 2 );
  272. const newEndPos = Position.createAt( root, 0 );
  273. selection.addRange( new Range( startPos1, endPos1 ) );
  274. selection.addRange( new Range( startPos2, endPos2 ) );
  275. const spy = sinon.spy();
  276. selection.on( 'change:range', spy );
  277. selection.setFocus( newEndPos );
  278. const ranges = Array.from( selection.getRanges() );
  279. expect( ranges ).to.have.lengthOf( 2 );
  280. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  281. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  282. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  283. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  284. expect( selection.isBackward ).to.be.true;
  285. expect( spy.calledOnce ).to.be.true;
  286. } );
  287. it( 'collapses the selection when extending to the anchor', () => {
  288. const startPos = Position.createAt( root, 1 );
  289. const endPos = Position.createAt( root, 2 );
  290. selection.addRange( new Range( startPos, endPos ) );
  291. selection.setFocus( startPos );
  292. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  293. expect( selection.isCollapsed ).to.be.true;
  294. } );
  295. it( 'uses Position.createAt', () => {
  296. const startPos = Position.createAt( root, 1 );
  297. const endPos = Position.createAt( root, 2 );
  298. const newEndPos = Position.createAt( root, 4 );
  299. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  300. selection.addRange( new Range( startPos, endPos ) );
  301. selection.setFocus( root, 'END' );
  302. expect( spy.calledOnce ).to.be.true;
  303. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  304. } );
  305. } );
  306. describe( 'removeAllRanges', () => {
  307. let spy, ranges;
  308. beforeEach( () => {
  309. selection.addRange( liveRange );
  310. selection.addRange( range );
  311. spy = sinon.spy();
  312. selection.on( 'change:range', spy );
  313. ranges = selection._ranges;
  314. selection.removeAllRanges();
  315. } );
  316. it( 'should remove all stored ranges', () => {
  317. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  318. } );
  319. it( 'should fire exactly one update event', () => {
  320. expect( spy.calledOnce ).to.be.true;
  321. } );
  322. } );
  323. describe( 'setRanges', () => {
  324. let newRanges, spy, oldRanges;
  325. before( () => {
  326. newRanges = [
  327. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  328. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  329. ];
  330. } );
  331. beforeEach( () => {
  332. selection.addRange( liveRange );
  333. selection.addRange( range );
  334. spy = sinon.spy();
  335. selection.on( 'change:range', spy );
  336. oldRanges = selection._ranges;
  337. } );
  338. it( 'should throw an error when range is invalid', () => {
  339. expect( () => {
  340. selection.setRanges( [ { invalid: 'range' } ] );
  341. } ).to.throw( CKEditorError, 'model-selection-invalid-range: Invalid Range.' );
  342. } );
  343. it( 'should remove all ranges and add given ranges', () => {
  344. selection.setRanges( newRanges );
  345. let ranges = selection._ranges;
  346. expect( ranges.length ).to.equal( 2 );
  347. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  348. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  349. } );
  350. it( 'should use last range from given array to get anchor and focus position', () => {
  351. selection.setRanges( newRanges );
  352. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  353. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  354. } );
  355. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  356. selection.setRanges( newRanges, true );
  357. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  358. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  359. } );
  360. it( 'should fire exactly one update event', () => {
  361. selection.setRanges( newRanges );
  362. expect( spy.calledOnce ).to.be.true;
  363. } );
  364. } );
  365. describe( 'getFirstRange', () => {
  366. it( 'should return null if no ranges were added', () => {
  367. expect( selection.getFirstRange() ).to.be.null;
  368. } );
  369. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  370. // This will not be the first range despite being added as first
  371. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  372. // This should be the first range.
  373. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  374. // A random range that is not first.
  375. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  376. let range = selection.getFirstRange();
  377. expect( range.start.path ).to.deep.equal( [ 1 ] );
  378. expect( range.end.path ).to.deep.equal( [ 4 ] );
  379. } );
  380. } );
  381. describe( 'getFirstPosition', () => {
  382. it( 'should return null if no ranges were added', () => {
  383. expect( selection.getFirstPosition() ).to.be.null;
  384. } );
  385. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  386. // This will not be a range containing the first position despite being added as first
  387. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  388. // This should be the first range.
  389. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  390. // A random range that is not first.
  391. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  392. let position = selection.getFirstPosition();
  393. expect( position.path ).to.deep.equal( [ 1 ] );
  394. } );
  395. } );
  396. describe( 'createFromSelection', () => {
  397. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  398. selection.addRange( liveRange );
  399. selection.addRange( range, true );
  400. const snapshot = Selection.createFromSelection( selection );
  401. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  402. const selectionRanges = Array.from( selection.getRanges() );
  403. const snapshotRanges = Array.from( snapshot.getRanges() );
  404. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  405. for ( let i = 0; i < selectionRanges.length; i++ ) {
  406. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  407. }
  408. } );
  409. } );
  410. } );