selection.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 copy added ranges and store multiple ranges', () => {
  81. selection.addRange( liveRange );
  82. selection.addRange( range );
  83. const ranges = selection._ranges;
  84. expect( ranges.length ).to.equal( 2 );
  85. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  86. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  87. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  88. expect( ranges[ 1 ] ).not.to.be.equal( range );
  89. } );
  90. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  91. selection.addRange( liveRange );
  92. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  93. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  94. selection.addRange( range );
  95. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  96. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  97. } );
  98. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  99. selection.addRange( liveRange, true );
  100. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  101. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  102. selection.addRange( range, true );
  103. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  104. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  105. } );
  106. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  107. selection.addRange( liveRange );
  108. const ranges = Array.from( selection.getRanges() );
  109. selection.addRange( range );
  110. expect( ranges.length ).to.equal( 1 );
  111. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  112. } );
  113. it( 'should fire change:range event when adding a range', () => {
  114. let spy = sinon.spy();
  115. selection.on( 'change:range', spy );
  116. selection.addRange( range );
  117. expect( spy.called ).to.be.true;
  118. } );
  119. it( 'should throw an error if added range intersects with already stored range', () => {
  120. selection.addRange( liveRange );
  121. expect( () => {
  122. selection.addRange(
  123. new Range(
  124. new Position( root, [ 0, 4 ] ),
  125. new Position( root, [ 1, 2 ] )
  126. )
  127. );
  128. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  129. } );
  130. } );
  131. describe( 'collapse', () => {
  132. it( 'fires change:range', () => {
  133. const spy = sinon.spy();
  134. selection.on( 'change:range', spy );
  135. selection.collapse( root );
  136. expect( spy.calledOnce ).to.be.true;
  137. } );
  138. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  139. selection.collapse( root );
  140. expect( selection ).to.have.property( 'isCollapsed', true );
  141. const focus = selection.focus;
  142. expect( focus ).to.have.property( 'parent', root );
  143. expect( focus ).to.have.property( 'offset', 0 );
  144. } );
  145. it( 'sets selection at given offset in given parent', () => {
  146. selection.collapse( root, 3 );
  147. expect( selection ).to.have.property( 'isCollapsed', true );
  148. const focus = selection.focus;
  149. expect( focus ).to.have.property( 'parent', root );
  150. expect( focus ).to.have.property( 'offset', 3 );
  151. } );
  152. it( 'sets selection at the end of the given parent', () => {
  153. selection.collapse( root, 'END' );
  154. expect( selection ).to.have.property( 'isCollapsed', true );
  155. const focus = selection.focus;
  156. expect( focus ).to.have.property( 'parent', root );
  157. expect( focus ).to.have.property( 'offset', root.getChildCount() );
  158. } );
  159. it( 'sets selection before the specified element', () => {
  160. selection.collapse( root.getChild( 1 ), 'BEFORE' );
  161. expect( selection ).to.have.property( 'isCollapsed', true );
  162. const focus = selection.focus;
  163. expect( focus ).to.have.property( 'parent', root );
  164. expect( focus ).to.have.property( 'offset', 1 );
  165. } );
  166. it( 'sets selection after the specified element', () => {
  167. selection.collapse( root.getChild( 1 ), 'AFTER' );
  168. expect( selection ).to.have.property( 'isCollapsed', true );
  169. const focus = selection.focus;
  170. expect( focus ).to.have.property( 'parent', root );
  171. expect( focus ).to.have.property( 'offset', 2 );
  172. } );
  173. it( 'sets selection at the specified position', () => {
  174. const pos = Position.createFromParentAndOffset( root, 3 );
  175. selection.collapse( pos );
  176. expect( selection ).to.have.property( 'isCollapsed', true );
  177. const focus = selection.focus;
  178. expect( focus ).to.have.property( 'parent', root );
  179. expect( focus ).to.have.property( 'offset', 3 );
  180. } );
  181. } );
  182. describe( 'setFocus', () => {
  183. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  184. selection.addRange( range );
  185. selection.addRange( liveRange );
  186. const spy = sinon.spy();
  187. selection.on( 'change:range', spy );
  188. selection.setFocus( selection.focus );
  189. expect( count( selection.getRanges() ) ).to.equal( 2 );
  190. expect( spy.callCount ).to.equal( 0 );
  191. } );
  192. it( 'fires change:range', () => {
  193. selection.addRange( range );
  194. const spy = sinon.spy();
  195. selection.on( 'change:range', spy );
  196. selection.setFocus( Position.createAt( root, 'END' ) );
  197. expect( spy.calledOnce ).to.be.true;
  198. } );
  199. it( 'throws if there are no ranges in selection', () => {
  200. const endPos = Position.createAt( root, 'END' );
  201. expect( () => {
  202. selection.setFocus( endPos );
  203. } ).to.throw( CKEditorError, /selection-setFocus-no-ranges/ );
  204. } );
  205. it( 'modifies existing collapsed selection', () => {
  206. const startPos = Position.createAt( root, 1 );
  207. const endPos = Position.createAt( root, 2 );
  208. selection.collapse( startPos );
  209. selection.setFocus( endPos );
  210. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  211. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  212. } );
  213. it( 'makes existing collapsed selection a backward selection', () => {
  214. const startPos = Position.createAt( root, 1 );
  215. const endPos = Position.createAt( root, 0 );
  216. selection.collapse( startPos );
  217. selection.setFocus( endPos );
  218. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  219. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  220. expect( selection.isBackward ).to.be.true;
  221. } );
  222. it( 'modifies existing non-collapsed selection', () => {
  223. const startPos = Position.createAt( root, 1 );
  224. const endPos = Position.createAt( root, 2 );
  225. const newEndPos = Position.createAt( root, 3 );
  226. selection.addRange( new Range( startPos, endPos ) );
  227. selection.setFocus( newEndPos );
  228. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  229. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  230. } );
  231. it( 'makes existing non-collapsed selection a backward selection', () => {
  232. const startPos = Position.createAt( root, 1 );
  233. const endPos = Position.createAt( root, 2 );
  234. const newEndPos = Position.createAt( root, 0 );
  235. selection.addRange( new Range( startPos, endPos ) );
  236. selection.setFocus( newEndPos );
  237. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  238. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  239. expect( selection.isBackward ).to.be.true;
  240. } );
  241. it( 'makes existing backward selection a forward selection', () => {
  242. const startPos = Position.createAt( root, 1 );
  243. const endPos = Position.createAt( root, 2 );
  244. const newEndPos = Position.createAt( root, 3 );
  245. selection.addRange( new Range( startPos, endPos ), true );
  246. selection.setFocus( newEndPos );
  247. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  248. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  249. expect( selection.isBackward ).to.be.false;
  250. } );
  251. it( 'modifies existing backward selection', () => {
  252. const startPos = Position.createAt( root, 1 );
  253. const endPos = Position.createAt( root, 2 );
  254. const newEndPos = Position.createAt( root, 0 );
  255. selection.addRange( new Range( startPos, endPos ), true );
  256. selection.setFocus( newEndPos );
  257. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  258. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  259. expect( selection.isBackward ).to.be.true;
  260. } );
  261. it( 'modifies only the last range', () => {
  262. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  263. const startPos1 = Position.createAt( root, 4 );
  264. const endPos1 = Position.createAt( root, 5 );
  265. const startPos2 = Position.createAt( root, 1 );
  266. const endPos2 = Position.createAt( root, 2 );
  267. const newEndPos = Position.createAt( root, 0 );
  268. selection.addRange( new Range( startPos1, endPos1 ) );
  269. selection.addRange( new Range( startPos2, endPos2 ) );
  270. const spy = sinon.spy();
  271. selection.on( 'change:range', spy );
  272. selection.setFocus( newEndPos );
  273. const ranges = Array.from( selection.getRanges() );
  274. expect( ranges ).to.have.lengthOf( 2 );
  275. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  276. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  277. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  278. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  279. expect( selection.isBackward ).to.be.true;
  280. expect( spy.calledOnce ).to.be.true;
  281. } );
  282. it( 'collapses the selection when extending to the anchor', () => {
  283. const startPos = Position.createAt( root, 1 );
  284. const endPos = Position.createAt( root, 2 );
  285. selection.addRange( new Range( startPos, endPos ) );
  286. selection.setFocus( startPos );
  287. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  288. expect( selection.isCollapsed ).to.be.true;
  289. } );
  290. it( 'uses Position.createAt', () => {
  291. const startPos = Position.createAt( root, 1 );
  292. const endPos = Position.createAt( root, 2 );
  293. const newEndPos = Position.createAt( root, 4 );
  294. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  295. selection.addRange( new Range( startPos, endPos ) );
  296. selection.setFocus( root, 'END' );
  297. expect( spy.calledOnce ).to.be.true;
  298. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  299. } );
  300. } );
  301. describe( 'removeAllRanges', () => {
  302. let spy, ranges;
  303. beforeEach( () => {
  304. selection.addRange( liveRange );
  305. selection.addRange( range );
  306. spy = sinon.spy();
  307. selection.on( 'change:range', spy );
  308. ranges = selection._ranges;
  309. selection.removeAllRanges();
  310. } );
  311. it( 'should remove all stored ranges', () => {
  312. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  313. } );
  314. it( 'should fire exactly one update event', () => {
  315. expect( spy.calledOnce ).to.be.true;
  316. } );
  317. } );
  318. describe( 'setRanges', () => {
  319. let newRanges, spy, oldRanges;
  320. before( () => {
  321. newRanges = [
  322. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  323. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  324. ];
  325. } );
  326. beforeEach( () => {
  327. selection.addRange( liveRange );
  328. selection.addRange( range );
  329. spy = sinon.spy();
  330. selection.on( 'change:range', spy );
  331. oldRanges = selection._ranges;
  332. } );
  333. it( 'should remove all ranges and add given ranges', () => {
  334. selection.setRanges( newRanges );
  335. let ranges = selection._ranges;
  336. expect( ranges.length ).to.equal( 2 );
  337. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  338. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  339. } );
  340. it( 'should use last range from given array to get anchor and focus position', () => {
  341. selection.setRanges( newRanges );
  342. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  343. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  344. } );
  345. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  346. selection.setRanges( newRanges, true );
  347. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  348. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  349. } );
  350. it( 'should fire exactly one update event', () => {
  351. selection.setRanges( newRanges );
  352. expect( spy.calledOnce ).to.be.true;
  353. } );
  354. } );
  355. describe( 'getFirstRange', () => {
  356. it( 'should return null if no ranges were added', () => {
  357. expect( selection.getFirstRange() ).to.be.null;
  358. } );
  359. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  360. // This will not be the first range despite being added as first
  361. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  362. // This should be the first range.
  363. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  364. // A random range that is not first.
  365. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  366. let range = selection.getFirstRange();
  367. expect( range.start.path ).to.deep.equal( [ 1 ] );
  368. expect( range.end.path ).to.deep.equal( [ 4 ] );
  369. } );
  370. } );
  371. describe( 'getFirstPosition', () => {
  372. it( 'should return null if no ranges were added', () => {
  373. expect( selection.getFirstPosition() ).to.be.null;
  374. } );
  375. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  376. // This will not be a range containing the first position despite being added as first
  377. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  378. // This should be the first range.
  379. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  380. // A random range that is not first.
  381. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  382. let position = selection.getFirstPosition();
  383. expect( position.path ).to.deep.equal( [ 1 ] );
  384. } );
  385. } );
  386. describe( 'createFromSelection', () => {
  387. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  388. selection.addRange( liveRange );
  389. selection.addRange( range, true );
  390. const snapshot = Selection.createFromSelection( selection );
  391. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  392. const selectionRanges = Array.from( selection.getRanges() );
  393. const snapshotRanges = Array.from( snapshot.getRanges() );
  394. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  395. for ( let i = 0; i < selectionRanges.length; i++ ) {
  396. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  397. }
  398. } );
  399. } );
  400. } );