selection.js 31 KB

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