8
0

selection.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. import Document from 'ckeditor5/engine/model/document.js';
  7. import Element from 'ckeditor5/engine/model/element.js';
  8. import Text from 'ckeditor5/engine/model/text.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/core/_utils/utils.js';
  15. import count from 'ckeditor5/utils/count.js';
  16. import { parse } from 'ckeditor5/engine/dev-utils/model.js';
  17. import Schema from 'ckeditor5/engine/model/schema.js';
  18. testUtils.createSinonSandbox();
  19. describe( 'Selection', () => {
  20. let doc, root, selection, liveRange, range, range1, range2, range3;
  21. beforeEach( () => {
  22. doc = new Document();
  23. root = doc.createRoot();
  24. root.appendChildren( [
  25. new Element( 'p' ),
  26. new Element( 'p' ),
  27. new Element( 'p', [], new Text( 'foobar' ) ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p' ),
  31. new Element( 'p', [], new Text( 'foobar' ) )
  32. ] );
  33. selection = new Selection();
  34. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  35. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  36. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  37. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  38. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  39. } );
  40. afterEach( () => {
  41. doc.destroy();
  42. liveRange.detach();
  43. } );
  44. describe( 'isCollapsed', () => {
  45. it( 'should return false for empty selection', () => {
  46. expect( selection.isCollapsed ).to.be.false;
  47. } );
  48. it( 'should return true when there is single collapsed ranges', () => {
  49. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  50. expect( selection.isCollapsed ).to.be.true;
  51. } );
  52. it( 'should return false when there are multiple ranges', () => {
  53. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  54. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  55. expect( selection.isCollapsed ).to.be.false;
  56. } );
  57. it( 'should return false when there is not collapsed range', () => {
  58. selection.addRange( range );
  59. expect( selection.isCollapsed ).to.be.false;
  60. } );
  61. } );
  62. describe( 'rangeCount', () => {
  63. it( 'should return proper range count', () => {
  64. expect( selection.rangeCount ).to.equal( 0 );
  65. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  66. expect( selection.rangeCount ).to.equal( 1 );
  67. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  68. expect( selection.rangeCount ).to.equal( 2 );
  69. } );
  70. } );
  71. describe( 'isBackward', () => {
  72. it( 'is defined by the last added range', () => {
  73. selection.addRange( range, true );
  74. expect( selection ).to.have.property( 'isBackward', true );
  75. selection.addRange( liveRange );
  76. expect( selection ).to.have.property( 'isBackward', false );
  77. } );
  78. it( 'is false when last range is collapsed', () => {
  79. const pos = Position.createAt( root, 0 );
  80. selection.addRange( new Range( pos, pos ), true );
  81. expect( selection.isBackward ).to.be.false;
  82. } );
  83. } );
  84. describe( 'addRange', () => {
  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 when range is invalid', () => {
  125. expect( () => {
  126. selection.addRange( { invalid: 'range' } );
  127. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  128. } );
  129. it( 'should throw an error if added range intersects with already stored range', () => {
  130. selection.addRange( liveRange );
  131. expect( () => {
  132. selection.addRange(
  133. new Range(
  134. new Position( root, [ 0, 4 ] ),
  135. new Position( root, [ 1, 2 ] )
  136. )
  137. );
  138. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  139. } );
  140. } );
  141. describe( 'collapse', () => {
  142. it( 'fires change:range', () => {
  143. const spy = sinon.spy();
  144. selection.on( 'change:range', spy );
  145. selection.collapse( root );
  146. expect( spy.calledOnce ).to.be.true;
  147. } );
  148. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  149. selection.collapse( root );
  150. expect( selection ).to.have.property( 'isCollapsed', true );
  151. const focus = selection.focus;
  152. expect( focus ).to.have.property( 'parent', root );
  153. expect( focus ).to.have.property( 'offset', 0 );
  154. } );
  155. it( 'sets selection at given offset in given parent', () => {
  156. selection.collapse( root, 3 );
  157. expect( selection ).to.have.property( 'isCollapsed', true );
  158. const focus = selection.focus;
  159. expect( focus ).to.have.property( 'parent', root );
  160. expect( focus ).to.have.property( 'offset', 3 );
  161. } );
  162. it( 'sets selection at the end of the given parent', () => {
  163. selection.collapse( root, 'end' );
  164. expect( selection ).to.have.property( 'isCollapsed', true );
  165. const focus = selection.focus;
  166. expect( focus ).to.have.property( 'parent', root );
  167. expect( focus ).to.have.property( 'offset', root.maxOffset );
  168. } );
  169. it( 'sets selection before the specified element', () => {
  170. selection.collapse( root.getChild( 1 ), 'before' );
  171. expect( selection ).to.have.property( 'isCollapsed', true );
  172. const focus = selection.focus;
  173. expect( focus ).to.have.property( 'parent', root );
  174. expect( focus ).to.have.property( 'offset', 1 );
  175. } );
  176. it( 'sets selection after the specified element', () => {
  177. selection.collapse( root.getChild( 1 ), 'after' );
  178. expect( selection ).to.have.property( 'isCollapsed', true );
  179. const focus = selection.focus;
  180. expect( focus ).to.have.property( 'parent', root );
  181. expect( focus ).to.have.property( 'offset', 2 );
  182. } );
  183. it( 'sets selection at the specified position', () => {
  184. const pos = Position.createFromParentAndOffset( root, 3 );
  185. selection.collapse( pos );
  186. expect( selection ).to.have.property( 'isCollapsed', true );
  187. const focus = selection.focus;
  188. expect( focus ).to.have.property( 'parent', root );
  189. expect( focus ).to.have.property( 'offset', 3 );
  190. } );
  191. } );
  192. describe( 'focus', () => {
  193. let r3;
  194. beforeEach( () => {
  195. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  196. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  197. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  198. selection.addRange( r1 );
  199. selection.addRange( r2 );
  200. } );
  201. it( 'should return correct focus when last added range is not backward one', () => {
  202. selection.addRange( r3 );
  203. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  204. } );
  205. it( 'should return correct focus when last added range is backward one', () => {
  206. selection.addRange( r3, true );
  207. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  208. } );
  209. it( 'should return null if no ranges in selection', () => {
  210. selection.removeAllRanges();
  211. expect( selection.focus ).to.be.null;
  212. } );
  213. } );
  214. describe( 'setFocus', () => {
  215. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  216. selection.addRange( range );
  217. selection.addRange( liveRange );
  218. const spy = sinon.spy();
  219. selection.on( 'change:range', spy );
  220. selection.setFocus( selection.focus );
  221. expect( count( selection.getRanges() ) ).to.equal( 2 );
  222. expect( spy.callCount ).to.equal( 0 );
  223. } );
  224. it( 'fires change:range', () => {
  225. selection.addRange( range );
  226. const spy = sinon.spy();
  227. selection.on( 'change:range', spy );
  228. selection.setFocus( Position.createAt( root, 'end' ) );
  229. expect( spy.calledOnce ).to.be.true;
  230. } );
  231. it( 'throws if there are no ranges in selection', () => {
  232. const endPos = Position.createAt( root, 'end' );
  233. expect( () => {
  234. selection.setFocus( endPos );
  235. } ).to.throw( CKEditorError, /model-selection-setFocus-no-ranges/ );
  236. } );
  237. it( 'modifies existing collapsed selection', () => {
  238. const startPos = Position.createAt( root, 1 );
  239. const endPos = Position.createAt( root, 2 );
  240. selection.collapse( startPos );
  241. selection.setFocus( endPos );
  242. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  243. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  244. } );
  245. it( 'makes existing collapsed selection a backward selection', () => {
  246. const startPos = Position.createAt( root, 1 );
  247. const endPos = Position.createAt( root, 0 );
  248. selection.collapse( startPos );
  249. selection.setFocus( endPos );
  250. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  251. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  252. expect( selection.isBackward ).to.be.true;
  253. } );
  254. it( 'modifies existing non-collapsed selection', () => {
  255. const startPos = Position.createAt( root, 1 );
  256. const endPos = Position.createAt( root, 2 );
  257. const newEndPos = Position.createAt( root, 3 );
  258. selection.addRange( new Range( startPos, endPos ) );
  259. selection.setFocus( newEndPos );
  260. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  261. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  262. } );
  263. it( 'makes existing non-collapsed selection a backward selection', () => {
  264. const startPos = Position.createAt( root, 1 );
  265. const endPos = Position.createAt( root, 2 );
  266. const newEndPos = Position.createAt( root, 0 );
  267. selection.addRange( new Range( startPos, endPos ) );
  268. selection.setFocus( newEndPos );
  269. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  270. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  271. expect( selection.isBackward ).to.be.true;
  272. } );
  273. it( 'makes existing backward selection a forward selection', () => {
  274. const startPos = Position.createAt( root, 1 );
  275. const endPos = Position.createAt( root, 2 );
  276. const newEndPos = Position.createAt( root, 3 );
  277. selection.addRange( new Range( startPos, endPos ), true );
  278. selection.setFocus( newEndPos );
  279. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  280. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  281. expect( selection.isBackward ).to.be.false;
  282. } );
  283. it( 'modifies existing backward selection', () => {
  284. const startPos = Position.createAt( root, 1 );
  285. const endPos = Position.createAt( root, 2 );
  286. const newEndPos = Position.createAt( root, 0 );
  287. selection.addRange( new Range( startPos, endPos ), true );
  288. selection.setFocus( newEndPos );
  289. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  290. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  291. expect( selection.isBackward ).to.be.true;
  292. } );
  293. it( 'modifies only the last range', () => {
  294. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  295. const startPos1 = Position.createAt( root, 4 );
  296. const endPos1 = Position.createAt( root, 5 );
  297. const startPos2 = Position.createAt( root, 1 );
  298. const endPos2 = Position.createAt( root, 2 );
  299. const newEndPos = Position.createAt( root, 0 );
  300. selection.addRange( new Range( startPos1, endPos1 ) );
  301. selection.addRange( new Range( startPos2, endPos2 ) );
  302. const spy = sinon.spy();
  303. selection.on( 'change:range', spy );
  304. selection.setFocus( newEndPos );
  305. const ranges = Array.from( selection.getRanges() );
  306. expect( ranges ).to.have.lengthOf( 2 );
  307. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  308. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  309. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  310. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  311. expect( selection.isBackward ).to.be.true;
  312. expect( spy.calledOnce ).to.be.true;
  313. } );
  314. it( 'collapses the selection when extending to the anchor', () => {
  315. const startPos = Position.createAt( root, 1 );
  316. const endPos = Position.createAt( root, 2 );
  317. selection.addRange( new Range( startPos, endPos ) );
  318. selection.setFocus( startPos );
  319. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  320. expect( selection.isCollapsed ).to.be.true;
  321. } );
  322. it( 'uses Position.createAt', () => {
  323. const startPos = Position.createAt( root, 1 );
  324. const endPos = Position.createAt( root, 2 );
  325. const newEndPos = Position.createAt( root, 4 );
  326. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  327. selection.addRange( new Range( startPos, endPos ) );
  328. selection.setFocus( root, 'end' );
  329. expect( spy.calledOnce ).to.be.true;
  330. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  331. } );
  332. } );
  333. describe( 'removeAllRanges', () => {
  334. let spy;
  335. it( 'should remove all stored ranges', () => {
  336. selection.addRange( liveRange );
  337. selection.addRange( range );
  338. selection.removeAllRanges();
  339. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  340. } );
  341. it( 'should fire exactly one change:range event', () => {
  342. selection.addRange( liveRange );
  343. selection.addRange( range );
  344. spy = sinon.spy();
  345. selection.on( 'change:range', spy );
  346. selection.removeAllRanges();
  347. expect( spy.calledOnce ).to.be.true;
  348. } );
  349. it( 'should not fire change:range event if there were no ranges', () => {
  350. spy = sinon.spy();
  351. selection.on( 'change:range', spy );
  352. selection.removeAllRanges();
  353. expect( spy.called ).to.be.false;
  354. } );
  355. } );
  356. describe( 'setRanges', () => {
  357. let newRanges, spy, oldRanges;
  358. beforeEach( () => {
  359. newRanges = [
  360. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  361. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  362. ];
  363. selection.addRange( liveRange );
  364. selection.addRange( range );
  365. spy = sinon.spy();
  366. selection.on( 'change:range', spy );
  367. oldRanges = selection._ranges;
  368. } );
  369. it( 'should throw an error when range is invalid', () => {
  370. expect( () => {
  371. selection.setRanges( [ { invalid: 'range' } ] );
  372. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  373. } );
  374. it( 'should remove all ranges and add given ranges', () => {
  375. selection.setRanges( newRanges );
  376. let ranges = Array.from( selection.getRanges() );
  377. expect( ranges ).to.deep.equal( newRanges );
  378. } );
  379. it( 'should use last range from given array to get anchor and focus position', () => {
  380. selection.setRanges( newRanges );
  381. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  382. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  383. } );
  384. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  385. selection.setRanges( newRanges, true );
  386. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  387. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  388. } );
  389. it( 'should fire exactly one change:range event', () => {
  390. selection.setRanges( newRanges );
  391. expect( spy.calledOnce ).to.be.true;
  392. } );
  393. it( 'should fire change:range event with correct parameters', () => {
  394. selection.on( 'change:range', ( evt, data ) => {
  395. expect( data.directChange ).to.be.true;
  396. } );
  397. selection.setRanges( newRanges );
  398. } );
  399. it( 'should not fire change:range event if given ranges are the same', () => {
  400. selection.setRanges( [ liveRange, range ] );
  401. expect( spy.calledOnce ).to.be.false;
  402. } );
  403. } );
  404. describe( 'setTo', () => {
  405. it( 'should set selection to be same as given selection, using setRanges method', () => {
  406. const spy = sinon.spy( selection, 'setRanges' );
  407. const otherSelection = new Selection();
  408. otherSelection.addRange( range1 );
  409. otherSelection.addRange( range2, true );
  410. selection.setTo( otherSelection );
  411. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  412. expect( selection.isBackward ).to.be.true;
  413. expect( selection.setRanges.calledOnce ).to.be.true;
  414. spy.restore();
  415. } );
  416. } );
  417. describe( 'getFirstRange', () => {
  418. it( 'should return null if no ranges were added', () => {
  419. expect( selection.getFirstRange() ).to.be.null;
  420. } );
  421. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  422. // This will not be the first range despite being added as first
  423. selection.addRange( range2 );
  424. // This should be the first range.
  425. selection.addRange( range1 );
  426. // A random range that is not first.
  427. selection.addRange( range3 );
  428. let range = selection.getFirstRange();
  429. expect( range.start.path ).to.deep.equal( [ 1 ] );
  430. expect( range.end.path ).to.deep.equal( [ 4 ] );
  431. } );
  432. } );
  433. describe( 'getFirstPosition', () => {
  434. it( 'should return null if no ranges were added', () => {
  435. expect( selection.getFirstPosition() ).to.be.null;
  436. } );
  437. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  438. // This will not be the first range despite being added as first
  439. selection.addRange( range2 );
  440. // This should be the first range.
  441. selection.addRange( range1 );
  442. // A random range that is not first.
  443. selection.addRange( range3 );
  444. let position = selection.getFirstPosition();
  445. expect( position.path ).to.deep.equal( [ 1 ] );
  446. } );
  447. } );
  448. describe( 'getLastRange', () => {
  449. it( 'should return null if no ranges were added', () => {
  450. expect( selection.getLastRange() ).to.be.null;
  451. } );
  452. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  453. selection.addRange( range3 );
  454. selection.addRange( range1 );
  455. selection.addRange( range2 );
  456. let range = selection.getLastRange();
  457. expect( range.start.path ).to.deep.equal( [ 6 ] );
  458. expect( range.end.path ).to.deep.equal( [ 7 ] );
  459. } );
  460. } );
  461. describe( 'getLastPosition', () => {
  462. it( 'should return null if no ranges were added', () => {
  463. expect( selection.getLastPosition() ).to.be.null;
  464. } );
  465. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  466. selection.addRange( range3 );
  467. selection.addRange( range1 );
  468. selection.addRange( range2 );
  469. let position = selection.getLastPosition();
  470. expect( position.path ).to.deep.equal( [ 7 ] );
  471. } );
  472. } );
  473. describe( 'isEqual', () => {
  474. it( 'should return true if selections equal', () => {
  475. selection.addRange( range1 );
  476. selection.addRange( range2 );
  477. const otherSelection = new Selection();
  478. otherSelection.addRange( range1 );
  479. otherSelection.addRange( range2 );
  480. expect( selection.isEqual( otherSelection ) ).to.be.true;
  481. } );
  482. it( 'should return true if backward selections equal', () => {
  483. selection.addRange( range1, true );
  484. const otherSelection = new Selection();
  485. otherSelection.addRange( range1, true );
  486. expect( selection.isEqual( otherSelection ) ).to.be.true;
  487. } );
  488. it( 'should return true if both selections have no ranges', () => {
  489. const otherSelection = new Selection();
  490. expect( selection.isEqual( otherSelection ) ).to.be.true;
  491. } );
  492. it( 'should return false if ranges count does not equal', () => {
  493. selection.addRange( range1 );
  494. selection.addRange( range2 );
  495. const otherSelection = new Selection();
  496. otherSelection.addRange( range2 );
  497. expect( selection.isEqual( otherSelection ) ).to.be.false;
  498. } );
  499. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  500. selection.addRange( range1 );
  501. selection.addRange( range3 );
  502. const otherSelection = new Selection();
  503. otherSelection.addRange( range2 );
  504. otherSelection.addRange( range3 );
  505. expect( selection.isEqual( otherSelection ) ).to.be.false;
  506. } );
  507. it( 'should return false if directions do not equal', () => {
  508. selection.addRange( range1 );
  509. const otherSelection = new Selection();
  510. otherSelection.addRange( range1, true );
  511. expect( selection.isEqual( otherSelection ) ).to.be.false;
  512. } );
  513. } );
  514. describe( 'collapseToStart', () => {
  515. it( 'should collapse to start position and fire change event', () => {
  516. selection.setRanges( [ range2, range1, range3 ] );
  517. const spy = sinon.spy();
  518. selection.on( 'change:range', spy );
  519. selection.collapseToStart();
  520. expect( selection.rangeCount ).to.equal( 1 );
  521. expect( selection.isCollapsed ).to.be.true;
  522. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  523. expect( spy.calledOnce ).to.be.true;
  524. } );
  525. it( 'should do nothing if selection was already collapsed', () => {
  526. selection.collapse( range1.start );
  527. const spy = sinon.spy( selection, 'fire' );
  528. selection.collapseToStart();
  529. expect( spy.notCalled ).to.be.true;
  530. spy.restore();
  531. } );
  532. it( 'should do nothing if no ranges present', () => {
  533. const spy = sinon.spy( selection, 'fire' );
  534. selection.collapseToStart();
  535. spy.restore();
  536. expect( spy.notCalled ).to.be.true;
  537. } );
  538. } );
  539. describe( 'collapseToEnd', () => {
  540. it( 'should collapse to start position and fire change:range event', () => {
  541. selection.setRanges( [ range2, range3, range1 ] );
  542. const spy = sinon.spy();
  543. selection.on( 'change:range', spy );
  544. selection.collapseToEnd();
  545. expect( selection.rangeCount ).to.equal( 1 );
  546. expect( selection.isCollapsed ).to.be.true;
  547. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  548. expect( spy.calledOnce ).to.be.true;
  549. } );
  550. it( 'should do nothing if selection was already collapsed', () => {
  551. selection.collapse( range1.start );
  552. const spy = sinon.spy( selection, 'fire' );
  553. selection.collapseToEnd();
  554. expect( spy.notCalled ).to.be.true;
  555. spy.restore();
  556. } );
  557. it( 'should do nothing if selection has no ranges', () => {
  558. const spy = sinon.spy( selection, 'fire' );
  559. selection.collapseToEnd();
  560. expect( spy.notCalled ).to.be.true;
  561. spy.restore();
  562. } );
  563. } );
  564. describe( 'createFromSelection', () => {
  565. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  566. selection.addRange( liveRange );
  567. selection.addRange( range, true );
  568. const snapshot = Selection.createFromSelection( selection );
  569. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  570. const selectionRanges = Array.from( selection.getRanges() );
  571. const snapshotRanges = Array.from( snapshot.getRanges() );
  572. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  573. for ( let i = 0; i < selectionRanges.length; i++ ) {
  574. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  575. }
  576. } );
  577. } );
  578. describe( 'attributes interface', () => {
  579. let rangeInFullP;
  580. beforeEach( () => {
  581. root.insertChildren( 0, [
  582. new Element( 'p', [], new Text( 'foobar' ) ),
  583. new Element( 'p', [], [] )
  584. ] );
  585. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  586. } );
  587. describe( 'setAttribute', () => {
  588. it( 'should set given attribute on the selection', () => {
  589. selection.setRanges( [ rangeInFullP ] );
  590. selection.setAttribute( 'foo', 'bar' );
  591. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  592. } );
  593. it( 'should fire change:attribute event with correct parameters', () => {
  594. selection.on( 'change:attribute', ( evt, data ) => {
  595. expect( data.directChange ).to.be.true;
  596. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  597. } );
  598. selection.setAttribute( 'foo', 'bar' );
  599. } );
  600. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  601. selection.setAttribute( 'foo', 'bar' );
  602. let spy = sinon.spy();
  603. selection.on( 'change:attribute', spy );
  604. selection.setAttribute( 'foo', 'bar' );
  605. expect( spy.called ).to.be.false;
  606. } );
  607. } );
  608. describe( 'getAttribute', () => {
  609. it( 'should return undefined if element does not contain given attribute', () => {
  610. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  611. } );
  612. } );
  613. describe( 'getAttributes', () => {
  614. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  615. selection.setRanges( [ rangeInFullP ] );
  616. selection.setAttribute( 'foo', 'bar' );
  617. selection.setAttribute( 'abc', 'xyz' );
  618. let attrs = Array.from( selection.getAttributes() );
  619. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  620. } );
  621. } );
  622. describe( 'getAttributeKeys', () => {
  623. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  624. selection.setRanges( [ rangeInFullP ] );
  625. selection.setAttribute( 'foo', 'bar' );
  626. selection.setAttribute( 'abc', 'xyz' );
  627. let attrs = Array.from( selection.getAttributeKeys() );
  628. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  629. } );
  630. } );
  631. describe( 'hasAttribute', () => {
  632. it( 'should return true if element contains attribute with given key', () => {
  633. selection.setRanges( [ rangeInFullP ] );
  634. selection.setAttribute( 'foo', 'bar' );
  635. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  636. } );
  637. it( 'should return false if element does not contain attribute with given key', () => {
  638. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  639. } );
  640. } );
  641. describe( 'clearAttributes', () => {
  642. it( 'should remove all attributes from the element', () => {
  643. selection.setRanges( [ rangeInFullP ] );
  644. selection.setAttribute( 'foo', 'bar' );
  645. selection.setAttribute( 'abc', 'xyz' );
  646. selection.clearAttributes();
  647. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  648. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  649. } );
  650. it( 'should fire change:attribute event with correct parameters', () => {
  651. selection.setAttribute( 'foo', 'bar' );
  652. selection.on( 'change:attribute', ( evt, data ) => {
  653. expect( data.directChange ).to.be.true;
  654. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  655. } );
  656. selection.clearAttributes();
  657. } );
  658. it( 'should not fire change:attribute event if there were no attributes', () => {
  659. let spy = sinon.spy();
  660. selection.on( 'change:attribute', spy );
  661. selection.clearAttributes();
  662. expect( spy.called ).to.be.false;
  663. } );
  664. } );
  665. describe( 'removeAttribute', () => {
  666. it( 'should remove attribute', () => {
  667. selection.setRanges( [ rangeInFullP ] );
  668. selection.setAttribute( 'foo', 'bar' );
  669. selection.removeAttribute( 'foo' );
  670. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  671. } );
  672. it( 'should fire change:attribute event with correct parameters', () => {
  673. selection.setAttribute( 'foo', 'bar' );
  674. selection.on( 'change:attribute', ( evt, data ) => {
  675. expect( data.directChange ).to.be.true;
  676. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  677. } );
  678. selection.removeAttribute( 'foo' );
  679. } );
  680. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  681. let spy = sinon.spy();
  682. selection.on( 'change:attribute', spy );
  683. selection.removeAttribute( 'foo' );
  684. expect( spy.called ).to.be.false;
  685. } );
  686. } );
  687. describe( 'setAttributesTo', () => {
  688. it( 'should remove all attributes set on element and set the given ones', () => {
  689. selection.setAttribute( 'abc', 'xyz' );
  690. selection.setAttributesTo( { foo: 'bar' } );
  691. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  692. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  693. } );
  694. it( 'should fire only one change:attribute event', () => {
  695. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  696. let spy = sinon.spy();
  697. selection.on( 'change:attribute', spy );
  698. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  699. expect( spy.calledOnce ).to.be.true;
  700. } );
  701. it( 'should fire change:attribute event with correct parameters', () => {
  702. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  703. selection.on( 'change:attribute', ( evt, data ) => {
  704. expect( data.directChange ).to.be.true;
  705. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  706. } );
  707. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  708. } );
  709. it( 'should not fire change:attribute event if attributes had not changed', () => {
  710. selection.setRanges( [ rangeInFullP ] );
  711. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  712. let spy = sinon.spy();
  713. selection.on( 'change:attribute', spy );
  714. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  715. expect( spy.called ).to.be.false;
  716. } );
  717. } );
  718. } );
  719. describe( 'getSelectedElement', () => {
  720. let schema;
  721. beforeEach( () => {
  722. schema = new Schema();
  723. schema.registerItem( 'p', '$block' );
  724. } );
  725. it( 'should return selected element', () => {
  726. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  727. const p = model.getChild( 1 );
  728. expect( selection.getSelectedElement() ).to.equal( p );
  729. } );
  730. it( 'should return null if there is more than one range', () => {
  731. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  732. expect( selection.getSelectedElement() ).to.be.null;
  733. } );
  734. it( 'should return null if there is no selection', () => {
  735. expect( selection.getSelectedElement() ).to.be.null;
  736. } );
  737. it( 'should return null if selection is not over single element #1', () => {
  738. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  739. expect( selection.getSelectedElement() ).to.be.null;
  740. } );
  741. it( 'should return null if selection is not over single element #2', () => {
  742. const { selection } = parse( '<p>{bar}</p>', schema );
  743. expect( selection.getSelectedElement() ).to.be.null;
  744. } );
  745. } );
  746. } );